| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package json // import "miniflux.app/v2/internal/http/response/json"
- import (
- "errors"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- func TestOKResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- OK(w, r, map[string]string{"key": "value"})
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- defer resp.Body.Close()
- expectedStatusCode := http.StatusOK
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"key":"value"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestCreatedResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- Created(w, r, map[string]string{"key": "value"})
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusCreated
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"key":"value"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestNoContentResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- NoContent(w, r)
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusNoContent
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := ``
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestServerErrorResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- ServerError(w, r, errors.New("some error"))
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- defer resp.Body.Close()
- expectedStatusCode := http.StatusInternalServerError
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"some error"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestBadRequestResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- BadRequest(w, r, errors.New("Some Error"))
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusBadRequest
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"Some Error"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestUnauthorizedResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- Unauthorized(w, r)
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusUnauthorized
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"access unauthorized"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestForbiddenResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- Forbidden(w, r)
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusForbidden
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"access forbidden"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestNotFoundResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- NotFound(w, r)
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusNotFound
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"resource not found"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
- func TestBuildInvalidJSONResponse(t *testing.T) {
- r, err := http.NewRequest("GET", "/", nil)
- if err != nil {
- t.Fatal(err)
- }
- w := httptest.NewRecorder()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- OK(w, r, make(chan int))
- })
- handler.ServeHTTP(w, r)
- resp := w.Result()
- expectedStatusCode := http.StatusInternalServerError
- if resp.StatusCode != expectedStatusCode {
- t.Fatalf(`Unexpected status code, got %d instead of %d`, resp.StatusCode, expectedStatusCode)
- }
- expectedBody := `{"error_message":"json: unsupported type: chan int"}`
- actualBody := w.Body.String()
- if actualBody != expectedBody {
- t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
- }
- expectedContentType := contentTypeHeader
- actualContentType := resp.Header.Get("Content-Type")
- if actualContentType != expectedContentType {
- t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
- }
- }
|