1
1
import fetch from 'node-fetch' ;
2
2
3
3
import { schema } from './fixtures/simple' ;
4
- import { bodyAsExecResult , startTServer , TServer } from './utils/tserver' ;
4
+ import { bodyAsExecResult , startTServer } from './utils/tserver' ;
5
5
6
- let server ! : TServer ;
6
+ let serverUrl ! : string ;
7
7
beforeAll ( ( ) => {
8
- server = startTServer ( { schema } ) ;
8
+ const server = startTServer ( { schema } ) ;
9
+ serverUrl = server . url ;
9
10
} ) ;
10
11
11
12
describe ( 'Media Types' , ( ) => {
12
13
it ( 'must accept application/graphql+json and match the content-type' , async ( ) => {
13
- const url = new URL ( server . url ) ;
14
+ const url = new URL ( serverUrl ) ;
14
15
url . searchParams . set ( 'query' , '{ __typename }' ) ;
15
16
16
17
const res = await fetch ( url . toString ( ) , {
@@ -25,7 +26,7 @@ describe('Media Types', () => {
25
26
} ) ;
26
27
27
28
it ( 'must accept application/json and match the content-type' , async ( ) => {
28
- const url = new URL ( server . url ) ;
29
+ const url = new URL ( serverUrl ) ;
29
30
url . searchParams . set ( 'query' , '{ __typename }' ) ;
30
31
31
32
const res = await fetch ( url . toString ( ) , {
@@ -38,7 +39,7 @@ describe('Media Types', () => {
38
39
} ) ;
39
40
40
41
it ( 'must accept */* and use application/graphql+json for the content-type' , async ( ) => {
41
- const url = new URL ( server . url ) ;
42
+ const url = new URL ( serverUrl ) ;
42
43
url . searchParams . set ( 'query' , '{ __typename }' ) ;
43
44
44
45
const res = await fetch ( url . toString ( ) , {
@@ -53,7 +54,7 @@ describe('Media Types', () => {
53
54
} ) ;
54
55
55
56
it ( 'must assume application/graphql+json content-type when accept is missing' , async ( ) => {
56
- const url = new URL ( server . url ) ;
57
+ const url = new URL ( serverUrl ) ;
57
58
url . searchParams . set ( 'query' , '{ __typename }' ) ;
58
59
59
60
const res = await fetch ( url . toString ( ) ) ;
@@ -64,7 +65,7 @@ describe('Media Types', () => {
64
65
} ) ;
65
66
66
67
it ( 'must use utf-8 charset in response' , async ( ) => {
67
- const url = new URL ( server . url ) ;
68
+ const url = new URL ( serverUrl ) ;
68
69
url . searchParams . set ( 'query' , '{ __typename }' ) ;
69
70
70
71
const res = await fetch ( url . toString ( ) ) ;
@@ -73,7 +74,7 @@ describe('Media Types', () => {
73
74
} ) ;
74
75
75
76
it ( 'must accept only utf-8 charset' , async ( ) => {
76
- const url = new URL ( server . url ) ;
77
+ const url = new URL ( serverUrl ) ;
77
78
url . searchParams . set ( 'query' , '{ __typename }' ) ;
78
79
79
80
const res = await fetch ( url . toString ( ) , {
@@ -88,7 +89,7 @@ describe('Media Types', () => {
88
89
89
90
describe ( 'Request' , ( ) => {
90
91
it ( 'must accept POST requests' , async ( ) => {
91
- const res = await fetch ( server . url , {
92
+ const res = await fetch ( serverUrl , {
92
93
method : 'POST' ,
93
94
headers : { 'content-type' : 'application/json' } ,
94
95
body : JSON . stringify ( { query : '{ __typename }' } ) ,
@@ -97,7 +98,7 @@ describe('Request', () => {
97
98
} ) ;
98
99
99
100
it ( 'may accept application/x-www-form-urlencoded formatted GET requests' , async ( ) => {
100
- const url = new URL ( server . url ) ;
101
+ const url = new URL ( serverUrl ) ;
101
102
url . searchParams . set ( 'query' , '{ __typename }' ) ;
102
103
103
104
const res = await fetch ( url . toString ( ) ) ;
@@ -106,7 +107,7 @@ describe('Request', () => {
106
107
107
108
describe ( 'GET' , ( ) => {
108
109
it ( 'must not allow executing mutations' , async ( ) => {
109
- const url = new URL ( server . url ) ;
110
+ const url = new URL ( serverUrl ) ;
110
111
url . searchParams . set (
111
112
'query' ,
112
113
'mutation { f10d019f15804f92a7c7470205c866da }' , // making sure the field doesnt exist
@@ -123,15 +124,15 @@ describe('Request', () => {
123
124
124
125
describe ( 'POST' , ( ) => {
125
126
it ( 'should respond with 4xx status code if content-type is not supplied' , async ( ) => {
126
- const res = await fetch ( server . url , {
127
+ const res = await fetch ( serverUrl , {
127
128
method : 'POST' ,
128
129
} ) ;
129
130
expect ( res . status ) . toBeGreaterThanOrEqual ( 400 ) ;
130
131
expect ( res . status ) . toBeLessThanOrEqual ( 599 ) ;
131
132
} ) ;
132
133
133
134
it ( 'must accept application/json requests' , async ( ) => {
134
- const res = await fetch ( server . url , {
135
+ const res = await fetch ( serverUrl , {
135
136
method : 'POST' ,
136
137
headers : { 'content-type' : 'application/json' } ,
137
138
body : JSON . stringify ( { query : '{ __typename }' } ) ,
@@ -140,7 +141,7 @@ describe('Request', () => {
140
141
} ) ;
141
142
142
143
it ( 'must require a request body' , async ( ) => {
143
- const res = await fetch ( server . url , {
144
+ const res = await fetch ( serverUrl , {
144
145
method : 'POST' ,
145
146
headers : { 'content-type' : 'application/json' } ,
146
147
} ) ;
@@ -150,7 +151,7 @@ describe('Request', () => {
150
151
151
152
describe ( 'Request Parameters' , ( ) => {
152
153
it ( 'must require the {query} parameter' , async ( ) => {
153
- const res = await fetch ( server . url , {
154
+ const res = await fetch ( serverUrl , {
154
155
method : 'POST' ,
155
156
headers : {
156
157
'content-type' : 'application/json' ,
@@ -163,7 +164,7 @@ describe('Request', () => {
163
164
it . each ( [ { obj : 'ect' } , 0 , false , [ 'array' ] ] ) (
164
165
'must not allow `%j` for the {query} parameter' ,
165
166
async ( invalid ) => {
166
- const res = await fetch ( server . url , {
167
+ const res = await fetch ( serverUrl , {
167
168
method : 'POST' ,
168
169
headers : {
169
170
'content-type' : 'application/json' ,
@@ -177,7 +178,7 @@ describe('Request', () => {
177
178
} ,
178
179
) ;
179
180
it ( 'must accept a string for the {query} parameter' , async ( ) => {
180
- const res = await fetch ( server . url , {
181
+ const res = await fetch ( serverUrl , {
181
182
method : 'POST' ,
182
183
headers : {
183
184
'content-type' : 'application/json' ,
@@ -193,7 +194,7 @@ describe('Request', () => {
193
194
it . each ( [ { obj : 'ect' } , 0 , false , [ 'array' ] ] ) (
194
195
'must not allow `%j` for the {operationName} parameter' ,
195
196
async ( invalid ) => {
196
- const res = await fetch ( server . url , {
197
+ const res = await fetch ( serverUrl , {
197
198
method : 'POST' ,
198
199
headers : {
199
200
'content-type' : 'application/json' ,
@@ -208,7 +209,7 @@ describe('Request', () => {
208
209
} ,
209
210
) ;
210
211
it ( 'must accept a string for the {operationName} parameter' , async ( ) => {
211
- const res = await fetch ( server . url , {
212
+ const res = await fetch ( serverUrl , {
212
213
method : 'POST' ,
213
214
headers : {
214
215
'content-type' : 'application/json' ,
@@ -225,7 +226,7 @@ describe('Request', () => {
225
226
it . each ( [ 'string' , 0 , false , [ 'array' ] ] ) (
226
227
'must not allow `%j` for the {variables} parameter' ,
227
228
async ( invalid ) => {
228
- const res = await fetch ( server . url , {
229
+ const res = await fetch ( serverUrl , {
229
230
method : 'POST' ,
230
231
headers : {
231
232
'content-type' : 'application/json' ,
@@ -240,7 +241,7 @@ describe('Request', () => {
240
241
} ,
241
242
) ;
242
243
it ( 'must accept a map for the {variables} parameter' , async ( ) => {
243
- const res = await fetch ( server . url , {
244
+ const res = await fetch ( serverUrl , {
244
245
method : 'POST' ,
245
246
headers : {
246
247
'content-type' : 'application/json' ,
@@ -256,7 +257,7 @@ describe('Request', () => {
256
257
expect ( result ) . not . toHaveProperty ( 'errors' ) ;
257
258
} ) ;
258
259
it ( 'must accept a URL-encoded JSON string for the {variable} parameter in GETs' , async ( ) => {
259
- const url = new URL ( server . url ) ;
260
+ const url = new URL ( serverUrl ) ;
260
261
url . searchParams . set (
261
262
'query' ,
262
263
'query Type($name: String!) { __type(name: $name) { name } }' ,
@@ -273,7 +274,7 @@ describe('Request', () => {
273
274
it . each ( [ 'string' , 0 , false , [ 'array' ] ] ) (
274
275
'must not allow `%j` for the {extensions} parameter' ,
275
276
async ( invalid ) => {
276
- const res = await fetch ( server . url , {
277
+ const res = await fetch ( serverUrl , {
277
278
method : 'POST' ,
278
279
headers : {
279
280
'content-type' : 'application/json' ,
@@ -288,7 +289,7 @@ describe('Request', () => {
288
289
} ,
289
290
) ;
290
291
it ( 'must accept a map for the {extensions} parameter' , async ( ) => {
291
- const res = await fetch ( server . url , {
292
+ const res = await fetch ( serverUrl , {
292
293
method : 'POST' ,
293
294
headers : {
294
295
'content-type' : 'application/json' ,
@@ -310,7 +311,7 @@ describe('Request', () => {
310
311
describe ( 'Response' , ( ) => {
311
312
describe ( 'application/json' , ( ) => {
312
313
it ( 'should use 200 status code on JSON parsing failure' , async ( ) => {
313
- const res = await fetch ( server . url , {
314
+ const res = await fetch ( serverUrl , {
314
315
method : 'POST' ,
315
316
headers : {
316
317
'content-type' : 'application/json' ,
@@ -322,7 +323,7 @@ describe('Response', () => {
322
323
} ) ;
323
324
324
325
it ( 'should use 200 status code if parameters are invalid' , async ( ) => {
325
- const url = new URL ( server . url ) ;
326
+ const url = new URL ( serverUrl ) ;
326
327
url . searchParams . set ( 'qeury' /* typo */ , '{ __typename }' ) ;
327
328
const res = await fetch ( url . toString ( ) , {
328
329
method : 'GET' ,
@@ -332,7 +333,7 @@ describe('Response', () => {
332
333
} ) ;
333
334
334
335
it ( 'should use 200 status code on document parsing failure' , async ( ) => {
335
- const url = new URL ( server . url ) ;
336
+ const url = new URL ( serverUrl ) ;
336
337
url . searchParams . set ( 'query' , '{' ) ;
337
338
const res = await fetch ( url . toString ( ) , {
338
339
method : 'GET' ,
@@ -342,7 +343,7 @@ describe('Response', () => {
342
343
} ) ;
343
344
344
345
it ( 'should use 200 status code on document validation failure' , async ( ) => {
345
- const url = new URL ( server . url ) ;
346
+ const url = new URL ( serverUrl ) ;
346
347
url . searchParams . set ( 'query' , '{ 8f31403dfe404bccbb0e835f2629c6a7 }' ) ; // making sure the field doesnt exist
347
348
const res = await fetch ( url . toString ( ) , {
348
349
method : 'GET' ,
@@ -354,7 +355,7 @@ describe('Response', () => {
354
355
355
356
describe ( 'application/graphql+json' , ( ) => {
356
357
it ( 'must use 4xx or 5xx status codes on JSON parsing failure' , async ( ) => {
357
- const res = await fetch ( server . url , {
358
+ const res = await fetch ( serverUrl , {
358
359
method : 'POST' ,
359
360
headers : {
360
361
'content-type' : 'application/json' ,
@@ -366,7 +367,7 @@ describe('Response', () => {
366
367
expect ( res . status ) . toBeLessThanOrEqual ( 599 ) ;
367
368
} ) ;
368
369
it ( 'should use 400 status code on JSON parsing failure' , async ( ) => {
369
- const res = await fetch ( server . url , {
370
+ const res = await fetch ( serverUrl , {
370
371
method : 'POST' ,
371
372
headers : {
372
373
'content-type' : 'application/json' ,
@@ -378,7 +379,7 @@ describe('Response', () => {
378
379
} ) ;
379
380
380
381
it ( 'must use 4xx or 5xx status codes if parameters are invalid' , async ( ) => {
381
- const url = new URL ( server . url ) ;
382
+ const url = new URL ( serverUrl ) ;
382
383
url . searchParams . set ( 'qeury' /* typo */ , '{ __typename }' ) ;
383
384
const res = await fetch ( url . toString ( ) , {
384
385
method : 'GET' ,
@@ -388,7 +389,7 @@ describe('Response', () => {
388
389
expect ( res . status ) . toBeLessThanOrEqual ( 599 ) ;
389
390
} ) ;
390
391
it ( 'should use 400 status code if parameters are invalid' , async ( ) => {
391
- const url = new URL ( server . url ) ;
392
+ const url = new URL ( serverUrl ) ;
392
393
url . searchParams . set ( 'qeury' /* typo */ , '{ __typename }' ) ;
393
394
const res = await fetch ( url . toString ( ) , {
394
395
method : 'GET' ,
@@ -398,7 +399,7 @@ describe('Response', () => {
398
399
} ) ;
399
400
400
401
it ( 'must use 4xx or 5xx status codes on document parsing failure' , async ( ) => {
401
- const url = new URL ( server . url ) ;
402
+ const url = new URL ( serverUrl ) ;
402
403
url . searchParams . set ( 'query' , '{' ) ;
403
404
const res = await fetch ( url . toString ( ) , {
404
405
method : 'GET' ,
@@ -408,7 +409,7 @@ describe('Response', () => {
408
409
expect ( res . status ) . toBeLessThanOrEqual ( 599 ) ;
409
410
} ) ;
410
411
it ( 'should use 400 status code on document parsing failure' , async ( ) => {
411
- const url = new URL ( server . url ) ;
412
+ const url = new URL ( serverUrl ) ;
412
413
url . searchParams . set ( 'query' , '{' ) ;
413
414
const res = await fetch ( url . toString ( ) , {
414
415
method : 'GET' ,
@@ -418,7 +419,7 @@ describe('Response', () => {
418
419
} ) ;
419
420
420
421
it ( 'must use 4xx or 5xx status codes on document validation failure' , async ( ) => {
421
- const url = new URL ( server . url ) ;
422
+ const url = new URL ( serverUrl ) ;
422
423
url . searchParams . set ( 'query' , '{ 8f31403dfe404bccbb0e835f2629c6a7 }' ) ; // making sure the field doesnt exist
423
424
const res = await fetch ( url . toString ( ) , {
424
425
method : 'GET' ,
@@ -428,7 +429,7 @@ describe('Response', () => {
428
429
expect ( res . status ) . toBeLessThanOrEqual ( 599 ) ;
429
430
} ) ;
430
431
it ( 'should use 400 status code on document validation failure' , async ( ) => {
431
- const url = new URL ( server . url ) ;
432
+ const url = new URL ( serverUrl ) ;
432
433
url . searchParams . set ( 'query' , '{ 8f31403dfe404bccbb0e835f2629c6a7 }' ) ; // making sure the field doesnt exist
433
434
const res = await fetch ( url . toString ( ) , {
434
435
method : 'GET' ,
0 commit comments