json_test.go 9.1 KB

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