Skip to content

Commit

Permalink
Take raw into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Fuhrimann committed Aug 27, 2017
1 parent ce8e677 commit e2a07d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion graphiql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestRenderGraphiQL(t *testing.T) {
cases := map[string]struct {
graphiqlEnabled bool
accept string
url string
expectedStatusCode int
expectedContentType string
expectedBodyContains string
Expand All @@ -37,11 +38,23 @@ func TestRenderGraphiQL(t *testing.T) {
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, "", nil)
req, err := http.NewRequest(http.MethodGet, tc.url, nil)
if err != nil {
t.Error(err)
}
Expand Down
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *

if h.graphiql {
acceptHeader := r.Header.Get("Accept")
if !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
_, raw := r.URL.Query()["raw"]
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
renderGraphiQL(w, params)
return
}
Expand Down

0 comments on commit e2a07d5

Please sign in to comment.