-
Notifications
You must be signed in to change notification settings - Fork 133
/
graphcoolPlayground_test.go
91 lines (80 loc) · 2.56 KB
/
graphcoolPlayground_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
91
package handler_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
)
func TestRenderPlayground(t *testing.T) {
cases := map[string]struct {
playgroundEnabled bool
accept string
url string
expectedStatusCode int
expectedContentType string
expectedBodyContains string
}{
"renders Playground": {
playgroundEnabled: true,
accept: "text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
expectedBodyContains: "<!DOCTYPE html>",
},
"doesn't render Playground if turned off": {
playgroundEnabled: false,
accept: "text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render Playground if Content-Type application/json is present": {
playgroundEnabled: true,
accept: "application/json,text/html",
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render Playground if Content-Type text/html is not present": {
playgroundEnabled: true,
expectedStatusCode: http.StatusOK,
expectedContentType: "application/json; charset=utf-8",
},
"doesn't render Playground if 'raw' query is present": {
playgroundEnabled: 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: false,
Playground: tc.playgroundEnabled,
})
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)
}
})
}
}