http_test

package module
v0.0.0-...-3e41d62 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2016 License: MIT Imports: 0 Imported by: 0

README

http_test

Test helpers for the net/http requests.

Request Builder

import request "github.com/vsco/http_test/builder"

type json struct {
	Foo string `json:"foo"`
}

// Example Handler and Mux
func testHandler(c web.C, w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(r.Method))
}

s := web.New()
s.Post("/post", testHandler)

// Sending Requests. 
resp := Post("/post").Use(s).Do()

// Sending POST Params
resp := Post("/post").Use(s).Params(map[string]string{"foo":"bar",}).Do()
	
// Sending JSON POST Bodies
js := &json{
	Foo: "bar",
}
resp := Post("/post").Use(s).JSON(js).Do()
	
// Sending Headers
resp := Post("/post").Use(s).Header("foo", "bar").Do()
resp := Post("/post").Use(s).Headers(map[string]string{"foo":"bar",}).Do()

Asserting Responses


import (
	"github.com/vsco/http_test/builder"
	"github.com/vsco/http_test/assert"
)

func jsonEchoHandler(c web.C, w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)

	if err != nil {
		log.Fatal(err)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(body)
}

type json struct {
	Foo string `json:"foo"`
}

func TestResponse(t *testing.T) {
	s := web.New()
	s.Post("/post", jsonEchoHandler)
	
	js := &json{
		Foo: "bar",
	}
	
	req := Post("/post").Use(s).JSON(js).Do()
	
	expected := &json{
		Foo: "bar",
	}

	assert.Response(t, req.Response).
		IsOK().
		IsJSON().
		ContainsJSON(&expected)
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL