-
Notifications
You must be signed in to change notification settings - Fork 9
/
http_test.go
114 lines (87 loc) · 3.04 KB
/
http_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
package jaal_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/kylelemons/godebug/pretty"
"go.appointy.com/jaal"
"go.appointy.com/jaal/schemabuilder"
)
func testHTTPRequest(req *http.Request) *httptest.ResponseRecorder {
schema := schemabuilder.NewSchema()
query := schema.Query()
query.FieldFunc("mirror", func(args struct{ Value int64 }) int64 {
return args.Value * -1
})
builtSchema := schema.MustBuild()
rr := httptest.NewRecorder()
handler := jaal.HTTPHandler(builtSchema)
handler.ServeHTTP(rr, req)
return rr
}
func TestHTTPMustPost(t *testing.T) {
req, err := http.NewRequest("GET", "/graphql", nil)
if err != nil {
t.Fatal(err)
}
rr := testHTTPRequest(req)
if rr.Code != 200 {
t.Errorf("expected 200, but received %d", rr.Code)
}
if diff := pretty.Compare(rr.Body.String(), `{"data":null,"errors":[{"message":"request must be a POST","extensions":{"code":"Unknown"},"paths":[]}]}`); diff != "" {
t.Errorf("expected response to match, but received %s", diff)
}
}
func TestHTTPParseQuery(t *testing.T) {
req, err := http.NewRequest("POST", "/graphql", nil)
if err != nil {
t.Fatal(err)
}
rr := testHTTPRequest(req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, but received %d", rr.Code)
}
if diff := pretty.Compare(rr.Body.String(), `{"data":null,"errors":[{"message":"request must include a query","extensions":{"code":"Unknown"},"paths":[]}]}`); diff != "" {
t.Errorf("expected response to match, but received %s", diff)
}
}
func TestHTTPMustHaveQuery(t *testing.T) {
req, err := http.NewRequest("POST", "/graphql", strings.NewReader(`{"query":""}`))
if err != nil {
t.Fatal(err)
}
rr := testHTTPRequest(req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, but received %d", rr.Code)
}
if diff := pretty.Compare(rr.Body.String(), `{"data":null,"errors":[{"message":"must have a single query","extensions":{"code":"Unknown"},"paths":[]}]}`); diff != "" {
t.Errorf("expected response to match, but received %s", diff)
}
}
func TestHTTPSuccess(t *testing.T) {
req, err := http.NewRequest("POST", "/graphql", strings.NewReader(`{"query": "query TestQuery($value: int64) { mirror(value: $value) }", "variables": { "value": 1 }}`))
if err != nil {
t.Fatal(err)
}
rr := testHTTPRequest(req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, but received %d", rr.Code)
}
if diff := pretty.Compare(rr.Body.String(), "{\"data\":{\"mirror\":-1},\"errors\":null}"); diff != "" {
t.Errorf("expected response to match, but received %s", diff)
}
}
func TestHTTPContentType(t *testing.T) {
req, err := http.NewRequest("POST", "/graphql", strings.NewReader(`{"query": "query TestQuery($value: int64) { mirror(value: $value) }", "variables": { "value": 1 }}`))
if err != nil {
t.Fatal(err)
}
rr := testHTTPRequest(req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, but received %d", rr.Code)
}
if diff := pretty.Compare(rr.HeaderMap.Get("Content-Type"), "application/json"); diff != "" {
t.Errorf("expected response to match, but received %s", diff)
}
}