Documentation ¶
Overview ¶
Package assert implements assertions to be run on HTTP response
Index ¶
- func CompactJSON(jsonStr string, t *testing.T) string
- func CompactJSONb(jsonBytes []byte, t *testing.T) string
- func IndentJSON(jsonStr string, t *testing.T) string
- func IndentJSONb(jsonBytes []byte, t *testing.T) string
- func SetDiff(d Differ)
- type Assert
- func (a *Assert) Body(test func(t *testing.T, responseBody []byte)) *Assert
- func (a *Assert) ContentType(contentType string) *Assert
- func (a *Assert) Custom(customTest func(t *testing.T, response *http.Response)) *Assert
- func (a *Assert) Header(key string, value string) *Assert
- func (a *Assert) HeaderMissing(key string) *Assert
- func (a *Assert) JSON(expectedContent string) *Assert
- func (a *Assert) JSONMatches(test interface{}) *Assert
- func (a *Assert) JSONUnmarshallsTo(obj interface{}) *Assert
- func (a *Assert) Response(customTest func(t *testing.T, response *http.Response)) *Assert
- func (a *Assert) Status(statusCode int) *Assert
- func (a *Assert) TestRun() func(*testing.T)
- type Differ
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompactJSON ¶
CompactJSON returns JSON compacted to one line
func CompactJSONb ¶
CompactJSONb returns JSON compacted to one line
func IndentJSON ¶
IndentJSON pretty-prints JSON
func IndentJSONb ¶
IndentJSONb pretty-prints JSON
func SetDiff ¶
func SetDiff(d Differ)
TODO research for diffing https://godoc.org/gotest.tools/assert#Equal
Types ¶
type Assert ¶
Assert Specifies how the http.Response should look like by chaining assertions on it. Assertions are evaluated immediately.
func (*Assert) ContentType ¶
ContentType Assert that response is of specific `Content-Type`
func (*Assert) HeaderMissing ¶
HeaderMissing Assert that specific header is not set.
func (*Assert) JSON ¶
JSON Assert that the response equals to given JSON. Indentation here doesn't play a role.
To show a diff between expected and actual values set `HANDLERTEST_DIFF=true` env variable
func (*Assert) JSONMatches ¶
JSONMatches Asserts that JSON response unmarshalls to object given as 2nd argument in the function further asserting conditions on the returned objects.
In case it's hard to predict your whole response, or you don't want to test the whole response, you might use this function to get an unmarshalled response body and test for specific values.
Example ¶
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 } // TODO mock t in #12 to test for returned value 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
func (*Assert) JSONUnmarshallsTo ¶
JSONUnmarshallsTo Assert that JSON response unmarshalls to given object / list
func (*Assert) Response ¶ added in v1.1.0
Response Use it to implement custom assertions you might need that is not covered by this lib use below function. Please also file an issue to support your case if it is general enough.
func (*Assert) Status ¶
Status Assert that response has specific HTTP Status Code
Example (Multiple) ¶
package main import ( "fmt" "net/http" "strconv" "testing" "gitlab.com/gotests/handlertest" ) func main() { AHandler := func(w http.ResponseWriter, r *http.Request) { statusStr := r.Header.Get("X-Return") status, err := strconv.Atoi(statusStr) var response string if err != nil { w.WriteHeader(http.StatusBadRequest) response = `{"code": "WRONG_INPUT"}` } else { w.WriteHeader(status) response = `{"id": 1}` } _, err = w.Write([]byte(response)) if err != nil { fmt.Print(err) } } type successfulResponse struct { ID int `json:"id"` } type errorResponse struct { Code string `json:"code"` } t := new(testing.T) type expected struct { status int jsonMatches interface{} } tests := []struct { name string status string expected expected }{{ name: "Status 200", status: "200", expected: expected{ status: 200, jsonMatches: func(t *testing.T, ret successfulResponse) { if ret.ID != 1 { t.Error("Expected id=1") } }, }, }, { name: "Error", status: "awfa", expected: expected{ status: 400, jsonMatches: func(t *testing.T, ret errorResponse) { if ret.Code != "WRONG_INPUT_DATA" { t.Errorf("Expected WRONG_INPUT_DATA, got %s", ret.Code) fmt.Printf("Expected WRONG_INPUT_DATA, got %s", ret.Code) } }, }, }, } for _, tt := range tests { // for somereason example don't like t.Run //t.Run(tt.name, func(t *testing.T) { handlertest.Call(AHandler).Header("X-Return", tt.status). Assert(t). Status(tt.expected.status). JSONMatches(tt.expected.jsonMatches) //}) } }
Output: Expected WRONG_INPUT_DATA, got WRONG_INPUT