Skip to content

Commit 2942b17

Browse files
committed
handler_test: adds GraphQL Playground unit tests
1 parent 9a4af46 commit 2942b17

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

handler_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,61 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
290290
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
291291
}
292292
}
293+
294+
func TestPlayground(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", "/custom-path/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+
PlaygroundConfig: &handler.PlaygroundConfig{
325+
Endpoint: "/custom-path/graphql",
326+
SubscriptionEndpoint: "/custom-path/ws",
327+
},
328+
})
329+
330+
resp := httptest.NewRecorder()
331+
h.ContextHandler(context.Background(), resp, req)
332+
333+
if resp.Code != http.StatusOK {
334+
t.Fatalf("unexpected server response %v", resp.Code)
335+
}
336+
337+
expectedBodyContains := []string{
338+
`endpoint: "/custom-path/graphql"`,
339+
`subscriptionEndpoint: "/custom-path/ws"`,
340+
}
341+
respBody := resp.Body.String()
342+
t.Log(respBody)
343+
344+
for _, e := range expectedBodyContains {
345+
if !strings.Contains(respBody, e) {
346+
t.Fatalf("wrong body, expected %s to contain %s", respBody, e)
347+
}
348+
}
349+
350+
}

0 commit comments

Comments
 (0)