1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const { hookRunner, onSendHookRunner } = require('../../lib/hooks')
|
6 |
|
7 | test('hookRunner - Basic', t => {
|
8 | t.plan(9)
|
9 |
|
10 | hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
11 |
|
12 | function iterator (fn, a, b, next) {
|
13 | return fn(a, b, next)
|
14 | }
|
15 |
|
16 | function fn1 (a, b, next) {
|
17 | t.strictEqual(a, 'a')
|
18 | t.strictEqual(b, 'b')
|
19 | next()
|
20 | }
|
21 |
|
22 | function fn2 (a, b, next) {
|
23 | t.strictEqual(a, 'a')
|
24 | t.strictEqual(b, 'b')
|
25 | next()
|
26 | }
|
27 |
|
28 | function fn3 (a, b, next) {
|
29 | t.strictEqual(a, 'a')
|
30 | t.strictEqual(b, 'b')
|
31 | next()
|
32 | }
|
33 |
|
34 | function done (err, a, b) {
|
35 | t.error(err)
|
36 | t.strictEqual(a, 'a')
|
37 | t.strictEqual(b, 'b')
|
38 | }
|
39 | })
|
40 |
|
41 | test('hookRunner - In case of error should skip to done', t => {
|
42 | t.plan(7)
|
43 |
|
44 | hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
45 |
|
46 | function iterator (fn, a, b, next) {
|
47 | return fn(a, b, next)
|
48 | }
|
49 |
|
50 | function fn1 (a, b, next) {
|
51 | t.strictEqual(a, 'a')
|
52 | t.strictEqual(b, 'b')
|
53 | next()
|
54 | }
|
55 |
|
56 | function fn2 (a, b, next) {
|
57 | t.strictEqual(a, 'a')
|
58 | t.strictEqual(b, 'b')
|
59 | next(new Error('kaboom'))
|
60 | }
|
61 |
|
62 | function fn3 () {
|
63 | t.fail('We should not be here')
|
64 | }
|
65 |
|
66 | function done (err, a, b) {
|
67 | t.strictEqual(err.message, 'kaboom')
|
68 | t.strictEqual(a, 'a')
|
69 | t.strictEqual(b, 'b')
|
70 | }
|
71 | })
|
72 |
|
73 | test('hookRunner - Should handle promises', t => {
|
74 | t.plan(9)
|
75 |
|
76 | hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
77 |
|
78 | function iterator (fn, a, b, next) {
|
79 | return fn(a, b, next)
|
80 | }
|
81 |
|
82 | function fn1 (a, b) {
|
83 | t.strictEqual(a, 'a')
|
84 | t.strictEqual(b, 'b')
|
85 | return Promise.resolve()
|
86 | }
|
87 |
|
88 | function fn2 (a, b) {
|
89 | t.strictEqual(a, 'a')
|
90 | t.strictEqual(b, 'b')
|
91 | return Promise.resolve()
|
92 | }
|
93 |
|
94 | function fn3 (a, b) {
|
95 | t.strictEqual(a, 'a')
|
96 | t.strictEqual(b, 'b')
|
97 | return Promise.resolve()
|
98 | }
|
99 |
|
100 | function done (err, a, b) {
|
101 | t.error(err)
|
102 | t.strictEqual(a, 'a')
|
103 | t.strictEqual(b, 'b')
|
104 | }
|
105 | })
|
106 |
|
107 | test('hookRunner - In case of error should skip to done (with promises)', t => {
|
108 | t.plan(7)
|
109 |
|
110 | hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
111 |
|
112 | function iterator (fn, a, b, next) {
|
113 | return fn(a, b, next)
|
114 | }
|
115 |
|
116 | function fn1 (a, b) {
|
117 | t.strictEqual(a, 'a')
|
118 | t.strictEqual(b, 'b')
|
119 | return Promise.resolve()
|
120 | }
|
121 |
|
122 | function fn2 (a, b) {
|
123 | t.strictEqual(a, 'a')
|
124 | t.strictEqual(b, 'b')
|
125 | return Promise.reject(new Error('kaboom'))
|
126 | }
|
127 |
|
128 | function fn3 () {
|
129 | t.fail('We should not be here')
|
130 | }
|
131 |
|
132 | function done (err, a, b) {
|
133 | t.strictEqual(err.message, 'kaboom')
|
134 | t.strictEqual(a, 'a')
|
135 | t.strictEqual(b, 'b')
|
136 | }
|
137 | })
|
138 |
|
139 | test('hookRunner - Be able to exit before its natural end', t => {
|
140 | t.plan(4)
|
141 |
|
142 | var shouldStop = false
|
143 | hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
144 |
|
145 | function iterator (fn, a, b, next) {
|
146 | if (shouldStop) {
|
147 | return undefined
|
148 | }
|
149 | return fn(a, b, next)
|
150 | }
|
151 |
|
152 | function fn1 (a, b, next) {
|
153 | t.strictEqual(a, 'a')
|
154 | t.strictEqual(b, 'b')
|
155 | next()
|
156 | }
|
157 |
|
158 | function fn2 (a, b) {
|
159 | t.strictEqual(a, 'a')
|
160 | t.strictEqual(b, 'b')
|
161 | shouldStop = true
|
162 | return Promise.resolve()
|
163 | }
|
164 |
|
165 | function fn3 () {
|
166 | t.fail('this should not be called')
|
167 | }
|
168 |
|
169 | function done () {
|
170 | t.fail('this should not be called')
|
171 | }
|
172 | })
|
173 |
|
174 | test('hookRunner - Promises that resolve to a value do not change the state', t => {
|
175 | t.plan(5)
|
176 |
|
177 | const originalState = { a: 'a', b: 'b' }
|
178 |
|
179 | hookRunner([fn1, fn2, fn3], iterator, originalState, 'b', done)
|
180 |
|
181 | function iterator (fn, state, b, next) {
|
182 | return fn(state, b, next)
|
183 | }
|
184 |
|
185 | function fn1 (state, b, next) {
|
186 | t.strictEqual(state, originalState)
|
187 | return Promise.resolve(null)
|
188 | }
|
189 |
|
190 | function fn2 (state, b, next) {
|
191 | t.strictEqual(state, originalState)
|
192 | return Promise.resolve('string')
|
193 | }
|
194 |
|
195 | function fn3 (state, b, next) {
|
196 | t.strictEqual(state, originalState)
|
197 | return Promise.resolve({ object: true })
|
198 | }
|
199 |
|
200 | function done (err, state, b) {
|
201 | t.error(err)
|
202 | t.strictEqual(state, originalState)
|
203 | }
|
204 | })
|
205 |
|
206 | test('onSendHookRunner - Basic', t => {
|
207 | t.plan(13)
|
208 |
|
209 | const originalRequest = { body: null }
|
210 | const originalReply = { request: originalRequest }
|
211 | const originalPayload = 'payload'
|
212 |
|
213 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, originalPayload, done)
|
214 |
|
215 | function fn1 (request, reply, payload, next) {
|
216 | t.deepEqual(request, originalRequest)
|
217 | t.deepEqual(reply, originalReply)
|
218 | t.strictEqual(payload, originalPayload)
|
219 | next()
|
220 | }
|
221 |
|
222 | function fn2 (request, reply, payload, next) {
|
223 | t.deepEqual(request, originalRequest)
|
224 | t.deepEqual(reply, originalReply)
|
225 | t.strictEqual(payload, originalPayload)
|
226 | next()
|
227 | }
|
228 |
|
229 | function fn3 (request, reply, payload, next) {
|
230 | t.deepEqual(request, originalRequest)
|
231 | t.deepEqual(reply, originalReply)
|
232 | t.strictEqual(payload, originalPayload)
|
233 | next()
|
234 | }
|
235 |
|
236 | function done (err, request, reply, payload) {
|
237 | t.error(err)
|
238 | t.deepEqual(request, originalRequest)
|
239 | t.deepEqual(reply, originalReply)
|
240 | t.strictEqual(payload, originalPayload)
|
241 | }
|
242 | })
|
243 |
|
244 | test('onSendHookRunner - Can change the payload', t => {
|
245 | t.plan(7)
|
246 |
|
247 | const originalRequest = { body: null }
|
248 | const originalReply = { request: originalRequest }
|
249 | const v1 = { hello: 'world' }
|
250 | const v2 = { ciao: 'mondo' }
|
251 | const v3 = { winter: 'is coming' }
|
252 | const v4 = { winter: 'has come' }
|
253 |
|
254 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)
|
255 |
|
256 | function fn1 (request, reply, payload, next) {
|
257 | t.deepEqual(payload, v1)
|
258 | next(null, v2)
|
259 | }
|
260 |
|
261 | function fn2 (request, reply, payload, next) {
|
262 | t.deepEqual(payload, v2)
|
263 | next(null, v3)
|
264 | }
|
265 |
|
266 | function fn3 (request, reply, payload, next) {
|
267 | t.deepEqual(payload, v3)
|
268 | next(null, v4)
|
269 | }
|
270 |
|
271 | function done (err, request, reply, payload) {
|
272 | t.error(err)
|
273 | t.deepEqual(request, originalRequest)
|
274 | t.deepEqual(reply, originalReply)
|
275 | t.deepEqual(payload, v4)
|
276 | }
|
277 | })
|
278 |
|
279 | test('onSendHookRunner - In case of error should skip to done', t => {
|
280 | t.plan(6)
|
281 |
|
282 | const originalRequest = { body: null }
|
283 | const originalReply = { request: originalRequest }
|
284 | const v1 = { hello: 'world' }
|
285 | const v2 = { ciao: 'mondo' }
|
286 |
|
287 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)
|
288 |
|
289 | function fn1 (request, reply, payload, next) {
|
290 | t.deepEqual(payload, v1)
|
291 | next(null, v2)
|
292 | }
|
293 |
|
294 | function fn2 (request, reply, payload, next) {
|
295 | t.deepEqual(payload, v2)
|
296 | next(new Error('kaboom'))
|
297 | }
|
298 |
|
299 | function fn3 () {
|
300 | t.fail('We should not be here')
|
301 | }
|
302 |
|
303 | function done (err, request, reply, payload) {
|
304 | t.strictEqual(err.message, 'kaboom')
|
305 | t.deepEqual(request, originalRequest)
|
306 | t.deepEqual(reply, originalReply)
|
307 | t.deepEqual(payload, v2)
|
308 | }
|
309 | })
|
310 |
|
311 | test('onSendHookRunner - Should handle promises', t => {
|
312 | t.plan(7)
|
313 |
|
314 | const originalRequest = { body: null }
|
315 | const originalReply = { request: originalRequest }
|
316 | const v1 = { hello: 'world' }
|
317 | const v2 = { ciao: 'mondo' }
|
318 | const v3 = { winter: 'is coming' }
|
319 | const v4 = { winter: 'has come' }
|
320 |
|
321 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)
|
322 |
|
323 | function fn1 (request, reply, payload) {
|
324 | t.deepEqual(payload, v1)
|
325 | return Promise.resolve(v2)
|
326 | }
|
327 |
|
328 | function fn2 (request, reply, payload) {
|
329 | t.deepEqual(payload, v2)
|
330 | return Promise.resolve(v3)
|
331 | }
|
332 |
|
333 | function fn3 (request, reply, payload) {
|
334 | t.deepEqual(payload, v3)
|
335 | return Promise.resolve(v4)
|
336 | }
|
337 |
|
338 | function done (err, request, reply, payload) {
|
339 | t.error(err)
|
340 | t.deepEqual(request, originalRequest)
|
341 | t.deepEqual(reply, originalReply)
|
342 | t.deepEqual(payload, v4)
|
343 | }
|
344 | })
|
345 |
|
346 | test('onSendHookRunner - In case of error should skip to done (with promises)', t => {
|
347 | t.plan(6)
|
348 |
|
349 | const originalRequest = { body: null }
|
350 | const originalReply = { request: originalRequest }
|
351 | const v1 = { hello: 'world' }
|
352 | const v2 = { ciao: 'mondo' }
|
353 |
|
354 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)
|
355 |
|
356 | function fn1 (request, reply, payload) {
|
357 | t.deepEqual(payload, v1)
|
358 | return Promise.resolve(v2)
|
359 | }
|
360 |
|
361 | function fn2 (request, reply, payload) {
|
362 | t.deepEqual(payload, v2)
|
363 | return Promise.reject(new Error('kaboom'))
|
364 | }
|
365 |
|
366 | function fn3 () {
|
367 | t.fail('We should not be here')
|
368 | }
|
369 |
|
370 | function done (err, request, reply, payload) {
|
371 | t.strictEqual(err.message, 'kaboom')
|
372 | t.deepEqual(request, originalRequest)
|
373 | t.deepEqual(reply, originalReply)
|
374 | t.deepEqual(payload, v2)
|
375 | }
|
376 | })
|
377 |
|
378 | test('onSendHookRunner - Be able to exit before its natural end', t => {
|
379 | t.plan(2)
|
380 |
|
381 | const originalRequest = { body: null }
|
382 | const originalReply = { request: originalRequest }
|
383 | const v1 = { hello: 'world' }
|
384 | const v2 = { ciao: 'mondo' }
|
385 |
|
386 | onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)
|
387 |
|
388 | function fn1 (request, reply, payload, next) {
|
389 | t.deepEqual(payload, v1)
|
390 | next(null, v2)
|
391 | }
|
392 |
|
393 | function fn2 (request, reply, payload) {
|
394 | t.deepEqual(payload, v2)
|
395 | }
|
396 |
|
397 | function fn3 () {
|
398 | t.fail('this should not be called')
|
399 | }
|
400 |
|
401 | function done () {
|
402 | t.fail('this should not be called')
|
403 | }
|
404 | })
|