http_assertions.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  10. // if building a new request fails.
  11. func httpCode(handler http.HandlerFunc, method, url string, values url.Values) int {
  12. w := httptest.NewRecorder()
  13. req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
  14. if err != nil {
  15. return -1
  16. }
  17. handler(w, req)
  18. return w.Code
  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) bool {
  26. code := httpCode(handler, method, url, values)
  27. if code == -1 {
  28. return false
  29. }
  30. return code >= http.StatusOK && code <= http.StatusPartialContent
  31. }
  32. // HTTPRedirect asserts that a specified handler returns a redirect status code.
  33. //
  34. // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
  35. //
  36. // Returns whether the assertion was successful (true) or not (false).
  37. func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
  38. code := httpCode(handler, method, url, values)
  39. if code == -1 {
  40. return false
  41. }
  42. return code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
  43. }
  44. // HTTPError asserts that a specified handler returns an error status code.
  45. //
  46. // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
  47. //
  48. // Returns whether the assertion was successful (true) or not (false).
  49. func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
  50. code := httpCode(handler, method, url, values)
  51. if code == -1 {
  52. return false
  53. }
  54. return code >= http.StatusBadRequest
  55. }
  56. // HTTPBody is a helper that returns HTTP body of the response. It returns
  57. // empty string if building a new request fails.
  58. func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
  59. w := httptest.NewRecorder()
  60. req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
  61. if err != nil {
  62. return ""
  63. }
  64. handler(w, req)
  65. return w.Body.String()
  66. }
  67. // HTTPBodyContains asserts that a specified handler returns a
  68. // body that contains a string.
  69. //
  70. // assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
  71. //
  72. // Returns whether the assertion was successful (true) or not (false).
  73. func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
  74. body := HTTPBody(handler, method, url, values)
  75. contains := strings.Contains(body, fmt.Sprint(str))
  76. if !contains {
  77. Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
  78. }
  79. return contains
  80. }
  81. // HTTPBodyNotContains asserts that a specified handler returns a
  82. // body that does not contain a string.
  83. //
  84. // assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
  85. //
  86. // Returns whether the assertion was successful (true) or not (false).
  87. func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
  88. body := HTTPBody(handler, method, url, values)
  89. contains := strings.Contains(body, fmt.Sprint(str))
  90. if contains {
  91. Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
  92. }
  93. return !contains
  94. }