@@ -291,7 +291,60 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
291
291
}
292
292
}
293
293
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 ) {
295
348
query := graphql .NewObject (graphql.ObjectConfig {
296
349
Name : "Query" ,
297
350
Fields : graphql.Fields {
@@ -335,16 +388,15 @@ func TestPlayground(t *testing.T) {
335
388
}
336
389
337
390
expectedBodyContains := []string {
391
+ "GraphQL Playground" ,
338
392
`endpoint: "/custom-path/graphql"` ,
339
393
`subscriptionEndpoint: "/custom-path/ws"` ,
340
394
}
341
395
respBody := resp .Body .String ()
342
- t .Log (respBody )
343
396
344
397
for _ , e := range expectedBodyContains {
345
398
if ! strings .Contains (respBody , e ) {
346
399
t .Fatalf ("wrong body, expected %s to contain %s" , respBody , e )
347
400
}
348
401
}
349
-
350
402
}
0 commit comments