-
Notifications
You must be signed in to change notification settings - Fork 13
/
create-test-server.js
244 lines (190 loc) · 6.23 KB
/
create-test-server.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import querystring from 'querystring';
import test from 'ava';
import got from 'got';
import createTestServer from '../';
test('createTestServer is a function', t => {
t.is(typeof createTestServer, 'function');
});
test('createTestServer returns a Promise', t => {
t.true(createTestServer() instanceof Promise);
});
test('server instance exposes useful properties', async t => {
const server = await createTestServer();
t.true(typeof server.port === 'number');
t.true(typeof server.sslPort === 'number');
t.true(typeof server.url === 'string');
t.true(typeof server.sslUrl === 'string');
t.true(typeof server.caCert === 'string');
t.true(typeof server.listen === 'function');
t.true(typeof server.close === 'function');
});
test('express endpoint', async t => {
const server = await createTestServer();
server.get('/foo', (req, res) => {
res.send('bar');
});
const { body } = await got(server.url + '/foo');
t.is(body, 'bar');
});
test('server can be stopped and restarted', async t => {
const server = await createTestServer();
server.get('/foo', (req, res) => {
res.send('bar');
});
t.plan(3);
const { body } = await got(server.url + '/foo');
t.is(body, 'bar');
const closedUrl = server.url;
await server.close();
await got(closedUrl + '/foo', { timeout: 100 }).catch(err => {
t.is(err.code, 'ETIMEDOUT');
});
await server.listen();
const { body: bodyRestarted } = await got(server.url + '/foo');
t.is(bodyRestarted, 'bar');
});
test('server uses a new port on each listen', async t => {
const server = await createTestServer();
const origPort = server.port;
await server.close();
await server.listen();
t.not(origPort, server.port);
});
test('server listens for SSL traffic', async t => {
const server = await createTestServer();
server.get('/foo', (req, res) => {
res.send('bar');
});
const { body } = await got(server.sslUrl + '/foo', { rejectUnauthorized: false });
t.is(body, 'bar');
});
test('server automatically parses JSON request body', async t => {
const server = await createTestServer();
const object = { foo: 'bar' };
server.post('/echo', (req, res) => {
t.deepEqual(req.body, object);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/json' },
body: JSON.stringify(object)
});
});
test('server automatically parses text request body', async t => {
const server = await createTestServer();
const text = 'foo';
server.post('/echo', (req, res) => {
t.deepEqual(req.body, text);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'text/plain' },
body: text
});
});
test('server automatically parses URL-encoded form request body', async t => {
const server = await createTestServer();
const object = { foo: 'bar' };
server.post('/echo', (req, res) => {
t.deepEqual(req.body, object);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: querystring.stringify(object)
});
});
test('server automatically parses binary request body', async t => {
const server = await createTestServer();
const buffer = Buffer.from('foo');
server.post('/echo', (req, res) => {
t.deepEqual(req.body, buffer);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/octet-stream' },
body: buffer
});
});
test('opts.certificate is passed through to createCert()', async t => {
const server = await createTestServer({ certificate: 'foo.bar' });
server.get('/foo', (req, res) => {
res.send('bar');
});
const { body } = await got(server.sslUrl + '/foo', {
ca: server.caCert,
headers: { host: 'foo.bar' }
});
t.is(body, 'bar');
});
test('opts.bodyParser is passed through to bodyParser', async t => {
const smallServer = await createTestServer({ bodyParser: { limit: '100kb' } });
const bigServer = await createTestServer({ bodyParser: { limit: '200kb' } });
const buf = Buffer.alloc(150 * 1024);
// Custom error handler so we don't dump the stack trace in the test output
smallServer.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
res.status(500).end();
});
t.plan(3);
smallServer.post('/', (req, res) => {
t.fail();
res.end();
});
bigServer.post('/', (req, res) => {
t.true(req.body.length === buf.length);
res.end();
});
await t.throws(got.post(smallServer.url, {
headers: { 'content-type': 'application/octet-stream' },
body: buf
}));
await t.notThrows(got.post(bigServer.url, {
headers: { 'content-type': 'application/octet-stream' },
body: buf
}));
});
test('if opts.bodyParser is false body parsing middleware is disabled', async t => {
const server = await createTestServer({ bodyParser: false });
const text = 'foo';
server.post('/echo', (req, res) => {
t.deepEqual(req.body, undefined);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'text/plain' },
body: text
});
});
test('support returning body directly', async t => {
const server = await createTestServer();
server.get('/foo', () => 'bar');
server.get('/bar', () => ({ foo: 'bar' }));
server.get('/async', () => Promise.resolve('bar'));
const bodyString = (await got(server.url + '/foo')).body;
const bodyJson = (await got(server.url + '/bar', { json: true })).body;
const bodyAsync = (await got(server.url + '/async')).body;
t.deepEqual(bodyString, 'bar');
t.deepEqual(bodyJson, { foo: 'bar' });
t.deepEqual(bodyAsync, 'bar');
});
test('support returning body directly without wrapping in function', async t => {
const server = await createTestServer();
server.get('/foo', 'bar');
server.get('/bar', ({ foo: 'bar' }));
server.get('/async', Promise.resolve('bar'));
const bodyString = (await got(server.url + '/foo')).body;
const bodyJson = (await got(server.url + '/bar', { json: true })).body;
const bodyAsync = (await got(server.url + '/async')).body;
t.deepEqual(bodyString, 'bar');
t.deepEqual(bodyJson, { foo: 'bar' });
t.deepEqual(bodyAsync, 'bar');
});
test('accepts multiple callbacks in `.get()`', async t => {
const server = await createTestServer();
server.get('/foo', (req, res, next) => {
res.set('foo', 'bar');
next();
}, (req, res) => res.get('foo'));
const { body } = await got(server.url + '/foo');
t.is(body, 'bar');
});