Skip to content

Commit

Permalink
handler_test: consolidates graphql playground unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed May 17, 2024
1 parent 2942b17 commit 5f4c951
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,60 @@ func TestHandler_BasicQuery_WithFormatErrorFn(t *testing.T) {
}
}

func TestPlayground(t *testing.T) {
func TestPlaygroundWithDefaultConfig(t *testing.T) {
query := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"ping": &graphql.Field{
Name: "ping",
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "OK", nil
},
},
},
})

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: query,
})
if err != nil {
t.Fatal(err)
}

req, err := http.NewRequest("GET", "/graphql", nil)
req.Header.Set("Accept", "text/html")
if err != nil {
t.Fatal(err)
}

h := handler.New(&handler.Config{
Schema: &schema,
Playground: true,
})

resp := httptest.NewRecorder()
h.ContextHandler(context.Background(), resp, req)

if resp.Code != http.StatusOK {
t.Fatalf("unexpected server response %v", resp.Code)
}

expectedBodyContains := []string{
"GraphQL Playground",
`endpoint: "/graphql"`,
`subscriptionEndpoint: "ws:///subscriptions"`,
}
respBody := resp.Body.String()

for _, e := range expectedBodyContains {
if !strings.Contains(respBody, e) {
t.Fatalf("wrong body, expected %s to contain %s", respBody, e)
}
}
}

func TestPlaygroundWithCustomConfig(t *testing.T) {
query := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
Expand Down Expand Up @@ -335,16 +388,15 @@ func TestPlayground(t *testing.T) {
}

expectedBodyContains := []string{
"GraphQL Playground",
`endpoint: "/custom-path/graphql"`,
`subscriptionEndpoint: "/custom-path/ws"`,
}
respBody := resp.Body.String()
t.Log(respBody)

for _, e := range expectedBodyContains {
if !strings.Contains(respBody, e) {
t.Fatalf("wrong body, expected %s to contain %s", respBody, e)
}
}

}

0 comments on commit 5f4c951

Please sign in to comment.