-
Notifications
You must be signed in to change notification settings - Fork 135
/
graphiql_test.go
90 lines (79 loc) · 2.52 KB
/
graphiql_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package handler_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
)
func TestRenderGraphiQL(t *testing.T) {
cases := map[string]struct {
graphiqlEnabled bool
accept string
url string
expectedStatusCode int
expectedContentType string
expectedBodyContains string
}{
"renders GraphiQL": {
graphiqlEnabled: true,
accept: "text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
expectedBodyContains: "<!DOCTYPE html>",
},
"doesn't render graphiQL if turned off": {
graphiqlEnabled: false,
accept: "text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render GraphiQL if Content-Type application/json is present": {
graphiqlEnabled: true,
accept: "application/json,text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render GraphiQL if Content-Type text/html is not present": {
graphiqlEnabled: true,
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render GraphiQL if 'raw' query is present": {
graphiqlEnabled: true,
accept: "text/html",
url: "?raw",
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
}
for tcID, tc := range cases {
t.Run(tcID, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, tc.url, nil)
if err != nil {
t.Error(err)
}
req.Header.Set("Accept", tc.accept)
h := handler.New(&handler.Config{
Schema: &testutil.StarWarsSchema,
GraphiQL: tc.graphiqlEnabled,
})
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
resp := rr.Result()
statusCode := resp.StatusCode
if statusCode != tc.expectedStatusCode {
t.Fatalf("%s: wrong status code, expected %v, got %v", tcID, tc.expectedStatusCode, statusCode)
}
contentType := resp.Header.Get("Content-Type")
if contentType != tc.expectedContentType {
t.Fatalf("%s: wrong content type, expected %s, got %s", tcID, tc.expectedContentType, contentType)
}
body := rr.Body.String()
if !strings.Contains(body, tc.expectedBodyContains) {
t.Fatalf("%s: wrong body, expected %s to contain %s", tcID, body, tc.expectedBodyContains)
}
})
}
}