-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
320 lines (262 loc) · 6.48 KB
/
test.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
'use strict';
const t = require('tap');
const test = t.test;
const Fastify = require('fastify');
const { loadNuxt } = require('nuxt');
const pino = require('pino');
test('should construct nuxt with proper environment', async (t) => {
t.plan(2);
var app;
var options;
var dev;
process.env.NODE_ENV = 'production';
dev = process.env.NODE_ENV !== 'production';
t.equal(dev, false);
app = await loadNuxt('start');
t.equal(app.options.dev, false);
app.close();
});
test('should return an html document', (t) => {
t.plan(3);
const fastify = Fastify();
t.teardown(() => fastify.close());
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello');
});
fastify.inject(
{
url: '/hello',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/html; charset=utf-8');
}
);
});
test('should support different methods', (t) => {
t.plan(3);
const fastify = Fastify();
t.teardown(() => fastify.close());
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello', { method: 'options' });
});
fastify.inject(
{
url: '/hello',
method: 'OPTIONS',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/html; charset=utf-8');
}
);
});
test('should support a custom handler', (t) => {
t.plan(3);
const fastify = Fastify();
t.teardown(() => fastify.close());
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello', (app, req, reply) => {
app.render(req.raw, reply.raw, '/hello', req.query, {});
});
});
fastify.inject(
{
url: '/hello',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/html; charset=utf-8');
}
);
});
test('should return 404 on undefined route', (t) => {
t.plan(2);
const fastify = Fastify();
t.teardown(() => fastify.close());
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello');
});
fastify.inject(
{
url: '/test',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 404);
}
);
});
test('should throw if path is not a string', (t) => {
t.plan(2);
const fastify = Fastify();
fastify.register(require('./index')).after((err) => {
t.error(err);
try {
fastify.nuxt(null);
t.fail();
} catch (e) {
t.equal(e.message, 'path must be a string');
}
});
fastify.close();
});
test('should throw if opts.method is not a string', (t) => {
t.plan(2);
const fastify = Fastify();
fastify.register(require('./index')).after((err) => {
t.error(err);
try {
fastify.nuxt('/hello', { method: 1 });
t.fail();
} catch (e) {
t.equal(e.message, 'options.method must be a string');
}
});
fastify.close();
});
test('should throw if opts.schema is not an object', (t) => {
t.plan(2);
const fastify = Fastify();
fastify.register(require('./index')).after((err) => {
t.error(err);
try {
fastify.nuxt('/hello', { schema: 1 });
t.fail();
} catch (e) {
t.equal(e.message, 'options.schema must be an object');
}
});
fastify.close();
});
test('should throw if callback is not a function', (t) => {
t.plan(2);
const fastify = Fastify();
fastify.register(require('./index')).after((err) => {
t.error(err);
try {
fastify.nuxt('/hello', {}, 1);
t.fail();
} catch (e) {
t.equal(e.message, 'callback must be a function');
}
});
fastify.close();
});
test('should serve /_nuxt/* static assets', (t) => {
t.plan(9);
const manifest = require('./.nuxt/dist/server/client.manifest.json');
const fastify = Fastify();
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello');
});
t.teardown(() => fastify.close());
const commonAssets = manifest.initial;
commonAssets.map((suffix) => testNuxtAsset(t, fastify, `/_nuxt/${suffix}`));
});
test('should not log any errors', (t) => {
t.plan(5);
let showedError = false;
const logger = pino({
level: 'error',
formatters: {
log: (obj) => {
showedError = true;
return obj;
},
},
});
const fastify = Fastify({
logger,
});
fastify.register(require('./index')).after(() => {
fastify.nuxt('/hello');
});
fastify.inject(
{
url: '/hello',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/html; charset=utf-8');
t.match(res.payload, '<h1 class="title">Hello World</h1>');
t.equal(showedError, false, 'Should not show any error');
}
);
});
test('should respect plugin logLevel', (t) => {
t.plan(5);
let didLog = false;
const logger = pino({
formatters: {
log: (obj) => {
didLog = true;
return obj;
},
},
});
const fastify = Fastify({
logger,
});
fastify
.register(require('./index'), {
logLevel: 'error',
})
.after(() => {
fastify.nuxt('/hello');
});
t.teardown(() => fastify.close());
fastify.inject(
{
url: '/hello',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/html; charset=utf-8');
t.match(res.payload, '<h1 class="title">Hello World</h1>');
t.equal(didLog, false);
}
);
});
test('should preserve Fastify response headers set by plugins and hooks', (t) => {
t.plan(3);
const fastify = Fastify();
t.teardown(() => fastify.close());
fastify.register(require('./index')).after(() => {
fastify.addHook('onRequest', (req, reply, done) => {
reply.header('test-header', 'hello');
done();
});
fastify.nuxt('/hello', (app, req, reply) => {
app.render(req.raw, reply.raw, '/hello', req.query, {});
});
});
fastify.inject(
{
url: '/hello',
method: 'GET',
},
(err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['test-header'], 'hello');
}
);
});
function testNuxtAsset(t, fastify, url) {
fastify.inject({ url, method: 'GET' }, (err, res) => {
t.error(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8');
});
}