Skip to content

Commit 5f4c951

Browse files
committed
handler_test: consolidates graphql playground unit tests
1 parent 2942b17 commit 5f4c951

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

handler_test.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,60 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
291291
}
292292
}
293293

294-
func TestPlayground(t *testing.T) {
294+
func TestPlaygroundWithDefaultConfig(t *testing.T) {
295+
query := graphql.NewObject(graphql.ObjectConfig{
296+
Name: "Query",
297+
Fields: graphql.Fields{
298+
"ping": &graphql.Field{
299+
Name: "ping",
300+
Type: graphql.String,
301+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
302+
return "OK", nil
303+
},
304+
},
305+
},
306+
})
307+
308+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
309+
Query: query,
310+
})
311+
if err != nil {
312+
t.Fatal(err)
313+
}
314+
315+
req, err := http.NewRequest("GET", "/graphql", nil)
316+
req.Header.Set("Accept", "text/html")
317+
if err != nil {
318+
t.Fatal(err)
319+
}
320+
321+
h := handler.New(&handler.Config{
322+
Schema: &schema,
323+
Playground: true,
324+
})
325+
326+
resp := httptest.NewRecorder()
327+
h.ContextHandler(context.Background(), resp, req)
328+
329+
if resp.Code != http.StatusOK {
330+
t.Fatalf("unexpected server response %v", resp.Code)
331+
}
332+
333+
expectedBodyContains := []string{
334+
"GraphQL Playground",
335+
`endpoint: "/graphql"`,
336+
`subscriptionEndpoint: "ws:///subscriptions"`,
337+
}
338+
respBody := resp.Body.String()
339+
340+
for _, e := range expectedBodyContains {
341+
if !strings.Contains(respBody, e) {
342+
t.Fatalf("wrong body, expected %s to contain %s", respBody, e)
343+
}
344+
}
345+
}
346+
347+
func TestPlaygroundWithCustomConfig(t *testing.T) {
295348
query := graphql.NewObject(graphql.ObjectConfig{
296349
Name: "Query",
297350
Fields: graphql.Fields{
@@ -335,16 +388,15 @@ func TestPlayground(t *testing.T) {
335388
}
336389

337390
expectedBodyContains := []string{
391+
"GraphQL Playground",
338392
`endpoint: "/custom-path/graphql"`,
339393
`subscriptionEndpoint: "/custom-path/ws"`,
340394
}
341395
respBody := resp.Body.String()
342-
t.Log(respBody)
343396

344397
for _, e := range expectedBodyContains {
345398
if !strings.Contains(respBody, e) {
346399
t.Fatalf("wrong body, expected %s to contain %s", respBody, e)
347400
}
348401
}
349-
350402
}

0 commit comments

Comments
 (0)