json_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package json // import "miniflux.app/v2/internal/http/response/json"
  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, map[string]string{"key": "value"})
  18. })
  19. handler.ServeHTTP(w, r)
  20. resp := w.Result()
  21. defer resp.Body.Close()
  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 := `{"key":"value"}`
  27. actualBody := w.Body.String()
  28. if actualBody != expectedBody {
  29. t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
  30. }
  31. expectedContentType := contentTypeHeader
  32. actualContentType := resp.Header.Get("Content-Type")
  33. if actualContentType != expectedContentType {
  34. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  35. }
  36. }
  37. func TestCreatedResponse(t *testing.T) {
  38. r, err := http.NewRequest("GET", "/", nil)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. w := httptest.NewRecorder()
  43. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  44. Created(w, r, map[string]string{"key": "value"})
  45. })
  46. handler.ServeHTTP(w, r)
  47. resp := w.Result()
  48. expectedStatusCode := http.StatusCreated
  49. if resp.StatusCode != expectedStatusCode {
  50. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  51. }
  52. expectedBody := `{"key":"value"}`
  53. actualBody := w.Body.String()
  54. if actualBody != expectedBody {
  55. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  56. }
  57. expectedContentType := contentTypeHeader
  58. actualContentType := resp.Header.Get("Content-Type")
  59. if actualContentType != expectedContentType {
  60. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  61. }
  62. }
  63. func TestNoContentResponse(t *testing.T) {
  64. r, err := http.NewRequest("GET", "/", nil)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. w := httptest.NewRecorder()
  69. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  70. NoContent(w, r)
  71. })
  72. handler.ServeHTTP(w, r)
  73. resp := w.Result()
  74. expectedStatusCode := http.StatusNoContent
  75. if resp.StatusCode != expectedStatusCode {
  76. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  77. }
  78. expectedBody := ``
  79. actualBody := w.Body.String()
  80. if actualBody != expectedBody {
  81. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  82. }
  83. expectedContentType := contentTypeHeader
  84. actualContentType := resp.Header.Get("Content-Type")
  85. if actualContentType != expectedContentType {
  86. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  87. }
  88. }
  89. func TestServerErrorResponse(t *testing.T) {
  90. r, err := http.NewRequest("GET", "/", nil)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. w := httptest.NewRecorder()
  95. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  96. ServerError(w, r, errors.New("some error"))
  97. })
  98. handler.ServeHTTP(w, r)
  99. resp := w.Result()
  100. defer resp.Body.Close()
  101. expectedStatusCode := http.StatusInternalServerError
  102. if resp.StatusCode != expectedStatusCode {
  103. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  104. }
  105. expectedBody := `{"error_message":"some error"}`
  106. actualBody := w.Body.String()
  107. if actualBody != expectedBody {
  108. t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
  109. }
  110. expectedContentType := contentTypeHeader
  111. actualContentType := resp.Header.Get("Content-Type")
  112. if actualContentType != expectedContentType {
  113. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  114. }
  115. }
  116. func TestBadRequestResponse(t *testing.T) {
  117. r, err := http.NewRequest("GET", "/", nil)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. w := httptest.NewRecorder()
  122. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  123. BadRequest(w, r, errors.New("Some Error"))
  124. })
  125. handler.ServeHTTP(w, r)
  126. resp := w.Result()
  127. expectedStatusCode := http.StatusBadRequest
  128. if resp.StatusCode != expectedStatusCode {
  129. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  130. }
  131. expectedBody := `{"error_message":"Some Error"}`
  132. actualBody := w.Body.String()
  133. if actualBody != expectedBody {
  134. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  135. }
  136. expectedContentType := contentTypeHeader
  137. actualContentType := resp.Header.Get("Content-Type")
  138. if actualContentType != expectedContentType {
  139. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  140. }
  141. }
  142. func TestUnauthorizedResponse(t *testing.T) {
  143. r, err := http.NewRequest("GET", "/", nil)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. w := httptest.NewRecorder()
  148. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  149. Unauthorized(w, r)
  150. })
  151. handler.ServeHTTP(w, r)
  152. resp := w.Result()
  153. expectedStatusCode := http.StatusUnauthorized
  154. if resp.StatusCode != expectedStatusCode {
  155. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  156. }
  157. expectedBody := `{"error_message":"access unauthorized"}`
  158. actualBody := w.Body.String()
  159. if actualBody != expectedBody {
  160. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  161. }
  162. expectedContentType := contentTypeHeader
  163. actualContentType := resp.Header.Get("Content-Type")
  164. if actualContentType != expectedContentType {
  165. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  166. }
  167. }
  168. func TestForbiddenResponse(t *testing.T) {
  169. r, err := http.NewRequest("GET", "/", nil)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. w := httptest.NewRecorder()
  174. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  175. Forbidden(w, r)
  176. })
  177. handler.ServeHTTP(w, r)
  178. resp := w.Result()
  179. expectedStatusCode := http.StatusForbidden
  180. if resp.StatusCode != expectedStatusCode {
  181. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  182. }
  183. expectedBody := `{"error_message":"access forbidden"}`
  184. actualBody := w.Body.String()
  185. if actualBody != expectedBody {
  186. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  187. }
  188. expectedContentType := contentTypeHeader
  189. actualContentType := resp.Header.Get("Content-Type")
  190. if actualContentType != expectedContentType {
  191. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  192. }
  193. }
  194. func TestNotFoundResponse(t *testing.T) {
  195. r, err := http.NewRequest("GET", "/", nil)
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. w := httptest.NewRecorder()
  200. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  201. NotFound(w, r)
  202. })
  203. handler.ServeHTTP(w, r)
  204. resp := w.Result()
  205. expectedStatusCode := http.StatusNotFound
  206. if resp.StatusCode != expectedStatusCode {
  207. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  208. }
  209. expectedBody := `{"error_message":"resource not found"}`
  210. actualBody := w.Body.String()
  211. if actualBody != expectedBody {
  212. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  213. }
  214. expectedContentType := contentTypeHeader
  215. actualContentType := resp.Header.Get("Content-Type")
  216. if actualContentType != expectedContentType {
  217. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  218. }
  219. }
  220. func TestBuildInvalidJSONResponse(t *testing.T) {
  221. r, err := http.NewRequest("GET", "/", nil)
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. w := httptest.NewRecorder()
  226. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  227. OK(w, r, make(chan int))
  228. })
  229. handler.ServeHTTP(w, r)
  230. resp := w.Result()
  231. expectedStatusCode := http.StatusInternalServerError
  232. if resp.StatusCode != expectedStatusCode {
  233. t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
  234. }
  235. expectedBody := `{"error_message":"json: unsupported type: chan int"}`
  236. actualBody := w.Body.String()
  237. if actualBody != expectedBody {
  238. t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
  239. }
  240. expectedContentType := contentTypeHeader
  241. actualContentType := resp.Header.Get("Content-Type")
  242. if actualContentType != expectedContentType {
  243. t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
  244. }
  245. }