-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
tests_test.go
46 lines (36 loc) · 1.24 KB
/
tests_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fuego
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
// Contains random tests reported on the issues.
func TestContentType(t *testing.T) {
server := NewServer()
t.Run("Sends application/problem+json when return type is HTTPError", func(t *testing.T) {
GetStd(server, "/json-problems", func(w http.ResponseWriter, r *http.Request) {
SendJSONError(w, nil, UnauthorizedError{
Title: "Unauthorized",
})
})
req := httptest.NewRequest("GET", "/json-problems", nil)
w := httptest.NewRecorder()
server.Mux.ServeHTTP(w, req)
require.Equal(t, "application/problem+json", w.Result().Header.Get("Content-Type"))
require.Equal(t, 401, w.Code)
require.Contains(t, w.Body.String(), "Unauthorized")
})
t.Run("Sends application/json when return type is not HTTPError", func(t *testing.T) {
GetStd(server, "/json", func(w http.ResponseWriter, r *http.Request) {
SendJSONError(w, nil, errors.New("error"))
})
req := httptest.NewRequest("GET", "/json", nil)
w := httptest.NewRecorder()
server.Mux.ServeHTTP(w, req)
require.Equal(t, "application/json", w.Header().Get("Content-Type"))
require.Equal(t, 500, w.Code)
require.Equal(t, "{}\n", w.Body.String())
})
}