Skip to content

Commit

Permalink
Merge branch 'master' into feature/iso-8859-1/take2
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou authored Oct 26, 2020
2 parents 8853a30 + 480b1cf commit ca468d4
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.nyc_output/
coverage/
node_modules/
npm-debug.log
Expand Down
30 changes: 17 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ node_js:
- "5.12"
- "6.17"
- "7.10"
- "8.16"
- "8.17"
- "9.11"
- "10.16"
- "10.19"
- "11.15"
- "12.7"
sudo: false
- "12.16"
- "13.11"
cache:
directories:
- node_modules
Expand Down Expand Up @@ -66,15 +66,19 @@ before_install:
# Configure eslint for linting
if node_version_lt '8.0'; then npm_remove_module_re '^eslint(-|$)'
fi
- |
# Configure istanbul for coverage
if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$'
fi
- |
# Configure mocha for testing
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0'
elif node_version_lt '8.0' ; then npm_use_module 'mocha' '6.2.2'
fi
- |
# Configure nyc for coverage
if node_version_lt '0.10'; then npm_remove_module_re '^nyc$'
elif node_version_lt '4.0' ; then npm_use_module 'nyc' '10.3.2'
elif node_version_lt '6.0' ; then npm_use_module 'nyc' '11.9.0'
elif node_version_lt '8.0' ; then npm_use_module 'nyc' '14.1.1'
fi
- |
# Configure supertest for http calls
Expand All @@ -95,8 +99,8 @@ before_scrpt:
npm -s ls ||:
script:
- |
# Run test script, depending on istanbul install
if npm_module_installed 'istanbul'; then npm run-script test-travis
# Run test script, depending on nyc install
if npm_module_installed 'nyc'; then npm run-script test-travis
else npm test
fi
- |
Expand All @@ -105,8 +109,8 @@ script:
fi
after_script:
- |
# Upload coverage to coveralls if exists
if [[ -e ./coverage/lcov.info ]]; then
# Upload coverage to coveralls
if [[ -d .nyc_output ]]; then
npm install --save-dev coveralls@2
coveralls < ./coverage/lcov.info
nyc report --reporter=text-lcov | coveralls
fi
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ unreleased

* deps: http-errors@1.7.3
- deps: inherits@2.0.4
* deps: qs@6.9.3
* deps: raw-body@2.4.1
- deps: http-errors@1.7.3
* deps: safe-buffer@5.2.0
* deps: type-is@~1.6.18

1.19.0 / 2019-04-25
Expand Down
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,15 @@ form. Defaults to `false`.

## Errors

The middlewares provided by this module create errors depending on the error
condition during parsing. The errors will typically have a `status`/`statusCode`
property that contains the suggested HTTP response code, an `expose` property
to determine if the `message` property should be displayed to the client, a
`type` property to determine the type of error without matching against the
`message`, and a `body` property containing the read body, if available.

The following are the common errors emitted, though any error can come through
The middlewares provided by this module create errors using the
[`http-errors` module](https://www.npmjs.com/package/http-errors). The errors
will typically have a `status`/`statusCode` property that contains the suggested
HTTP response code, an `expose` property to determine if the `message` property
should be displayed to the client, a `type` property to determine the type of
error without matching against the `message`, and a `body` property containing
the read body, if available.

The following are the common errors created, though any error can come through
for various reasons.

### content encoding unsupported
Expand All @@ -314,6 +315,20 @@ contained an encoding but the "inflation" option was set to `false`. The
`'encoding.unsupported'`, and the `charset` property will be set to the
encoding that is unsupported.

### entity parse failed

This error will occur when the request contained an entity that could not be
parsed by the middleware. The `status` property is set to `400`, the `type`
property is set to `'entity.parse.failed'`, and the `body` property is set to
the entity value that failed parsing.

### entity verify failed

This error will occur when the request contained an entity that could not be
failed verification by the defined `verify` option. The `status` property is
set to `403`, the `type` property is set to `'entity.verify.failed'`, and the
`body` property is set to the entity value that failed verification.

### request aborted

This error will occur when the request is aborted by the client before reading
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
"http-errors": "1.7.3",
"iconv-lite": "0.4.24",
"on-finished": "~2.3.0",
"qs": "6.9.1",
"qs": "6.9.3",
"raw-body": "2.4.1",
"type-is": "~1.6.18"
},
"devDependencies": {
"eslint": "6.0.0",
"eslint-config-standard": "12.0.0",
"eslint-plugin-import": "2.18.0",
"eslint-plugin-markdown": "1.0.0",
"eslint-plugin-node": "9.1.0",
"eslint": "6.8.0",
"eslint-config-standard": "14.1.1",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-markdown": "1.0.2",
"eslint-plugin-node": "9.2.0",
"eslint-plugin-promise": "4.2.1",
"eslint-plugin-standard": "4.0.0",
"istanbul": "0.4.5",
"eslint-plugin-standard": "4.0.1",
"methods": "1.1.2",
"mocha": "6.1.4",
"safe-buffer": "5.1.2",
"mocha": "7.1.1",
"nyc": "15.0.0",
"safe-buffer": "5.2.0",
"supertest": "4.0.2"
},
"files": [
Expand All @@ -46,7 +46,7 @@
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --require test/support/env --reporter spec --check-leaks --bail test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/"
"test-cov": "nyc --reporter=html --reporter=text npm test",
"test-travis": "nyc --reporter=text npm test"
}
}
16 changes: 10 additions & 6 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ describe('bodyParser()', function () {

describe('with verify option', function () {
it('should apply to json', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
}
})

request(server)
.post('/')
Expand All @@ -126,9 +128,11 @@ describe('bodyParser()', function () {
})

it('should apply to urlencoded', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x20) throw new Error('no leading space')
}
})

request(server)
.post('/')
Expand Down
76 changes: 46 additions & 30 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,11 @@ describe('bodyParser.json()', function () {
})

it('should error from verify', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

request(server)
.post('/')
Expand All @@ -402,9 +404,11 @@ describe('bodyParser.json()', function () {
})

it('should error with type = "entity.verify.failed"', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

request(server)
.post('/')
Expand All @@ -415,12 +419,14 @@ describe('bodyParser.json()', function () {
})

it('should allow custom codes', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] !== 0x5b) return
var err = new Error('no arrays')
err.status = 400
throw err
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] !== 0x5b) return
var err = new Error('no arrays')
err.status = 400
throw err
}
})

request(server)
.post('/')
Expand All @@ -430,12 +436,14 @@ describe('bodyParser.json()', function () {
})

it('should allow custom type', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] !== 0x5b) return
var err = new Error('no arrays')
err.type = 'foo.bar'
throw err
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] !== 0x5b) return
var err = new Error('no arrays')
err.type = 'foo.bar'
throw err
}
})

request(server)
.post('/')
Expand All @@ -446,9 +454,11 @@ describe('bodyParser.json()', function () {
})

it('should include original body on error object', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

request(server)
.post('/')
Expand All @@ -459,9 +469,11 @@ describe('bodyParser.json()', function () {
})

it('should allow pass-through', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

request(server)
.post('/')
Expand All @@ -471,9 +483,11 @@ describe('bodyParser.json()', function () {
})

it('should work with different charsets', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}
})

var test = request(server).post('/')
test.set('Content-Type', 'application/json; charset=utf-16')
Expand All @@ -482,9 +496,11 @@ describe('bodyParser.json()', function () {
})

it('should 415 on unknown charset prior to verify', function (done) {
var server = createServer({ verify: function (req, res, buf) {
throw new Error('unexpected verify call')
} })
var server = createServer({
verify: function (req, res, buf) {
throw new Error('unexpected verify call')
}
})

var test = request(server).post('/')
test.set('Content-Type', 'application/json; charset=x-bogus')
Expand Down
30 changes: 18 additions & 12 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,11 @@ describe('bodyParser.raw()', function () {
})

it('should error from verify', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x00) throw new Error('no leading null')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x00) throw new Error('no leading null')
}
})

var test = request(server).post('/')
test.set('Content-Type', 'application/octet-stream')
Expand All @@ -259,12 +261,14 @@ describe('bodyParser.raw()', function () {
})

it('should allow custom codes', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] !== 0x00) return
var err = new Error('no leading null')
err.status = 400
throw err
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] !== 0x00) return
var err = new Error('no leading null')
err.status = 400
throw err
}
})

var test = request(server).post('/')
test.set('Content-Type', 'application/octet-stream')
Expand All @@ -273,9 +277,11 @@ describe('bodyParser.raw()', function () {
})

it('should allow pass-through', function (done) {
var server = createServer({ verify: function (req, res, buf) {
if (buf[0] === 0x00) throw new Error('no leading null')
} })
var server = createServer({
verify: function (req, res, buf) {
if (buf[0] === 0x00) throw new Error('no leading null')
}
})

var test = request(server).post('/')
test.set('Content-Type', 'application/octet-stream')
Expand Down
Loading

0 comments on commit ca468d4

Please sign in to comment.