http_assertions_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package assert
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. )
  8. func httpOK(w http.ResponseWriter, r *http.Request) {
  9. w.WriteHeader(http.StatusOK)
  10. }
  11. func httpRedirect(w http.ResponseWriter, r *http.Request) {
  12. w.WriteHeader(http.StatusTemporaryRedirect)
  13. }
  14. func httpError(w http.ResponseWriter, r *http.Request) {
  15. w.WriteHeader(http.StatusInternalServerError)
  16. }
  17. func TestHTTPStatuses(t *testing.T) {
  18. assert := New(t)
  19. mockT := new(testing.T)
  20. assert.Equal(HTTPSuccess(mockT, httpOK, "GET", "/", nil), true)
  21. assert.Equal(HTTPSuccess(mockT, httpRedirect, "GET", "/", nil), false)
  22. assert.Equal(HTTPSuccess(mockT, httpError, "GET", "/", nil), false)
  23. assert.Equal(HTTPRedirect(mockT, httpOK, "GET", "/", nil), false)
  24. assert.Equal(HTTPRedirect(mockT, httpRedirect, "GET", "/", nil), true)
  25. assert.Equal(HTTPRedirect(mockT, httpError, "GET", "/", nil), false)
  26. assert.Equal(HTTPError(mockT, httpOK, "GET", "/", nil), false)
  27. assert.Equal(HTTPError(mockT, httpRedirect, "GET", "/", nil), false)
  28. assert.Equal(HTTPError(mockT, httpError, "GET", "/", nil), true)
  29. }
  30. func TestHTTPStatusesWrapper(t *testing.T) {
  31. assert := New(t)
  32. mockAssert := New(new(testing.T))
  33. assert.Equal(mockAssert.HTTPSuccess(httpOK, "GET", "/", nil), true)
  34. assert.Equal(mockAssert.HTTPSuccess(httpRedirect, "GET", "/", nil), false)
  35. assert.Equal(mockAssert.HTTPSuccess(httpError, "GET", "/", nil), false)
  36. assert.Equal(mockAssert.HTTPRedirect(httpOK, "GET", "/", nil), false)
  37. assert.Equal(mockAssert.HTTPRedirect(httpRedirect, "GET", "/", nil), true)
  38. assert.Equal(mockAssert.HTTPRedirect(httpError, "GET", "/", nil), false)
  39. assert.Equal(mockAssert.HTTPError(httpOK, "GET", "/", nil), false)
  40. assert.Equal(mockAssert.HTTPError(httpRedirect, "GET", "/", nil), false)
  41. assert.Equal(mockAssert.HTTPError(httpError, "GET", "/", nil), true)
  42. }
  43. func httpHelloName(w http.ResponseWriter, r *http.Request) {
  44. name := r.FormValue("name")
  45. w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
  46. }
  47. func TestHttpBody(t *testing.T) {
  48. assert := New(t)
  49. mockT := new(testing.T)
  50. assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  51. assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  52. assert.False(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  53. assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  54. assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  55. assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  56. }
  57. func TestHttpBodyWrappers(t *testing.T) {
  58. assert := New(t)
  59. mockAssert := New(new(testing.T))
  60. assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  61. assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  62. assert.False(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  63. assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  64. assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  65. assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  66. }