html_test.go 6.7 KB

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