forked from graphql-go/handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
153 lines (138 loc) · 4.02 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package handler_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
"github.com/graphql-go/relay/examples/starwars" // TODO: remove this dependency
"golang.org/x/net/context"
)
func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.Result {
// clone request body reader so that we can have a nicer error message
bodyString := ""
var target graphql.Result
if b, err := ioutil.ReadAll(recorder.Body); err == nil {
bodyString = string(b)
}
readerClone := strings.NewReader(bodyString)
decoder := json.NewDecoder(readerClone)
err := decoder.Decode(&target)
if err != nil {
t.Fatalf("DecodeResponseToType(): %v \n%v", err.Error(), bodyString)
}
return &target
}
func executeTest(t *testing.T, h *handler.Handler, req *http.Request) (*graphql.Result, *httptest.ResponseRecorder) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
result := decodeResponse(t, resp)
return result, resp
}
func TestContextPropagated(t *testing.T) {
myNameQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"name": &graphql.Field{
Name: "name",
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return p.Context.Value("name"), nil
},
},
},
})
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{myNameQuery, nil})
if err != nil {
t.Fatal(err)
}
expected := &graphql.Result{
Data: map[string]interface{}{
"name": "context-data",
},
}
queryString := `query={name}`
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
h := handler.New(&handler.Config{
Schema: &myNameSchema,
Pretty: true,
})
ctx := context.WithValue(context.Background(), "name", "context-data")
resp := httptest.NewRecorder()
h.ContextHandler(ctx, resp, req)
result := decodeResponse(t, resp)
if resp.Code != http.StatusOK {
t.Fatalf("unexpected server response %v", resp.Code)
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestHandler_BasicQuery_Pretty(t *testing.T) {
expected := &graphql.Result{
Data: map[string]interface{}{
"rebels": map[string]interface{}{
"id": "RmFjdGlvbjox",
"name": "Alliance to Restore the Republic",
},
},
}
queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
h := handler.New(&handler.Config{
Schema: &starwars.Schema,
Pretty: true,
})
result, resp := executeTest(t, h, req)
if resp.Code != http.StatusOK {
t.Fatalf("unexpected server response %v", resp.Code)
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestHandler_BasicQuery_Ugly(t *testing.T) {
expected := &graphql.Result{
Data: map[string]interface{}{
"rebels": map[string]interface{}{
"id": "RmFjdGlvbjox",
"name": "Alliance to Restore the Republic",
},
},
}
queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
h := handler.New(&handler.Config{
Schema: &starwars.Schema,
Pretty: false,
})
result, resp := executeTest(t, h, req)
if resp.Code != http.StatusOK {
t.Fatalf("unexpected server response %v", resp.Code)
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
}
func TestHandler_Params_NilParams(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if str, ok := r.(string); ok {
if str != "undefined GraphQL schema" {
t.Fatalf("unexpected error, got %v", r)
}
// test passed
return
}
t.Fatalf("unexpected error, got %v", r)
}
t.Fatalf("expected to panic, did not panic")
}()
_ = handler.New(nil)
}