json_test.go 8.6 KB

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