Skip to content

Instantly share code, notes, and snippets.

@lateefj
Last active August 29, 2015 14:21
Show Gist options
  • Save lateefj/f70f27b1824fd64b4cc6 to your computer and use it in GitHub Desktop.
Save lateefj/f70f27b1824fd64b4cc6 to your computer and use it in GitHub Desktop.
Basic McTest Usage
req, _ := http.NewRequest("GET", "/path/to/handler", nil)
resp := NewMockTestResponse(t)
HomeHandler(resp, req)
b := "HomeHandler"
if !resp.AssertCode(http.StatusOK) {
t.Fatalf("Response StatusCode is %d asserted that it is %d", resp.StatusCode, http.StatusOK)
}
if !resp.AssertBody(b) {
t.Fatalf("Response body is %s asserted that it is %s", resp.String(), b)
}
req, _ := http.NewRequest("GET", "/path/to/handler", nil)
resp := NewMockTestResponse(t)
HomeHandler(resp, req)
b := "HomeHandler"
if !resp.AssertCode(http.StatusOK) {
t.Fatalf("Response StatusCode is %d asserted that it is %d", resp.StatusCode, http.StatusOK)
}
p := payload{X: "bar", Y: "foo"}
inst := &payload{}
if !resp.AssertJson(inst, p) {
t.Fatalf("Response body is %s asserted that it is %v", resp.String(), p)
}
req, _ := http.NewRequest("GET", "/path/to/handler", nil)
// Create mock
resp := NewMockTestResponse(t)
testAuthWrapper(testAuthHandler)(resp, req)
if !resp.AssertCode(http.StatusUnauthorized) {
t.Fatalf("Response StatusCode is %d asserted that it is %d", resp.StatusCode, http.StatusUnauthorized)
}
if !resp.AssertBody(failedAuth) {
t.Fatalf("Response body is %s asserted that it is %s", resp.String(), failedAuth)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment