Documentation ¶
Overview ¶
Package handlertest is a toolkit for testing http handlers in a verbose way.
See assert subpackage handling assertions on http.Response https://godoc.org/gitlab.com/gotests/handlertest/assert
Example (TestListFilter) ¶
package main import ( "fmt" "net/http" "testing" "gitlab.com/gotests/handlertest" ) func main() { ProductListControllerThatLikesCategoryB := func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`[{"category":"b"}]`)) } type Product struct { Category string } t := new(testing.T) // create your request handlertest.Call(ProductListControllerThatLikesCategoryB).GET("/products?category=a"). // then assert your expectations Assert(t). Status(http.StatusOK). JSONMatches(func(t *testing.T, products []Product) { // unmarshalling of JSON objects is done for you if len(products) == 0 { t.Errorf("Expected to have some products returned") } for _, p := range products { if p.Category != "a" { t.Errorf("Expected filter to return products only of category %s, but got %s", "a", p.Category) fmt.Printf("Expected filter to return products only of category %s, but got %s", "a", p.Category) } } }) }
Output: Expected filter to return products only of category a, but got b
Example (TestUploadAttachments) ¶
package main import ( "fmt" "net/http" "testing" "gitlab.com/gotests/handlertest" ) func main() { UploadAttachmentsController := func(w http.ResponseWriter, r *http.Request) { _ = r.ParseMultipartForm(1 << 10) fmt.Printf("post_id: %s\n", r.PostForm.Get("post_id")) fmt.Println(r.MultipartForm.File["files[]"][0].Filename) } t := new(testing.T) // create request handlertest.Call(UploadAttachmentsController). POST("/attachments"). FormMultipartMap(map[string]string{ "post_id": "1", }). File("files[]", "img1.jpg", "contents"). // then assert your expectations Assert(t). Status(http.StatusCreated). ContentType("text/html") }
Output: post_id: 1 img1.jpg
Index ¶
- Constants
- func ValuesFromMap(values map[string]string) url.Values
- type Request
- func (r *Request) Assert(t *testing.T) *assert.Assert
- func (r *Request) ContentType(contentType string) *Request
- func (r *Request) Custom(customize func(request *http.Request)) *Request
- func (r *Request) File(field string, fileName string, content string) *Request
- func (r *Request) FileReader(field string, fileName string, content io.Reader) *Request
- func (r *Request) FileReaders(fields map[string]map[string]io.Reader) *Request
- func (r *Request) Files(fields map[string]map[string]string) *Request
- func (r *Request) FormMultipart(fields url.Values) *Request
- func (r *Request) FormMultipartMap(values map[string]string) *Request
- func (r *Request) FormURLEncoded(values url.Values) *Request
- func (r *Request) FormURLEncodedMap(values map[string]string) *Request
- func (r *Request) GET(url string) *Request
- func (r *Request) Header(key string, value string) *Request
- func (r *Request) JSON(json string) *Request
- func (r *Request) Method(method string) *Request
- func (r *Request) POST(url string) *Request
- func (r *Request) Request(customize func(request *http.Request) *http.Request) *Request
- func (r *Request) URL(url string) *Request
Examples ¶
Constants ¶
const ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
ContentTypeFormURLEncoded application/x-www-form-urlencoded
const ContentTypeJSON = "application/json"
ContentTypeJSON application/json
const ContentTypeMultipartFormDataPrefix = "multipart/form-data;"
ContentTypeMultipartFormDataPrefix multipart/form-data;
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request follows builder pattern to set how the request should look like.
func Call ¶
func Call(handler http.HandlerFunc) *Request
Call your handler. This creates Request builder on which you can set how the request should look like by chaining methods.
func (*Request) Assert ¶
Assert Return object to specify how the http.Response should look like by chaining assertions.
func (*Request) ContentType ¶
ContentType Shorthand for setting `Content-Type`
func (*Request) File ¶
File Sets a file on a given field with text contents Content-Type will be `multipart/form-data`
func (*Request) FileReader ¶
FileReader Sets a file on a given field with contents from io.Reader Content-Type will be `multipart/form-data`
Example ¶
package main import ( "fmt" "net/http" "os" "path/filepath" "testing" "gitlab.com/gotests/handlertest" ) func main() { YourHandler := func(w http.ResponseWriter, r *http.Request) { _ = r.ParseMultipartForm(1 << 10) fmt.Println(r.MultipartForm.File["files[]"][0].Filename) } t := new(testing.T) r, _ := os.Open(filepath.Join("testdata", "1.txt")) handlertest.Call(YourHandler).FileReader("files[]", "1.txt", r).Assert(t) }
Output: 1.txt
func (*Request) FileReaders ¶
FileReaders Sets several files with contents from io.Reader Content-Type will be `multipart/form-data`
func (*Request) Files ¶
Files Sets several files with text contents Content-Type will be `multipart/form-data`
func (*Request) FormMultipart ¶
FormMultipart Encodes form values request's body Content-Type will be `multipart/form-data`
func (*Request) FormMultipartMap ¶
FormMultipartMap Encodes form values request's body Content-Type will be `multipart/form-data`
func (*Request) FormURLEncoded ¶
FormURLEncoded Encodes form values in request's body. Content-Type will be `application/x-www-form-urlencoded`
func (*Request) FormURLEncodedMap ¶
FormURLEncodedMap Encodes form values in request's body. Using simple map in case your fields don't have multiple values. Content-Type will be `application/x-www-form-urlencoded`
func (*Request) JSON ¶
JSON Sets given json as body and adds `Content-Type: application/json` header