-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
http_multipart_mixed_test.go
170 lines (145 loc) · 4.96 KB
/
http_multipart_mixed_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package transport_test
import (
"bufio"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/99designs/gqlgen/graphql/handler/testserver"
"github.com/99designs/gqlgen/graphql/handler/transport"
)
func TestMultipartMixed(t *testing.T) {
initialize := func() *testserver.TestServer {
h := testserver.New()
h.AddTransport(transport.MultipartMixed{
Boundary: "graphql",
})
return h
}
initializeWithServer := func() (*testserver.TestServer, *httptest.Server) {
h := initialize()
return h, httptest.NewServer(h)
}
createHTTPRequest := func(url string, query string) *http.Request {
req, err := http.NewRequest("POST", url, strings.NewReader(query))
require.NoError(t, err, "Request threw error -> %s", err)
req.Header.Set("Accept", "multipart/mixed")
req.Header.Set("content-type", "application/json; charset=utf-8")
return req
}
doRequest := func(handler http.Handler, target, body string) *httptest.ResponseRecorder {
r := createHTTPRequest(target, body)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
return w
}
t.Run("decode failure", func(t *testing.T) {
handler, srv := initializeWithServer()
resp := doRequest(handler, srv.URL, "notjson")
assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
assert.Equal(
t,
`{"errors":[{"message":"json request body could not be decoded: invalid character 'o' in literal null (expecting 'u') body:notjson"}],"data":null}`,
resp.Body.String(),
)
})
t.Run("parse failure", func(t *testing.T) {
handler, srv := initializeWithServer()
resp := doRequest(handler, srv.URL, `{"query": "!"}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
assert.Equal(
t,
`{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"data":null}`,
resp.Body.String(),
)
})
t.Run("validation failure", func(t *testing.T) {
handler, srv := initializeWithServer()
resp := doRequest(handler, srv.URL, `{"query": "{ title }"}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
assert.Equal(
t,
`{"errors":[{"message":"Cannot query field \"title\" on type \"Query\".","locations":[{"line":1,"column":3}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`,
resp.Body.String(),
)
})
t.Run("invalid variable", func(t *testing.T) {
handler, srv := initializeWithServer()
resp := doRequest(handler, srv.URL,
`{"query": "query($id:Int!){find(id:$id)}","variables":{"id":false}}`,
)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
assert.Equal(
t,
`{"errors":[{"message":"cannot use bool as Int","path":["variable","id"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`,
resp.Body.String(),
)
})
readLine := func(br *bufio.Reader) string {
bs, err := br.ReadString('\n')
require.NoError(t, err)
return bs
}
t.Run("initial and incremental patches", func(t *testing.T) {
handler, srv := initializeWithServer()
defer srv.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
handler.SendNextSubscriptionMessage()
}()
client := &http.Client{}
req := createHTTPRequest(
srv.URL,
`{"query":"query { ... @defer { name } }"}`,
)
res, err := client.Do(req)
require.NoError(t, err, "Request threw error -> %s", err)
defer func() {
require.NoError(t, res.Body.Close())
}()
assert.Equal(t, 200, res.StatusCode, "Request return wrong status -> %d", res.Status)
assert.Equal(t, "keep-alive", res.Header.Get("Connection"))
assert.Contains(t, res.Header.Get("Content-Type"), "multipart/mixed")
assert.Contains(t, res.Header.Get("Content-Type"), `boundary="graphql"`)
br := bufio.NewReader(res.Body)
assert.Equal(t, "--graphql\r\n", readLine(br))
assert.Equal(t, "Content-Type: application/json\r\n", readLine(br))
assert.Equal(t, "\r\n", readLine(br))
assert.Equal(t,
"{\"data\":{\"name\":null},\"hasNext\":true}\r\n",
readLine(br),
)
wg.Add(1)
go func() {
defer wg.Done()
handler.SendNextSubscriptionMessage()
}()
assert.Equal(t, "--graphql\r\n", readLine(br))
assert.Equal(t, "Content-Type: application/json\r\n", readLine(br))
assert.Equal(t, "\r\n", readLine(br))
assert.Equal(
t,
"{\"incremental\":[{\"data\":{\"name\":\"test\"},\"hasNext\":false}],\"hasNext\":false}\r\n",
readLine(br),
)
assert.Equal(t, "--graphql--\r\n", readLine(br))
wg.Add(1)
go func() {
defer wg.Done()
handler.SendCompleteSubscriptionMessage()
}()
_, err = br.ReadByte()
assert.Equal(t, err, io.EOF)
wg.Wait()
})
}