forked from graphql/graphql-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse.ts
112 lines (95 loc) · 2.97 KB
/
use.ts
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
import { fetch } from '@whatwg-node/fetch';
import http from 'http';
import express from 'express';
import fastify from 'fastify';
import Koa from 'koa';
import mount from 'koa-mount';
import { createServerAdapter } from '@whatwg-node/server';
import { startDisposableServer } from './utils/tserver';
import { serverAudits } from '../audits';
import { schema } from './fixtures/simple';
import { createHandler as createNodeHandler } from '../use/node';
import { createHandler as createExpressHandler } from '../use/express';
import { createHandler as createFastifyHandler } from '../use/fastify';
import { createHandler as createFetchHandler } from '../use/fetch';
import { createHandler as createKoaHandler } from '../use/koa';
describe('node', () => {
const [url, dispose] = startDisposableServer(
http.createServer(createNodeHandler({ schema })),
);
afterAll(dispose);
for (const audit of serverAudits({ url, fetchFn: fetch })) {
it(audit.name, async () => {
const result = await audit.fn();
if (result.status !== 'ok') {
throw result.reason;
}
});
}
});
describe('express', () => {
const app = express();
app.all('/', createExpressHandler({ schema }));
const [url, dispose] = startDisposableServer(app.listen(0));
afterAll(dispose);
for (const audit of serverAudits({ url, fetchFn: fetch })) {
it(audit.name, async () => {
const result = await audit.fn();
if (result.status !== 'ok') {
throw result.reason;
}
});
}
});
describe('fastify', () => {
const app = fastify();
// otherwise will throw error code 400 when json data is malformed
app.addContentTypeParser(
'application/json',
{ parseAs: 'string' },
function (_, data, done) {
done(null, String(data));
},
);
app.all('/', createFastifyHandler({ schema }));
// call ready since we're not calling listen
app.ready();
const [url, dispose] = startDisposableServer(app.server);
afterAll(dispose);
for (const audit of serverAudits({ url, fetchFn: fetch })) {
it(audit.name, async () => {
const result = await audit.fn();
if (result.status !== 'ok') {
throw result.reason;
}
});
}
});
describe('fetch', () => {
const [url, dispose] = startDisposableServer(
http.createServer(createServerAdapter(createFetchHandler({ schema }))),
);
afterAll(dispose);
for (const audit of serverAudits({ url, fetchFn: fetch })) {
it(audit.name, async () => {
const result = await audit.fn();
if (result.status !== 'ok') {
throw result.reason;
}
});
}
});
describe('koa', () => {
const app = new Koa();
app.use(mount('/', createKoaHandler({ schema })));
const [url, dispose] = startDisposableServer(app.listen(0));
afterAll(dispose);
for (const audit of serverAudits({ url, fetchFn: fetch })) {
it(audit.name, async () => {
const result = await audit.fn();
if (result.status !== 'ok') {
throw result.reason;
}
});
}
});