Skip to content

Commit d0cd67b

Browse files
committed
refactor: serverUrl for compliance tests
1 parent 3cd0d50 commit d0cd67b

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/__tests__/server.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import fetch from 'node-fetch';
22

33
import { schema } from './fixtures/simple';
4-
import { bodyAsExecResult, startTServer, TServer } from './utils/tserver';
4+
import { bodyAsExecResult, startTServer } from './utils/tserver';
55

6-
let server!: TServer;
6+
let serverUrl!: string;
77
beforeAll(() => {
8-
server = startTServer({ schema });
8+
const server = startTServer({ schema });
9+
serverUrl = server.url;
910
});
1011

1112
describe('Media Types', () => {
1213
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);
1415
url.searchParams.set('query', '{ __typename }');
1516

1617
const res = await fetch(url.toString(), {
@@ -25,7 +26,7 @@ describe('Media Types', () => {
2526
});
2627

2728
it('must accept application/json and match the content-type', async () => {
28-
const url = new URL(server.url);
29+
const url = new URL(serverUrl);
2930
url.searchParams.set('query', '{ __typename }');
3031

3132
const res = await fetch(url.toString(), {
@@ -38,7 +39,7 @@ describe('Media Types', () => {
3839
});
3940

4041
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);
4243
url.searchParams.set('query', '{ __typename }');
4344

4445
const res = await fetch(url.toString(), {
@@ -53,7 +54,7 @@ describe('Media Types', () => {
5354
});
5455

5556
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);
5758
url.searchParams.set('query', '{ __typename }');
5859

5960
const res = await fetch(url.toString());
@@ -64,7 +65,7 @@ describe('Media Types', () => {
6465
});
6566

6667
it('must use utf-8 charset in response', async () => {
67-
const url = new URL(server.url);
68+
const url = new URL(serverUrl);
6869
url.searchParams.set('query', '{ __typename }');
6970

7071
const res = await fetch(url.toString());
@@ -73,7 +74,7 @@ describe('Media Types', () => {
7374
});
7475

7576
it('must accept only utf-8 charset', async () => {
76-
const url = new URL(server.url);
77+
const url = new URL(serverUrl);
7778
url.searchParams.set('query', '{ __typename }');
7879

7980
const res = await fetch(url.toString(), {
@@ -88,7 +89,7 @@ describe('Media Types', () => {
8889

8990
describe('Request', () => {
9091
it('must accept POST requests', async () => {
91-
const res = await fetch(server.url, {
92+
const res = await fetch(serverUrl, {
9293
method: 'POST',
9394
headers: { 'content-type': 'application/json' },
9495
body: JSON.stringify({ query: '{ __typename }' }),
@@ -97,7 +98,7 @@ describe('Request', () => {
9798
});
9899

99100
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);
101102
url.searchParams.set('query', '{ __typename }');
102103

103104
const res = await fetch(url.toString());
@@ -106,7 +107,7 @@ describe('Request', () => {
106107

107108
describe('GET', () => {
108109
it('must not allow executing mutations', async () => {
109-
const url = new URL(server.url);
110+
const url = new URL(serverUrl);
110111
url.searchParams.set(
111112
'query',
112113
'mutation { f10d019f15804f92a7c7470205c866da }', // making sure the field doesnt exist
@@ -123,15 +124,15 @@ describe('Request', () => {
123124

124125
describe('POST', () => {
125126
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, {
127128
method: 'POST',
128129
});
129130
expect(res.status).toBeGreaterThanOrEqual(400);
130131
expect(res.status).toBeLessThanOrEqual(599);
131132
});
132133

133134
it('must accept application/json requests', async () => {
134-
const res = await fetch(server.url, {
135+
const res = await fetch(serverUrl, {
135136
method: 'POST',
136137
headers: { 'content-type': 'application/json' },
137138
body: JSON.stringify({ query: '{ __typename }' }),
@@ -140,7 +141,7 @@ describe('Request', () => {
140141
});
141142

142143
it('must require a request body', async () => {
143-
const res = await fetch(server.url, {
144+
const res = await fetch(serverUrl, {
144145
method: 'POST',
145146
headers: { 'content-type': 'application/json' },
146147
});
@@ -150,7 +151,7 @@ describe('Request', () => {
150151

151152
describe('Request Parameters', () => {
152153
it('must require the {query} parameter', async () => {
153-
const res = await fetch(server.url, {
154+
const res = await fetch(serverUrl, {
154155
method: 'POST',
155156
headers: {
156157
'content-type': 'application/json',
@@ -163,7 +164,7 @@ describe('Request', () => {
163164
it.each([{ obj: 'ect' }, 0, false, ['array']])(
164165
'must not allow `%j` for the {query} parameter',
165166
async (invalid) => {
166-
const res = await fetch(server.url, {
167+
const res = await fetch(serverUrl, {
167168
method: 'POST',
168169
headers: {
169170
'content-type': 'application/json',
@@ -177,7 +178,7 @@ describe('Request', () => {
177178
},
178179
);
179180
it('must accept a string for the {query} parameter', async () => {
180-
const res = await fetch(server.url, {
181+
const res = await fetch(serverUrl, {
181182
method: 'POST',
182183
headers: {
183184
'content-type': 'application/json',
@@ -193,7 +194,7 @@ describe('Request', () => {
193194
it.each([{ obj: 'ect' }, 0, false, ['array']])(
194195
'must not allow `%j` for the {operationName} parameter',
195196
async (invalid) => {
196-
const res = await fetch(server.url, {
197+
const res = await fetch(serverUrl, {
197198
method: 'POST',
198199
headers: {
199200
'content-type': 'application/json',
@@ -208,7 +209,7 @@ describe('Request', () => {
208209
},
209210
);
210211
it('must accept a string for the {operationName} parameter', async () => {
211-
const res = await fetch(server.url, {
212+
const res = await fetch(serverUrl, {
212213
method: 'POST',
213214
headers: {
214215
'content-type': 'application/json',
@@ -225,7 +226,7 @@ describe('Request', () => {
225226
it.each(['string', 0, false, ['array']])(
226227
'must not allow `%j` for the {variables} parameter',
227228
async (invalid) => {
228-
const res = await fetch(server.url, {
229+
const res = await fetch(serverUrl, {
229230
method: 'POST',
230231
headers: {
231232
'content-type': 'application/json',
@@ -240,7 +241,7 @@ describe('Request', () => {
240241
},
241242
);
242243
it('must accept a map for the {variables} parameter', async () => {
243-
const res = await fetch(server.url, {
244+
const res = await fetch(serverUrl, {
244245
method: 'POST',
245246
headers: {
246247
'content-type': 'application/json',
@@ -256,7 +257,7 @@ describe('Request', () => {
256257
expect(result).not.toHaveProperty('errors');
257258
});
258259
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);
260261
url.searchParams.set(
261262
'query',
262263
'query Type($name: String!) { __type(name: $name) { name } }',
@@ -273,7 +274,7 @@ describe('Request', () => {
273274
it.each(['string', 0, false, ['array']])(
274275
'must not allow `%j` for the {extensions} parameter',
275276
async (invalid) => {
276-
const res = await fetch(server.url, {
277+
const res = await fetch(serverUrl, {
277278
method: 'POST',
278279
headers: {
279280
'content-type': 'application/json',
@@ -288,7 +289,7 @@ describe('Request', () => {
288289
},
289290
);
290291
it('must accept a map for the {extensions} parameter', async () => {
291-
const res = await fetch(server.url, {
292+
const res = await fetch(serverUrl, {
292293
method: 'POST',
293294
headers: {
294295
'content-type': 'application/json',
@@ -310,7 +311,7 @@ describe('Request', () => {
310311
describe('Response', () => {
311312
describe('application/json', () => {
312313
it('should use 200 status code on JSON parsing failure', async () => {
313-
const res = await fetch(server.url, {
314+
const res = await fetch(serverUrl, {
314315
method: 'POST',
315316
headers: {
316317
'content-type': 'application/json',
@@ -322,7 +323,7 @@ describe('Response', () => {
322323
});
323324

324325
it('should use 200 status code if parameters are invalid', async () => {
325-
const url = new URL(server.url);
326+
const url = new URL(serverUrl);
326327
url.searchParams.set('qeury' /* typo */, '{ __typename }');
327328
const res = await fetch(url.toString(), {
328329
method: 'GET',
@@ -332,7 +333,7 @@ describe('Response', () => {
332333
});
333334

334335
it('should use 200 status code on document parsing failure', async () => {
335-
const url = new URL(server.url);
336+
const url = new URL(serverUrl);
336337
url.searchParams.set('query', '{');
337338
const res = await fetch(url.toString(), {
338339
method: 'GET',
@@ -342,7 +343,7 @@ describe('Response', () => {
342343
});
343344

344345
it('should use 200 status code on document validation failure', async () => {
345-
const url = new URL(server.url);
346+
const url = new URL(serverUrl);
346347
url.searchParams.set('query', '{ 8f31403dfe404bccbb0e835f2629c6a7 }'); // making sure the field doesnt exist
347348
const res = await fetch(url.toString(), {
348349
method: 'GET',
@@ -354,7 +355,7 @@ describe('Response', () => {
354355

355356
describe('application/graphql+json', () => {
356357
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, {
358359
method: 'POST',
359360
headers: {
360361
'content-type': 'application/json',
@@ -366,7 +367,7 @@ describe('Response', () => {
366367
expect(res.status).toBeLessThanOrEqual(599);
367368
});
368369
it('should use 400 status code on JSON parsing failure', async () => {
369-
const res = await fetch(server.url, {
370+
const res = await fetch(serverUrl, {
370371
method: 'POST',
371372
headers: {
372373
'content-type': 'application/json',
@@ -378,7 +379,7 @@ describe('Response', () => {
378379
});
379380

380381
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);
382383
url.searchParams.set('qeury' /* typo */, '{ __typename }');
383384
const res = await fetch(url.toString(), {
384385
method: 'GET',
@@ -388,7 +389,7 @@ describe('Response', () => {
388389
expect(res.status).toBeLessThanOrEqual(599);
389390
});
390391
it('should use 400 status code if parameters are invalid', async () => {
391-
const url = new URL(server.url);
392+
const url = new URL(serverUrl);
392393
url.searchParams.set('qeury' /* typo */, '{ __typename }');
393394
const res = await fetch(url.toString(), {
394395
method: 'GET',
@@ -398,7 +399,7 @@ describe('Response', () => {
398399
});
399400

400401
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);
402403
url.searchParams.set('query', '{');
403404
const res = await fetch(url.toString(), {
404405
method: 'GET',
@@ -408,7 +409,7 @@ describe('Response', () => {
408409
expect(res.status).toBeLessThanOrEqual(599);
409410
});
410411
it('should use 400 status code on document parsing failure', async () => {
411-
const url = new URL(server.url);
412+
const url = new URL(serverUrl);
412413
url.searchParams.set('query', '{');
413414
const res = await fetch(url.toString(), {
414415
method: 'GET',
@@ -418,7 +419,7 @@ describe('Response', () => {
418419
});
419420

420421
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);
422423
url.searchParams.set('query', '{ 8f31403dfe404bccbb0e835f2629c6a7 }'); // making sure the field doesnt exist
423424
const res = await fetch(url.toString(), {
424425
method: 'GET',
@@ -428,7 +429,7 @@ describe('Response', () => {
428429
expect(res.status).toBeLessThanOrEqual(599);
429430
});
430431
it('should use 400 status code on document validation failure', async () => {
431-
const url = new URL(server.url);
432+
const url = new URL(serverUrl);
432433
url.searchParams.set('query', '{ 8f31403dfe404bccbb0e835f2629c6a7 }'); // making sure the field doesnt exist
433434
const res = await fetch(url.toString(), {
434435
method: 'GET',

0 commit comments

Comments
 (0)