-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract shared handler test server stubs
- Loading branch information
Showing
8 changed files
with
214 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package testserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/99designs/gqlgen/graphql" | ||
"github.com/99designs/gqlgen/graphql/handler" | ||
"github.com/vektah/gqlparser" | ||
"github.com/vektah/gqlparser/ast" | ||
) | ||
|
||
// New provides a server for use in tests that isn't relying on generated code. It isnt a perfect reproduction of | ||
// a generated server, but it aims to be good enough to test the handler package without relying on codegen. | ||
func New() *TestServer { | ||
next := make(chan struct{}) | ||
now := time.Unix(0, 0) | ||
|
||
graphql.Now = func() time.Time { | ||
defer func() { | ||
now = now.Add(100 * time.Nanosecond) | ||
}() | ||
return now | ||
} | ||
|
||
schema := gqlparser.MustLoadSchema(&ast.Source{Input: ` | ||
schema { query: Query } | ||
type Query { | ||
name: String! | ||
find(id: Int!): String! | ||
} | ||
type Mutation { | ||
name: String! | ||
} | ||
type Subscription { | ||
name: String! | ||
} | ||
`}) | ||
|
||
es := &graphql.ExecutableSchemaMock{ | ||
QueryFunc: func(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { | ||
// Field execution happens inside the generated code, lets simulate some of it. | ||
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{ | ||
Object: "Query", | ||
Field: graphql.CollectedField{ | ||
Field: &ast.Field{ | ||
Name: "name", | ||
Alias: "name", | ||
Definition: schema.Types["Query"].Fields.ForName("name"), | ||
}, | ||
}, | ||
}) | ||
res, err := graphql.GetRequestContext(ctx).ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) { | ||
return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res.(*graphql.Response) | ||
}, | ||
MutationFunc: func(ctx context.Context, op *ast.OperationDefinition) *graphql.Response { | ||
return graphql.ErrorResponse(ctx, "mutations are not supported") | ||
}, | ||
SubscriptionFunc: func(ctx context.Context, op *ast.OperationDefinition) func() *graphql.Response { | ||
return func() *graphql.Response { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-next: | ||
return &graphql.Response{ | ||
Data: []byte(`{"name":"test"}`), | ||
} | ||
} | ||
} | ||
}, | ||
SchemaFunc: func() *ast.Schema { | ||
return schema | ||
}, | ||
} | ||
return &TestServer{ | ||
Server: handler.New(es), | ||
next: next, | ||
} | ||
} | ||
|
||
type TestServer struct { | ||
*handler.Server | ||
next chan struct{} | ||
} | ||
|
||
func (s *TestServer) SendNextSubscriptionMessage() { | ||
select { | ||
case s.next <- struct{}{}: | ||
case <-time.After(1 * time.Second): | ||
fmt.Println("WARNING: no active subscription") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package transport_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/99designs/gqlgen/graphql/handler/testserver" | ||
"github.com/99designs/gqlgen/graphql/handler/transport" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGET(t *testing.T) { | ||
h := testserver.New() | ||
h.AddTransport(transport.GET{}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query={name}", ``) | ||
assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String()) | ||
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("decode failure", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query={name}&variables=notjson", "") | ||
assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String()) | ||
assert.Equal(t, `{"errors":[{"message":"variables could not be decoded"}],"data":null}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("invalid variable", func(t *testing.T) { | ||
resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "") | ||
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String()) | ||
assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"]}],"data":null}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("parse failure", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query=!", "") | ||
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String()) | ||
assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}]}],"data":null}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("no mutations", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query=mutation{name}", "") | ||
assert.Equal(t, http.StatusNotAcceptable, resp.Code, resp.Body.String()) | ||
assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String()) | ||
}) | ||
} |
Oops, something went wrong.