David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 1 | package assert |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | // httpCode is a helper that returns HTTP code of the response. It returns -1 and |
| 12 | // an error if building a new request fails. |
| 13 | func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) { |
| 14 | w := httptest.NewRecorder() |
| 15 | req, err := http.NewRequest(method, url, nil) |
| 16 | if err != nil { |
| 17 | return -1, err |
| 18 | } |
| 19 | req.URL.RawQuery = values.Encode() |
| 20 | handler(w, req) |
| 21 | return w.Code, nil |
| 22 | } |
| 23 | |
| 24 | // HTTPSuccess asserts that a specified handler returns a success status code. |
| 25 | // |
| 26 | // assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) |
| 27 | // |
| 28 | // Returns whether the assertion was successful (true) or not (false). |
| 29 | func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { |
| 30 | if h, ok := t.(tHelper); ok { |
| 31 | h.Helper() |
| 32 | } |
| 33 | code, err := httpCode(handler, method, url, values) |
| 34 | if err != nil { |
| 35 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) |
| 36 | return false |
| 37 | } |
| 38 | |
| 39 | isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent |
| 40 | if !isSuccessCode { |
| 41 | Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code)) |
| 42 | } |
| 43 | |
| 44 | return isSuccessCode |
| 45 | } |
| 46 | |
| 47 | // HTTPRedirect asserts that a specified handler returns a redirect status code. |
| 48 | // |
| 49 | // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} |
| 50 | // |
| 51 | // Returns whether the assertion was successful (true) or not (false). |
| 52 | func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { |
| 53 | if h, ok := t.(tHelper); ok { |
| 54 | h.Helper() |
| 55 | } |
| 56 | code, err := httpCode(handler, method, url, values) |
| 57 | if err != nil { |
| 58 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) |
| 59 | return false |
| 60 | } |
| 61 | |
| 62 | isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect |
| 63 | if !isRedirectCode { |
| 64 | Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code)) |
| 65 | } |
| 66 | |
| 67 | return isRedirectCode |
| 68 | } |
| 69 | |
| 70 | // HTTPError asserts that a specified handler returns an error status code. |
| 71 | // |
| 72 | // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} |
| 73 | // |
| 74 | // Returns whether the assertion was successful (true) or not (false). |
| 75 | func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { |
| 76 | if h, ok := t.(tHelper); ok { |
| 77 | h.Helper() |
| 78 | } |
| 79 | code, err := httpCode(handler, method, url, values) |
| 80 | if err != nil { |
| 81 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | isErrorCode := code >= http.StatusBadRequest |
| 86 | if !isErrorCode { |
| 87 | Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code)) |
| 88 | } |
| 89 | |
| 90 | return isErrorCode |
| 91 | } |
| 92 | |
| 93 | // HTTPBody is a helper that returns HTTP body of the response. It returns |
| 94 | // empty string if building a new request fails. |
| 95 | func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string { |
| 96 | w := httptest.NewRecorder() |
| 97 | req, err := http.NewRequest(method, url+"?"+values.Encode(), nil) |
| 98 | if err != nil { |
| 99 | return "" |
| 100 | } |
| 101 | handler(w, req) |
| 102 | return w.Body.String() |
| 103 | } |
| 104 | |
| 105 | // HTTPBodyContains asserts that a specified handler returns a |
| 106 | // body that contains a string. |
| 107 | // |
| 108 | // assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") |
| 109 | // |
| 110 | // Returns whether the assertion was successful (true) or not (false). |
| 111 | func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { |
| 112 | if h, ok := t.(tHelper); ok { |
| 113 | h.Helper() |
| 114 | } |
| 115 | body := HTTPBody(handler, method, url, values) |
| 116 | |
| 117 | contains := strings.Contains(body, fmt.Sprint(str)) |
| 118 | if !contains { |
| 119 | Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) |
| 120 | } |
| 121 | |
| 122 | return contains |
| 123 | } |
| 124 | |
| 125 | // HTTPBodyNotContains asserts that a specified handler returns a |
| 126 | // body that does not contain a string. |
| 127 | // |
| 128 | // assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") |
| 129 | // |
| 130 | // Returns whether the assertion was successful (true) or not (false). |
| 131 | func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { |
| 132 | if h, ok := t.(tHelper); ok { |
| 133 | h.Helper() |
| 134 | } |
| 135 | body := HTTPBody(handler, method, url, values) |
| 136 | |
| 137 | contains := strings.Contains(body, fmt.Sprint(str)) |
| 138 | if contains { |
| 139 | Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) |
| 140 | } |
| 141 | |
| 142 | return !contains |
| 143 | } |