Skip to content

Commit e2a07d5

Browse files
author
Tobias Fuhrimann
committed
Take raw into account
1 parent ce8e677 commit e2a07d5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

graphiql_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestRenderGraphiQL(t *testing.T) {
1414
cases := map[string]struct {
1515
graphiqlEnabled bool
1616
accept string
17+
url string
1718
expectedStatusCode int
1819
expectedContentType string
1920
expectedBodyContains string
@@ -37,11 +38,23 @@ func TestRenderGraphiQL(t *testing.T) {
3738
expectedStatusCode: http.StatusOK,
3839
expectedContentType: "application/json; charset=utf-8",
3940
},
41+
"doesn't render GraphiQL if Content-Type text/html is not present": {
42+
graphiqlEnabled: true,
43+
expectedStatusCode: http.StatusOK,
44+
expectedContentType: "application/json; charset=utf-8",
45+
},
46+
"doesn't render GraphiQL if 'raw' query is present": {
47+
graphiqlEnabled: true,
48+
accept: "text/html",
49+
url: "?raw",
50+
expectedStatusCode: http.StatusOK,
51+
expectedContentType: "application/json; charset=utf-8",
52+
},
4053
}
4154

4255
for tcID, tc := range cases {
4356
t.Run(tcID, func(t *testing.T) {
44-
req, err := http.NewRequest(http.MethodGet, "", nil)
57+
req, err := http.NewRequest(http.MethodGet, tc.url, nil)
4558
if err != nil {
4659
t.Error(err)
4760
}

handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
132132

133133
if h.graphiql {
134134
acceptHeader := r.Header.Get("Accept")
135-
if !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
135+
_, raw := r.URL.Query()["raw"]
136+
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
136137
renderGraphiQL(w, params)
137138
return
138139
}

0 commit comments

Comments
 (0)