You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.8 KiB

  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 TestHTTPSuccess(t *testing.T) {
  18. assert := New(t)
  19. mockT1 := new(testing.T)
  20. assert.Equal(HTTPSuccess(mockT1, httpOK, "GET", "/", nil), true)
  21. assert.False(mockT1.Failed())
  22. mockT2 := new(testing.T)
  23. assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
  24. assert.True(mockT2.Failed())
  25. mockT3 := new(testing.T)
  26. assert.Equal(HTTPSuccess(mockT3, httpError, "GET", "/", nil), false)
  27. assert.True(mockT3.Failed())
  28. }
  29. func TestHTTPRedirect(t *testing.T) {
  30. assert := New(t)
  31. mockT1 := new(testing.T)
  32. assert.Equal(HTTPRedirect(mockT1, httpOK, "GET", "/", nil), false)
  33. assert.True(mockT1.Failed())
  34. mockT2 := new(testing.T)
  35. assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
  36. assert.False(mockT2.Failed())
  37. mockT3 := new(testing.T)
  38. assert.Equal(HTTPRedirect(mockT3, httpError, "GET", "/", nil), false)
  39. assert.True(mockT3.Failed())
  40. }
  41. func TestHTTPError(t *testing.T) {
  42. assert := New(t)
  43. mockT1 := new(testing.T)
  44. assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
  45. assert.True(mockT1.Failed())
  46. mockT2 := new(testing.T)
  47. assert.Equal(HTTPError(mockT2, httpRedirect, "GET", "/", nil), false)
  48. assert.True(mockT2.Failed())
  49. mockT3 := new(testing.T)
  50. assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
  51. assert.False(mockT3.Failed())
  52. }
  53. func TestHTTPStatusesWrapper(t *testing.T) {
  54. assert := New(t)
  55. mockAssert := New(new(testing.T))
  56. assert.Equal(mockAssert.HTTPSuccess(httpOK, "GET", "/", nil), true)
  57. assert.Equal(mockAssert.HTTPSuccess(httpRedirect, "GET", "/", nil), false)
  58. assert.Equal(mockAssert.HTTPSuccess(httpError, "GET", "/", nil), false)
  59. assert.Equal(mockAssert.HTTPRedirect(httpOK, "GET", "/", nil), false)
  60. assert.Equal(mockAssert.HTTPRedirect(httpRedirect, "GET", "/", nil), true)
  61. assert.Equal(mockAssert.HTTPRedirect(httpError, "GET", "/", nil), false)
  62. assert.Equal(mockAssert.HTTPError(httpOK, "GET", "/", nil), false)
  63. assert.Equal(mockAssert.HTTPError(httpRedirect, "GET", "/", nil), false)
  64. assert.Equal(mockAssert.HTTPError(httpError, "GET", "/", nil), true)
  65. }
  66. func httpHelloName(w http.ResponseWriter, r *http.Request) {
  67. name := r.FormValue("name")
  68. w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
  69. }
  70. func TestHTTPRequestWithNoParams(t *testing.T) {
  71. var got *http.Request
  72. handler := func(w http.ResponseWriter, r *http.Request) {
  73. got = r
  74. w.WriteHeader(http.StatusOK)
  75. }
  76. True(t, HTTPSuccess(t, handler, "GET", "/url", nil))
  77. Empty(t, got.URL.Query())
  78. Equal(t, "/url", got.URL.RequestURI())
  79. }
  80. func TestHTTPRequestWithParams(t *testing.T) {
  81. var got *http.Request
  82. handler := func(w http.ResponseWriter, r *http.Request) {
  83. got = r
  84. w.WriteHeader(http.StatusOK)
  85. }
  86. params := url.Values{}
  87. params.Add("id", "12345")
  88. True(t, HTTPSuccess(t, handler, "GET", "/url", params))
  89. Equal(t, url.Values{"id": []string{"12345"}}, got.URL.Query())
  90. Equal(t, "/url?id=12345", got.URL.String())
  91. Equal(t, "/url?id=12345", got.URL.RequestURI())
  92. }
  93. func TestHttpBody(t *testing.T) {
  94. assert := New(t)
  95. mockT := new(testing.T)
  96. assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  97. assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  98. assert.False(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  99. assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  100. assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  101. assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  102. }
  103. func TestHttpBodyWrappers(t *testing.T) {
  104. assert := New(t)
  105. mockAssert := New(new(testing.T))
  106. assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  107. assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  108. assert.False(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  109. assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
  110. assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
  111. assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
  112. }