@@ -7,6 +7,7 @@ const Net = require('net');
7
7
const Path = require ( 'path' ) ;
8
8
const Zlib = require ( 'zlib' ) ;
9
9
10
+ const Boom = require ( '@hapi/boom' ) ;
10
11
const Code = require ( '@hapi/code' ) ;
11
12
const Hapi = require ( '..' ) ;
12
13
const Hoek = require ( '@hapi/hoek' ) ;
@@ -309,6 +310,123 @@ describe('Payload', () => {
309
310
await server . stop ( ) ;
310
311
} ) ;
311
312
313
+ it ( 'does not continue on errors before payload processing' , async ( ) => {
314
+
315
+ const server = Hapi . server ( ) ;
316
+ server . route ( { method : 'POST' , path : '/' , handler : ( request ) => request . payload } ) ;
317
+ server . ext ( 'onPreAuth' , ( request , h ) => {
318
+
319
+ throw new Boom . forbidden ( ) ;
320
+ } ) ;
321
+
322
+ await server . start ( ) ;
323
+
324
+ const client = Net . connect ( server . info . port ) ;
325
+
326
+ await Events . once ( client , 'connect' ) ;
327
+
328
+ client . write ( 'POST / HTTP/1.1\r\nexpect: 100-continue\r\nhost: host\r\naccept-encoding: gzip\r\n' +
329
+ 'content-type: application/json\r\ncontent-length: 14\r\nConnection: close\r\n\r\n' ) ;
330
+
331
+ let continued = false ;
332
+ const lines = [ ] ;
333
+ client . setEncoding ( 'ascii' ) ;
334
+ for await ( const chunk of client ) {
335
+
336
+ if ( chunk . startsWith ( 'HTTP/1.1 100 Continue' ) ) {
337
+ client . write ( '{"hello":true}' ) ;
338
+ continued = true ;
339
+ }
340
+ else {
341
+ lines . push ( ...chunk . split ( '\r\n' ) ) ;
342
+ }
343
+ }
344
+
345
+ const res = lines . shift ( ) ;
346
+
347
+ expect ( res ) . to . equal ( 'HTTP/1.1 403 Forbidden' ) ;
348
+ expect ( continued ) . to . be . false ( ) ;
349
+
350
+ await server . stop ( ) ;
351
+ } ) ;
352
+
353
+ it ( 'handles expect 100-continue on undefined routes' , async ( ) => {
354
+
355
+ const server = Hapi . server ( ) ;
356
+ await server . start ( ) ;
357
+
358
+ const client = Net . connect ( server . info . port ) ;
359
+
360
+ await Events . once ( client , 'connect' ) ;
361
+
362
+ client . write ( 'POST / HTTP/1.1\r\nexpect: 100-continue\r\nhost: host\r\naccept-encoding: gzip\r\n' +
363
+ 'content-type: application/json\r\ncontent-length: 14\r\nConnection: close\r\n\r\n' ) ;
364
+
365
+ let continued = false ;
366
+ const lines = [ ] ;
367
+ client . setEncoding ( 'ascii' ) ;
368
+ for await ( const chunk of client ) {
369
+
370
+ if ( chunk . startsWith ( 'HTTP/1.1 100 Continue' ) ) {
371
+ client . write ( '{"hello":true}' ) ;
372
+ continued = true ;
373
+ }
374
+ else {
375
+ lines . push ( ...chunk . split ( '\r\n' ) ) ;
376
+ }
377
+ }
378
+
379
+ const res = lines . shift ( ) ;
380
+
381
+ expect ( res ) . to . equal ( 'HTTP/1.1 404 Not Found' ) ;
382
+ expect ( continued ) . to . be . false ( ) ;
383
+
384
+ await server . stop ( ) ;
385
+ } ) ;
386
+
387
+ it ( 'does not continue on custom request.payload' , async ( ) => {
388
+
389
+ const server = Hapi . server ( ) ;
390
+ server . route ( { method : 'POST' , path : '/' , handler : ( request ) => request . payload } ) ;
391
+ server . ext ( 'onRequest' , ( request , h ) => {
392
+
393
+ request . payload = { custom : true } ;
394
+ return h . continue ;
395
+ } ) ;
396
+
397
+ await server . start ( ) ;
398
+
399
+ const client = Net . connect ( server . info . port ) ;
400
+
401
+ await Events . once ( client , 'connect' ) ;
402
+
403
+ client . write ( 'POST / HTTP/1.1\r\nexpect: 100-continue\r\nhost: host\r\naccept-encoding: gzip\r\n' +
404
+ 'content-type: application/json\r\ncontent-length: 14\r\nConnection: close\r\n\r\n' ) ;
405
+
406
+ let continued = false ;
407
+ const lines = [ ] ;
408
+ client . setEncoding ( 'ascii' ) ;
409
+ for await ( const chunk of client ) {
410
+
411
+ if ( chunk . startsWith ( 'HTTP/1.1 100 Continue' ) ) {
412
+ client . write ( '{"hello":true}' ) ;
413
+ continued = true ;
414
+ }
415
+ else {
416
+ lines . push ( ...chunk . split ( '\r\n' ) ) ;
417
+ }
418
+ }
419
+
420
+ const res = lines . shift ( ) ;
421
+ const payload = lines . pop ( ) ;
422
+
423
+ expect ( res ) . to . equal ( 'HTTP/1.1 200 OK' ) ;
424
+ expect ( payload ) . to . equal ( '{"custom":true}' ) ;
425
+ expect ( continued ) . to . be . false ( ) ;
426
+
427
+ await server . stop ( ) ;
428
+ } ) ;
429
+
312
430
it ( 'peeks at unparsed data' , async ( ) => {
313
431
314
432
let data = null ;
0 commit comments