Skip to content

Commit

Permalink
Test parsing failure when using async hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarmicki committed Jul 14, 2020
1 parent 64d8d18 commit 779a0a3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
27 changes: 23 additions & 4 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,17 @@ function read (req, res, next, parse, debug, options) {

// read off entire request
stream.resume()
onFinished(req, function onfinished () {
next(createError(400, _error))
})
if (isPromiseAvailable()) {
new Promise(function (resolve) {
onFinished(req, resolve)
}).then(function () {
next(createError(400, _error))
})
} else {
onFinished(req, function () {
next(createError(400, _error))
})
}
}

function readSuccessHandler (body) {
Expand Down Expand Up @@ -128,7 +136,7 @@ function read (req, res, next, parse, debug, options) {
}

debug('read body')
if (typeof global.Promise === 'function') {
if (isPromiseAvailable()) {
getBody(stream, opts)
.then(readSuccessHandler, readErrorHandler)
} else {
Expand Down Expand Up @@ -189,3 +197,14 @@ function contentstream (req, debug, inflate) {

return stream
}

/**
* Check whether Promise is available.
*
* @return {boolean}
* @private
*/

function isPromiseAvailable () {
return typeof global.Promise === 'function'
}
64 changes: 46 additions & 18 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var http = require('http')
var methods = require('methods')
var request = require('supertest')

var whenAsyncHooksAreSupportedIt = it.skip
var describeWhenAsyncHooksAreSupported = describe.skip
try {
var asyncHooks = require('async_hooks')
whenAsyncHooksAreSupportedIt = typeof asyncHooks.AsyncLocalStorage === 'function' ? it : it.skip
describeWhenAsyncHooksAreSupported = typeof asyncHooks.AsyncLocalStorage === 'function' ? describe : describe.skip
} catch (ignored) {
}

Expand Down Expand Up @@ -58,27 +58,55 @@ describe('bodyParser()', function () {
.expect(200, '{"user":"tobi"}', done)
})

whenAsyncHooksAreSupportedIt('should work with async hooks', function (done) {
var _bodyParser = bodyParser()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
describeWhenAsyncHooksAreSupported('used with async hooks', function () {
it('should maintain context when parsing was successful', function (done) {
var _bodyParser = bodyParser()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()

var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(asyncLocalStorage.getStore()))
var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(asyncLocalStorage.getStore()))
})
})
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"contextMaintained":true}', done)
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"contextMaintained":true}', done)
it('should maintain context when parsing has failed', function (done) {
var _bodyParser = bodyParser.text()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()

var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
if (!err) {
res.statusCode = 500
res.end('parsing was expeced to fail, but it succeeded')
}
res.end(JSON.stringify(asyncLocalStorage.getStore()))
})
})
})

request(server)
.post('/')
.set('Content-Type', 'text/plain; charset=x-fake')
.send('user is tobi')
.expect(200, '{"contextMaintained":true}', done)
})
})

describe('http methods', function () {
Expand Down

0 comments on commit 779a0a3

Please sign in to comment.