@@ -290,3 +290,113 @@ 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 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 ) {
348+ query := graphql .NewObject (graphql.ObjectConfig {
349+ Name : "Query" ,
350+ Fields : graphql.Fields {
351+ "ping" : & graphql.Field {
352+ Name : "ping" ,
353+ Type : graphql .String ,
354+ Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
355+ return "OK" , nil
356+ },
357+ },
358+ },
359+ })
360+
361+ schema , err := graphql .NewSchema (graphql.SchemaConfig {
362+ Query : query ,
363+ })
364+ if err != nil {
365+ t .Fatal (err )
366+ }
367+
368+ req , err := http .NewRequest ("GET" , "/custom-path/graphql" , nil )
369+ req .Header .Set ("Accept" , "text/html" )
370+ if err != nil {
371+ t .Fatal (err )
372+ }
373+
374+ h := handler .New (& handler.Config {
375+ Schema : & schema ,
376+ Playground : true ,
377+ PlaygroundConfig : & handler.PlaygroundConfig {
378+ Endpoint : "/custom-path/graphql" ,
379+ SubscriptionEndpoint : "/custom-path/ws" ,
380+ },
381+ })
382+
383+ resp := httptest .NewRecorder ()
384+ h .ContextHandler (context .Background (), resp , req )
385+
386+ if resp .Code != http .StatusOK {
387+ t .Fatalf ("unexpected server response %v" , resp .Code )
388+ }
389+
390+ expectedBodyContains := []string {
391+ "GraphQL Playground" ,
392+ `endpoint: "/custom-path/graphql"` ,
393+ `subscriptionEndpoint: "/custom-path/ws"` ,
394+ }
395+ respBody := resp .Body .String ()
396+
397+ for _ , e := range expectedBodyContains {
398+ if ! strings .Contains (respBody , e ) {
399+ t .Fatalf ("wrong body, expected %s to contain %s" , respBody , e )
400+ }
401+ }
402+ }
0 commit comments