github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/testify/assert/http_assertions.go (about) 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+"?"+values.Encode(), nil) 16 if err != nil { 17 return -1, err 18 } 19 handler(w, req) 20 return w.Code, nil 21 } 22 23 // HTTPSuccess asserts that a specified handler returns a success status code. 24 // 25 // assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) 26 // 27 // Returns whether the assertion was successful (true) or not (false). 28 func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 29 code, err := httpCode(handler, method, url, values) 30 if err != nil { 31 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 32 return false 33 } 34 35 isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent 36 if !isSuccessCode { 37 Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code)) 38 } 39 40 return isSuccessCode 41 } 42 43 // HTTPRedirect asserts that a specified handler returns a redirect status code. 44 // 45 // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 46 // 47 // Returns whether the assertion was successful (true) or not (false). 48 func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 49 code, err := httpCode(handler, method, url, values) 50 if err != nil { 51 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 52 return false 53 } 54 55 isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect 56 if !isRedirectCode { 57 Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code)) 58 } 59 60 return isRedirectCode 61 } 62 63 // HTTPError asserts that a specified handler returns an error status code. 64 // 65 // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 66 // 67 // Returns whether the assertion was successful (true) or not (false). 68 func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 69 code, err := httpCode(handler, method, url, values) 70 if err != nil { 71 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 72 return false 73 } 74 75 isErrorCode := code >= http.StatusBadRequest 76 if !isErrorCode { 77 Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code)) 78 } 79 80 return isErrorCode 81 } 82 83 // HTTPBody is a helper that returns HTTP body of the response. It returns 84 // empty string if building a new request fails. 85 func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string { 86 w := httptest.NewRecorder() 87 req, err := http.NewRequest(method, url+"?"+values.Encode(), nil) 88 if err != nil { 89 return "" 90 } 91 handler(w, req) 92 return w.Body.String() 93 } 94 95 // HTTPBodyContains asserts that a specified handler returns a 96 // body that contains a string. 97 // 98 // assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky") 99 // 100 // Returns whether the assertion was successful (true) or not (false). 101 func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 102 body := HTTPBody(handler, method, url, values) 103 104 contains := strings.Contains(body, fmt.Sprint(str)) 105 if !contains { 106 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) 107 } 108 109 return contains 110 } 111 112 // HTTPBodyNotContains asserts that a specified handler returns a 113 // body that does not contain a string. 114 // 115 // assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky") 116 // 117 // Returns whether the assertion was successful (true) or not (false). 118 func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 119 body := HTTPBody(handler, method, url, values) 120 121 contains := strings.Contains(body, fmt.Sprint(str)) 122 if contains { 123 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) 124 } 125 126 return !contains 127 }