1- package handler_test
1+ package handler
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "io/ioutil"
@@ -12,9 +13,6 @@ import (
1213
1314 "github.com/graphql-go/graphql"
1415 "github.com/graphql-go/graphql/testutil"
15- "github.com/graphql-go/handler"
16- "github.com/graphql-go/relay/examples/starwars" // TODO: remove this dependency
17- "golang.org/x/net/context"
1816)
1917
2018func decodeResponse (t * testing.T , recorder * httptest.ResponseRecorder ) * graphql.Result {
@@ -33,7 +31,7 @@ func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.
3331 }
3432 return & target
3533}
36- func executeTest (t * testing.T , h * handler. Handler , req * http.Request ) (* graphql.Result , * httptest.ResponseRecorder ) {
34+ func executeTest (t * testing.T , h * Handler , req * http.Request ) (* graphql.Result , * httptest.ResponseRecorder ) {
3735 resp := httptest .NewRecorder ()
3836 h .ServeHTTP (resp , req )
3937 result := decodeResponse (t , resp )
@@ -53,7 +51,8 @@ func TestContextPropagated(t *testing.T) {
5351 },
5452 },
5553 })
56- myNameSchema , err := graphql .NewSchema (graphql.SchemaConfig {myNameQuery , nil })
54+
55+ myNameSchema , err := graphql .NewSchema (graphql.SchemaConfig {Query : myNameQuery })
5756 if err != nil {
5857 t .Fatal (err )
5958 }
@@ -66,18 +65,19 @@ func TestContextPropagated(t *testing.T) {
6665 queryString := `query={name}`
6766 req , _ := http .NewRequest ("GET" , fmt .Sprintf ("/graphql?%v" , queryString ), nil )
6867
69- h := handler .New (& handler.Config {
70- Schema : & myNameSchema ,
71- Pretty : true ,
72- })
68+ h := New (& Config {Schema : & myNameSchema , Pretty : true })
7369
74- ctx := context .WithValue (context .Background (), "name" , "context-data" )
70+ * req = * req . WithContext ( context .WithValue (context .Background (), "name" , "context-data" ) )
7571 resp := httptest .NewRecorder ()
76- h .ContextHandler (ctx , resp , req )
72+
73+ h .ContextHandler (resp , req )
74+
7775 result := decodeResponse (t , resp )
76+
7877 if resp .Code != http .StatusOK {
7978 t .Fatalf ("unexpected server response %v" , resp .Code )
8079 }
80+
8181 if ! reflect .DeepEqual (result , expected ) {
8282 t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
8383 }
@@ -92,17 +92,18 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) {
9292 },
9393 },
9494 }
95+
9596 queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
9697 req , _ := http .NewRequest ("GET" , fmt .Sprintf ("/graphql?%v" , queryString ), nil )
9798
98- h := handler .New (& handler.Config {
99- Schema : & starwars .Schema ,
100- Pretty : true ,
101- })
99+ h := New (& Config {Schema : & schema , Pretty : true })
100+
102101 result , resp := executeTest (t , h , req )
102+
103103 if resp .Code != http .StatusOK {
104104 t .Fatalf ("unexpected server response %v" , resp .Code )
105105 }
106+
106107 if ! reflect .DeepEqual (result , expected ) {
107108 t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
108109 }
@@ -117,17 +118,18 @@ func TestHandler_BasicQuery_Ugly(t *testing.T) {
117118 },
118119 },
119120 }
121+
120122 queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
121123 req , _ := http .NewRequest ("GET" , fmt .Sprintf ("/graphql?%v" , queryString ), nil )
122124
123- h := handler .New (& handler.Config {
124- Schema : & starwars .Schema ,
125- Pretty : false ,
126- })
125+ h := New (& Config {Schema : & schema , Pretty : false })
126+
127127 result , resp := executeTest (t , h , req )
128+
128129 if resp .Code != http .StatusOK {
129130 t .Fatalf ("unexpected server response %v" , resp .Code )
130131 }
132+
131133 if ! reflect .DeepEqual (result , expected ) {
132134 t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
133135 }
@@ -148,6 +150,6 @@ func TestHandler_Params_NilParams(t *testing.T) {
148150 }
149151 t .Fatalf ("expected to panic, did not panic" )
150152 }()
151- _ = handler .New (nil )
152153
154+ _ = New (nil )
153155}
0 commit comments