http_assertions.go 4.4 KB

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