html_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package html // import "miniflux.app/http/response/html"
  5. import (
  6. "errors"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. )
  11. func TestOKResponse(t *testing.T) {
  12. r, err := http.NewRequest("GET", "/", nil)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. w := httptest.NewRecorder()
  17. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. OK(w, r, "Some HTML")
  19. })
  20. handler.ServeHTTP(w, r)
  21. resp := w.Result()
  22. expectedStatusCode := http.StatusOK
  23. if resp.StatusCode != expectedStatusCode {
  24. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  25. }
  26. expectedBody := `Some HTML`
  27. actualBody := w.Body.String()
  28. if actualBody != expectedBody {
  29. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  30. }
  31. headers := map[string]string{
  32. "Content-Type": "text/html; charset=utf-8",
  33. "Cache-Control": "no-cache, max-age=0, must-revalidate, no-store",
  34. }
  35. for header, expected := range headers {
  36. actual := resp.Header.Get(header)
  37. if actual != expected {
  38. t.Fatalf(`Unexpected header value, got %q instead of %q`, actual, expected)
  39. }
  40. }
  41. }
  42. func TestServerErrorResponse(t *testing.T) {
  43. r, err := http.NewRequest("GET", "/", nil)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. w := httptest.NewRecorder()
  48. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  49. ServerError(w, r, errors.New("Some error"))
  50. })
  51. handler.ServeHTTP(w, r)
  52. resp := w.Result()
  53. expectedStatusCode := http.StatusInternalServerError
  54. if resp.StatusCode != expectedStatusCode {
  55. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  56. }
  57. expectedBody := `Some error`
  58. actualBody := w.Body.String()
  59. if actualBody != expectedBody {
  60. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  61. }
  62. expectedContentType := "text/html; charset=utf-8"
  63. actualContentType := resp.Header.Get("Content-Type")
  64. if actualContentType != expectedContentType {
  65. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  66. }
  67. }
  68. func TestBadRequestResponse(t *testing.T) {
  69. r, err := http.NewRequest("GET", "/", nil)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. w := httptest.NewRecorder()
  74. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  75. BadRequest(w, r, errors.New("Some error"))
  76. })
  77. handler.ServeHTTP(w, r)
  78. resp := w.Result()
  79. expectedStatusCode := http.StatusBadRequest
  80. if resp.StatusCode != expectedStatusCode {
  81. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  82. }
  83. expectedBody := `Some error`
  84. actualBody := w.Body.String()
  85. if actualBody != expectedBody {
  86. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  87. }
  88. expectedContentType := "text/html; charset=utf-8"
  89. actualContentType := resp.Header.Get("Content-Type")
  90. if actualContentType != expectedContentType {
  91. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  92. }
  93. }
  94. func TestForbiddenResponse(t *testing.T) {
  95. r, err := http.NewRequest("GET", "/", nil)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. w := httptest.NewRecorder()
  100. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  101. Forbidden(w, r)
  102. })
  103. handler.ServeHTTP(w, r)
  104. resp := w.Result()
  105. expectedStatusCode := http.StatusForbidden
  106. if resp.StatusCode != expectedStatusCode {
  107. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  108. }
  109. expectedBody := `Access Forbidden`
  110. actualBody := w.Body.String()
  111. if actualBody != expectedBody {
  112. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  113. }
  114. expectedContentType := "text/html; charset=utf-8"
  115. actualContentType := resp.Header.Get("Content-Type")
  116. if actualContentType != expectedContentType {
  117. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  118. }
  119. }
  120. func TestNotFoundResponse(t *testing.T) {
  121. r, err := http.NewRequest("GET", "/", nil)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. w := httptest.NewRecorder()
  126. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  127. NotFound(w, r)
  128. })
  129. handler.ServeHTTP(w, r)
  130. resp := w.Result()
  131. expectedStatusCode := http.StatusNotFound
  132. if resp.StatusCode != expectedStatusCode {
  133. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  134. }
  135. expectedBody := `Page Not Found`
  136. actualBody := w.Body.String()
  137. if actualBody != expectedBody {
  138. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  139. }
  140. expectedContentType := "text/html; charset=utf-8"
  141. actualContentType := resp.Header.Get("Content-Type")
  142. if actualContentType != expectedContentType {
  143. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  144. }
  145. }
  146. func TestRedirectResponse(t *testing.T) {
  147. r, err := http.NewRequest("GET", "/", nil)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. w := httptest.NewRecorder()
  152. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  153. Redirect(w, r, "/path")
  154. })
  155. handler.ServeHTTP(w, r)
  156. resp := w.Result()
  157. defer resp.Body.Close()
  158. expectedStatusCode := http.StatusFound
  159. if resp.StatusCode != expectedStatusCode {
  160. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  161. }
  162. expectedResult := "/path"
  163. actualResult := resp.Header.Get("Location")
  164. if actualResult != expectedResult {
  165. t.Fatalf(`Unexpected redirect location, got %q instead of %q`, actualResult, expectedResult)
  166. }
  167. }