http_assertions.go 4.7 KB

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