PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/stretchr/testify/assert/http_assertions.go

https://gitlab.com/github-cloud-corporation/bootkube
Go | 157 lines | 78 code | 18 blank | 61 comment | 14 complexity | 4f0e84d0c6bc4cc1b32b056db5b7198b MD5 | raw file
  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, "Expected response body for %s to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)
  92. }
  93. return !contains
  94. }
  95. //
  96. // Assertions Wrappers
  97. //
  98. // HTTPSuccess asserts that a specified handler returns a success status code.
  99. //
  100. // assert.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil)
  101. //
  102. // Returns whether the assertion was successful (true) or not (false).
  103. func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method, url string, values url.Values) bool {
  104. return HTTPSuccess(a.t, handler, method, url, values)
  105. }
  106. // HTTPRedirect asserts that a specified handler returns a redirect status code.
  107. //
  108. // assert.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
  109. //
  110. // Returns whether the assertion was successful (true) or not (false).
  111. func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method, url string, values url.Values) bool {
  112. return HTTPRedirect(a.t, handler, method, url, values)
  113. }
  114. // HTTPError asserts that a specified handler returns an error status code.
  115. //
  116. // assert.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
  117. //
  118. // Returns whether the assertion was successful (true) or not (false).
  119. func (a *Assertions) HTTPError(handler http.HandlerFunc, method, url string, values url.Values) bool {
  120. return HTTPError(a.t, handler, method, url, values)
  121. }
  122. // HTTPBodyContains asserts that a specified handler returns a
  123. // body that contains a string.
  124. //
  125. // assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
  126. //
  127. // Returns whether the assertion was successful (true) or not (false).
  128. func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
  129. return HTTPBodyContains(a.t, handler, method, url, values, str)
  130. }
  131. // HTTPBodyNotContains asserts that a specified handler returns a
  132. // body that does not contain a string.
  133. //
  134. // assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
  135. //
  136. // Returns whether the assertion was successful (true) or not (false).
  137. func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
  138. return HTTPBodyNotContains(a.t, handler, method, url, values, str)
  139. }