Skip to content

Commit

Permalink
tests: store server on mocha context instead of variable shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 8, 2017
1 parent 29c8cd0 commit 3cb380b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 144 deletions.
22 changes: 9 additions & 13 deletions test/body-parser.js
Expand Up @@ -6,27 +6,26 @@ var request = require('supertest')
var bodyParser = require('..')

describe('bodyParser()', function () {
var server
before(function () {
server = createServer()
this.server = createServer()
})

it('should default to {}', function (done) {
request(server)
request(this.server)
.post('/')
.expect(200, '{}', done)
})

it('should parse JSON', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse x-www-form-urlencoded', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
Expand All @@ -53,12 +52,10 @@ describe('bodyParser()', function () {
})

describe('http methods', function () {
var server

before(function () {
var _bodyParser = bodyParser()

server = http.createServer(function (req, res) {
this.server = http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
if (err) {
res.statusCode = 500
Expand All @@ -81,7 +78,7 @@ describe('bodyParser()', function () {
}

it('should support ' + method.toUpperCase() + ' requests', function (done) {
request(server)[method]('/')
request(this.server)[method]('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(201, done)
Expand All @@ -90,21 +87,20 @@ describe('bodyParser()', function () {
})

describe('with type option', function () {
var server
before(function () {
server = createServer({ limit: '1mb', type: 'application/octet-stream' })
this.server = createServer({ limit: '1mb', type: 'application/octet-stream' })
})

it('should parse JSON', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse x-www-form-urlencoded', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
Expand Down
70 changes: 36 additions & 34 deletions test/json.js
Expand Up @@ -86,8 +86,12 @@ describe('bodyParser.json()', function () {
})

describe('when strict is false', function () {
before(function () {
this.server = createServer({ strict: false })
})

it('should parse primitives', function (done) {
request(createServer({ strict: false }))
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('true')
Expand All @@ -96,29 +100,28 @@ describe('bodyParser.json()', function () {
})

describe('when strict is true', function () {
var server
before(function () {
server = createServer({ strict: true })
this.server = createServer({ strict: true })
})

it('should not parse primitives', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, parseError('#rue').replace('#', 't'), done)
})

it('should not parse primitives with leading whitespaces', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send(' true')
.expect(400, parseError(' #rue').replace('#', 't'), done)
})

it('should allow leading whitespaces in JSON', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send(' { "user": "tobi" }')
Expand All @@ -127,8 +130,12 @@ describe('bodyParser.json()', function () {
})

describe('by default', function () {
before(function () {
this.server = createServer()
})

it('should 400 on primitives', function (done) {
request(createServer())
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('true')
Expand Down Expand Up @@ -195,13 +202,12 @@ describe('bodyParser.json()', function () {

describe('with inflate option', function () {
describe('when false', function () {
var server
before(function () {
server = createServer({ inflate: false })
this.server = createServer({ inflate: false })
})

it('should not accept content-encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
Expand All @@ -210,13 +216,12 @@ describe('bodyParser.json()', function () {
})

describe('when true', function () {
var server
before(function () {
server = createServer({ inflate: true })
this.server = createServer({ inflate: true })
})

it('should accept content-encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
Expand All @@ -227,21 +232,20 @@ describe('bodyParser.json()', function () {

describe('with type option', function () {
describe('when "application/vnd.api+json"', function () {
var server
before(function () {
server = createServer({ type: 'application/vnd.api+json' })
this.server = createServer({ type: 'application/vnd.api+json' })
})

it('should parse JSON for custom type', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/vnd.api+json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should ignore standard type', function (done) {
request(server)
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
Expand Down Expand Up @@ -359,103 +363,101 @@ describe('bodyParser.json()', function () {
})

describe('charset', function () {
var server
before(function () {
server = createServer()
this.server = createServer()
})

it('should parse utf-8', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=utf-8')
test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should parse utf-16', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=utf-16')
test.write(Buffer.from('feff007b0022006e0061006d00650022003a00228bba0022007d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should parse when content-length != char length', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=utf-8')
test.set('Content-Length', '13')
test.write(Buffer.from('7b2274657374223a22c3a5227d', 'hex'))
test.expect(200, '{"test":"å"}', done)
})

it('should default to utf-8', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should fail on unknown charset', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json; charset=koi8-r')
test.write(Buffer.from('7b226e616d65223a22cec5d4227d', 'hex'))
test.expect(415, 'unsupported charset "KOI8-R"', done)
})
})

describe('encoding', function () {
var server
before(function () {
server = createServer({ limit: '1kb' })
this.server = createServer({ limit: '1kb' })
})

it('should parse without encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should support identity encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'identity')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('7b226e616d65223a22e8aeba227d', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should support gzip encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should support deflate encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'deflate')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('789cab56ca4bcc4d55b2527ab16e97522d00274505ac', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should be case-insensitive', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'GZIP')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56ca4bcc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
test.expect(200, '{"name":"论"}', done)
})

it('should 415 on unknown encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'nulls')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('000000000000', 'hex'))
test.expect(415, 'unsupported content encoding "nulls"', done)
})

it('should 400 on malformed encoding', function (done) {
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bab56cc4d55b2527ab16e97522d00515be1cc0e000000', 'hex'))
Expand All @@ -464,7 +466,7 @@ describe('bodyParser.json()', function () {

it('should 413 when inflated value exceeds limit', function (done) {
// gzip'd data exceeds 1kb, but deflated below 1kb
var test = request(server).post('/')
var test = request(this.server).post('/')
test.set('Content-Encoding', 'gzip')
test.set('Content-Type', 'application/json')
test.write(Buffer.from('1f8b080000000000000bedc1010d000000c2a0f74f6d0f071400000000000000', 'hex'))
Expand Down

0 comments on commit 3cb380b

Please sign in to comment.