Skip to content

Commit 70ee617

Browse files
author
Tobias Fuhrimann
committed
Add tests for GraphiQL
1 parent 24733ec commit 70ee617

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

graphiql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params) {
3939

4040
// Create result string
4141
result, err := json.MarshalIndent(graphql.Do(params), "", " ")
42+
if err != nil {
43+
http.Error(w, err.Error(), http.StatusInternalServerError)
44+
return
45+
}
4246
resString := string(result)
4347

4448
p := graphiqlPage{

graphiql_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package handler_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strings"
7+
"testing"
8+
9+
"github.com/graphql-go/graphql/testutil"
10+
"github.com/graphql-go/handler"
11+
)
12+
13+
func TestRenderGraphiQL(t *testing.T) {
14+
cases := map[string]struct {
15+
graphiqlEnabled bool
16+
accept string
17+
expectedStatusCode int
18+
expectedContentType string
19+
expectedBodyContains string
20+
}{
21+
"renders GraphiQL": {
22+
graphiqlEnabled: true,
23+
accept: "text/html",
24+
expectedStatusCode: http.StatusOK,
25+
expectedContentType: "text/html; charset=utf-8",
26+
expectedBodyContains: "<!DOCTYPE html>",
27+
},
28+
"doesn't render graphiQL if turned off": {
29+
graphiqlEnabled: false,
30+
accept: "text/html",
31+
expectedStatusCode: http.StatusOK,
32+
expectedContentType: "application/json; charset=utf-8",
33+
},
34+
"doesn't render GraphiQL if Content-Type application/json is present": {
35+
graphiqlEnabled: true,
36+
accept: "application/json,text/html",
37+
expectedStatusCode: http.StatusOK,
38+
expectedContentType: "application/json; charset=utf-8",
39+
},
40+
}
41+
42+
for tcID, tc := range cases {
43+
t.Run(tcID, func(t *testing.T) {
44+
req, err := http.NewRequest(http.MethodGet, "", nil)
45+
if err != nil {
46+
t.Error(err)
47+
}
48+
49+
req.Header.Set("Accept", tc.accept)
50+
51+
h := handler.New(&handler.Config{
52+
Schema: &testutil.StarWarsSchema,
53+
GraphiQL: tc.graphiqlEnabled,
54+
})
55+
56+
rr := httptest.NewRecorder()
57+
58+
h.ServeHTTP(rr, req)
59+
resp := rr.Result()
60+
61+
statusCode := resp.StatusCode
62+
if statusCode != tc.expectedStatusCode {
63+
t.Fatalf("%s: wrong status code, expected %v, got %v", tcID, tc.expectedStatusCode, statusCode)
64+
}
65+
66+
contentType := resp.Header.Get("Content-Type")
67+
if contentType != tc.expectedContentType {
68+
t.Fatalf("%s: wrong content type, expected %s, got %s", tcID, tc.expectedContentType, contentType)
69+
}
70+
71+
body := rr.Body.String()
72+
if !strings.Contains(body, tc.expectedBodyContains) {
73+
t.Fatalf("%s: wrong body, expected %s to contain %s", tcID, body, tc.expectedBodyContains)
74+
}
75+
})
76+
}
77+
}

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
139139
}
140140

141141
// use proper JSON Header
142-
w.Header().Add("Content-Type", "application/json")
142+
w.Header().Add("Content-Type", "application/json; charset=utf-8")
143143

144144
if h.pretty {
145145
w.WriteHeader(http.StatusOK)

0 commit comments

Comments
 (0)