Skip to content

Commit

Permalink
Add tests for GraphiQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Fuhrimann committed Aug 27, 2017
1 parent 24733ec commit 70ee617
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions graphiql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params) {

// Create result string
result, err := json.MarshalIndent(graphql.Do(params), "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resString := string(result)

p := graphiqlPage{
Expand Down
77 changes: 77 additions & 0 deletions graphiql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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
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",
},
}

for tcID, tc := range cases {
t.Run(tcID, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "", 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)
}
})
}
}
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
}

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

if h.pretty {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit 70ee617

Please sign in to comment.