|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const net = require('net') |
| 4 | +const stream = require('stream') |
3 | 5 | const Code = require('@hapi/code')
|
4 | 6 | const Lab = require('@hapi/lab')
|
5 | 7 | const Hoek = require('@hapi/hoek')
|
@@ -290,6 +292,56 @@ experiment('logs each request', () => {
|
290 | 292 | await finish
|
291 | 293 | })
|
292 | 294 |
|
| 295 | + test('correctly set the status code on requests aborted during response payload processing', async (flags) => { |
| 296 | + const opts = { host: '127.0.0.1', port: 3000 } |
| 297 | + const server = Hapi.server(opts) |
| 298 | + |
| 299 | + let done |
| 300 | + const finish = new Promise(function (resolve, reject) { |
| 301 | + done = resolve |
| 302 | + }) |
| 303 | + |
| 304 | + server.route({ |
| 305 | + path: '/', |
| 306 | + method: 'GET', |
| 307 | + handler: (req, h) => { |
| 308 | + const source = new stream.Readable({ |
| 309 | + read () { |
| 310 | + if (this.called) { |
| 311 | + return |
| 312 | + } |
| 313 | + |
| 314 | + this.called = true |
| 315 | + this.push('delayed') |
| 316 | + } |
| 317 | + }) |
| 318 | + |
| 319 | + source.pipe(req.raw.res) |
| 320 | + return h.abandon |
| 321 | + } |
| 322 | + }) |
| 323 | + await registerWithSink(server, 'info', (data, enc, cb) => { |
| 324 | + if (data.res) { |
| 325 | + expect(data.res.statusCode).to.equal(200) |
| 326 | + expect(data.msg).to.match(/\[response\] get \/ - \(\d*ms\)/) |
| 327 | + done() |
| 328 | + } |
| 329 | + cb() |
| 330 | + }) |
| 331 | + |
| 332 | + await server.start() |
| 333 | + flags.onCleanup = () => server.stop() |
| 334 | + |
| 335 | + const client = net.connect(server.info.port, server.info.address, () => { |
| 336 | + client.write('GET / HTTP/1.1\r\n\r\n') |
| 337 | + }) |
| 338 | + |
| 339 | + client.on('data', () => { |
| 340 | + client.destroy() |
| 341 | + }) |
| 342 | + await finish |
| 343 | + }) |
| 344 | + |
293 | 345 | test('handles 500s', async () => {
|
294 | 346 | const server = getServer()
|
295 | 347 | let count = 0
|
|
0 commit comments