Skip to content
Myr

express.text.js

This file

Path

test/express.text.js

442 lines · 13.1 KiB · JavaScript

Last changed by

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

History of this file

Take it

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

line 1why, one step back
1'use strict'

tests: add express.text test suiteDouglas Christopher Wilsonbefore adoptionbb5211f

lines 2–442why, one step back
2
3var assert = require('assert')4var Buffer = require('safe-buffer').Buffer5var express = require('..')6var request = require('supertest')78describe('express.text()', function () {9 before(function () {10 this.app = createApp()11 })1213 it('should parse text/plain', function (done) {14 request(this.app)15 .post('/')16 .set('Content-Type', 'text/plain')17 .send('user is tobi')18 .expect(200, '"user is tobi"', done)19 })2021 it('should 400 when invalid content-length', function (done) {22 var app = express()2324 app.use(function (req, res, next) {25 req.headers['content-length'] = '20' // bad length26 next()27 })2829 app.use(express.text())3031 app.post('/', function (req, res) {32 res.json(req.body)33 })3435 request(app)36 .post('/')37 .set('Content-Type', 'text/plain')38 .send('user')39 .expect(400, /content length/, done)40 })4142 it('should handle Content-Length: 0', function (done) {43 request(createApp({ limit: '1kb' }))44 .post('/')45 .set('Content-Type', 'text/plain')46 .set('Content-Length', '0')47 .expect(200, '""', done)48 })4950 it('should handle empty message-body', function (done) {51 request(createApp({ limit: '1kb' }))52 .post('/')53 .set('Content-Type', 'text/plain')54 .set('Transfer-Encoding', 'chunked')55 .send('')56 .expect(200, '""', done)57 })5859 it('should handle duplicated middleware', function (done) {60 var app = express()6162 app.use(express.text())63 app.use(express.text())6465 app.post('/', function (req, res) {66 res.json(req.body)67 })6869 request(app)70 .post('/')71 .set('Content-Type', 'text/plain')72 .send('user is tobi')73 .expect(200, '"user is tobi"', done)74 })7576 describe('with defaultCharset option', function () {77 it('should change default charset', function (done) {78 var app = createApp({ defaultCharset: 'koi8-r' })79 var test = request(app).post('/')80 test.set('Content-Type', 'text/plain')81 test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))82 test.expect(200, '"name is нет"', done)83 })8485 it('should honor content-type charset', function (done) {86 var app = createApp({ defaultCharset: 'koi8-r' })87 var test = request(app).post('/')88 test.set('Content-Type', 'text/plain; charset=utf-8')89 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))90 test.expect(200, '"name is 论"', done)91 })92 })9394 describe('with limit option', function () {95 it('should 413 when over limit with Content-Length', function (done) {96 var buf = Buffer.alloc(1028, '.')97 request(createApp({ limit: '1kb' }))98 .post('/')99 .set('Content-Type', 'text/plain')100 .set('Content-Length', '1028')101 .send(buf.toString())102 .expect(413, done)103 })104105 it('should 413 when over limit with chunked encoding', function (done) {106 var buf = Buffer.alloc(1028, '.')107 var app = createApp({ limit: '1kb' })108 var test = request(app).post('/')109 test.set('Content-Type', 'text/plain')110 test.set('Transfer-Encoding', 'chunked')111 test.write(buf.toString())112 test.expect(413, done)113 })114115 it('should accept number of bytes', function (done) {116 var buf = Buffer.alloc(1028, '.')117 request(createApp({ limit: 1024 }))118 .post('/')119 .set('Content-Type', 'text/plain')120 .send(buf.toString())121 .expect(413, done)122 })123124 it('should not change when options altered', function (done) {125 var buf = Buffer.alloc(1028, '.')126 var options = { limit: '1kb' }127 var app = createApp(options)128129 options.limit = '100kb'130131 request(app)132 .post('/')133 .set('Content-Type', 'text/plain')134 .send(buf.toString())135 .expect(413, done)136 })137138 it('should not hang response', function (done) {139 var buf = Buffer.alloc(10240, '.')140 var app = createApp({ limit: '8kb' })141 var test = request(app).post('/')142 test.set('Content-Type', 'text/plain')143 test.write(buf)144 test.write(buf)145 test.write(buf)146 test.expect(413, done)147 })148 })149150 describe('with inflate option', function () {151 describe('when false', function () {152 before(function () {153 this.app = createApp({ inflate: false })154 })155156 it('should not accept content-encoding', function (done) {157 var test = request(this.app).post('/')158 test.set('Content-Encoding', 'gzip')159 test.set('Content-Type', 'text/plain')160 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))161 test.expect(415, 'content encoding unsupported', done)162 })163 })164165 describe('when true', function () {166 before(function () {167 this.app = createApp({ inflate: true })168 })169170 it('should accept content-encoding', function (done) {171 var test = request(this.app).post('/')172 test.set('Content-Encoding', 'gzip')173 test.set('Content-Type', 'text/plain')174 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))175 test.expect(200, '"name is 论"', done)176 })177 })178 })179180 describe('with type option', function () {181 describe('when "text/html"', function () {182 before(function () {183 this.app = createApp({ type: 'text/html' })184 })185186 it('should parse for custom type', function (done) {187 request(this.app)188 .post('/')189 .set('Content-Type', 'text/html')190 .send('<b>tobi</b>')191 .expect(200, '"<b>tobi</b>"', done)192 })193194 it('should ignore standard type', function (done) {195 request(this.app)196 .post('/')197 .set('Content-Type', 'text/plain')198 .send('user is tobi')199 .expect(200, '{}', done)200 })201 })202203 describe('when ["text/html", "text/plain"]', function () {204 before(function () {205 this.app = createApp({ type: ['text/html', 'text/plain'] })206 })207208 it('should parse "text/html"', function (done) {209 request(this.app)210 .post('/')211 .set('Content-Type', 'text/html')212 .send('<b>tobi</b>')213 .expect(200, '"<b>tobi</b>"', done)214 })215216 it('should parse "text/plain"', function (done) {217 request(this.app)218 .post('/')219 .set('Content-Type', 'text/plain')220 .send('tobi')221 .expect(200, '"tobi"', done)222 })223224 it('should ignore "text/xml"', function (done) {225 request(this.app)226 .post('/')227 .set('Content-Type', 'text/xml')228 .send('<user>tobi</user>')229 .expect(200, '{}', done)230 })231 })232233 describe('when a function', function () {234 it('should parse when truthy value returned', function (done) {235 var app = createApp({ type: accept })236237 function accept (req) {238 return req.headers['content-type'] === 'text/vnd.something'239 }240241 request(app)242 .post('/')243 .set('Content-Type', 'text/vnd.something')244 .send('user is tobi')245 .expect(200, '"user is tobi"', done)246 })247248 it('should work without content-type', function (done) {249 var app = createApp({ type: accept })250251 function accept (req) {252 return true253 }254255 var test = request(app).post('/')256 test.write('user is tobi')257 test.expect(200, '"user is tobi"', done)258 })259260 it('should not invoke without a body', function (done) {261 var app = createApp({ type: accept })262263 function accept (req) {264 throw new Error('oops!')265 }266267 request(app)268 .get('/')269 .expect(404, done)270 })271 })272 })273274 describe('with verify option', function () {275 it('should assert value is function', function () {276 assert.throws(createApp.bind(null, { verify: 'lol' }),277 /TypeError: option verify must be function/)278 })279280 it('should error from verify', function (done) {281 var app = createApp({ verify: function (req, res, buf) {282 if (buf[0] === 0x20) throw new Error('no leading space')283 } })284285 request(app)286 .post('/')287 .set('Content-Type', 'text/plain')288 .send(' user is tobi')289 .expect(403, 'no leading space', done)290 })291292 it('should allow custom codes', function (done) {293 var app = createApp({ verify: function (req, res, buf) {294 if (buf[0] !== 0x20) return295 var err = new Error('no leading space')296 err.status = 400297 throw err298 } })299300 request(app)301 .post('/')302 .set('Content-Type', 'text/plain')303 .send(' user is tobi')304 .expect(400, 'no leading space', done)305 })306307 it('should allow pass-through', function (done) {308 var app = createApp({ verify: function (req, res, buf) {309 if (buf[0] === 0x20) throw new Error('no leading space')310 } })311312 request(app)313 .post('/')314 .set('Content-Type', 'text/plain')315 .send('user is tobi')316 .expect(200, '"user is tobi"', done)317 })318319 it('should 415 on unknown charset prior to verify', function (done) {320 var app = createApp({ verify: function (req, res, buf) {321 throw new Error('unexpected verify call')322 } })323324 var test = request(app).post('/')325 test.set('Content-Type', 'text/plain; charset=x-bogus')326 test.write(Buffer.from('00000000', 'hex'))327 test.expect(415, 'unsupported charset "X-BOGUS"', done)328 })329 })330331 describe('charset', function () {332 before(function () {333 this.app = createApp()334 })335336 it('should parse utf-8', function (done) {337 var test = request(this.app).post('/')338 test.set('Content-Type', 'text/plain; charset=utf-8')339 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))340 test.expect(200, '"name is 论"', done)341 })342343 it('should parse codepage charsets', function (done) {344 var test = request(this.app).post('/')345 test.set('Content-Type', 'text/plain; charset=koi8-r')346 test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))347 test.expect(200, '"name is нет"', done)348 })349350 it('should parse when content-length != char length', function (done) {351 var test = request(this.app).post('/')352 test.set('Content-Type', 'text/plain; charset=utf-8')353 test.set('Content-Length', '11')354 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))355 test.expect(200, '"name is 论"', done)356 })357358 it('should default to utf-8', function (done) {359 var test = request(this.app).post('/')360 test.set('Content-Type', 'text/plain')361 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))362 test.expect(200, '"name is 论"', done)363 })364365 it('should 415 on unknown charset', function (done) {366 var test = request(this.app).post('/')367 test.set('Content-Type', 'text/plain; charset=x-bogus')368 test.write(Buffer.from('00000000', 'hex'))369 test.expect(415, 'unsupported charset "X-BOGUS"', done)370 })371 })372373 describe('encoding', function () {374 before(function () {375 this.app = createApp({ limit: '10kb' })376 })377378 it('should parse without encoding', function (done) {379 var test = request(this.app).post('/')380 test.set('Content-Type', 'text/plain')381 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))382 test.expect(200, '"name is 论"', done)383 })384385 it('should support identity encoding', function (done) {386 var test = request(this.app).post('/')387 test.set('Content-Encoding', 'identity')388 test.set('Content-Type', 'text/plain')389 test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))390 test.expect(200, '"name is 论"', done)391 })392393 it('should support gzip encoding', function (done) {394 var test = request(this.app).post('/')395 test.set('Content-Encoding', 'gzip')396 test.set('Content-Type', 'text/plain')397 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))398 test.expect(200, '"name is 论"', done)399 })400401 it('should support deflate encoding', function (done) {402 var test = request(this.app).post('/')403 test.set('Content-Encoding', 'deflate')404 test.set('Content-Type', 'text/plain')405 test.write(Buffer.from('789ccb4bcc4d55c82c5678b16e17001a6f050e', 'hex'))406 test.expect(200, '"name is 论"', done)407 })408409 it('should be case-insensitive', function (done) {410 var test = request(this.app).post('/')411 test.set('Content-Encoding', 'GZIP')412 test.set('Content-Type', 'text/plain')413 test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))414 test.expect(200, '"name is 论"', done)415 })416417 it('should fail on unknown encoding', function (done) {418 var test = request(this.app).post('/')419 test.set('Content-Encoding', 'nulls')420 test.set('Content-Type', 'text/plain')421 test.write(Buffer.from('000000000000', 'hex'))422 test.expect(415, 'unsupported content encoding "nulls"', done)423 })424 })425})426427function createApp (options) {428 var app = express()429430 app.use(express.text(options))431432 app.use(function (err, req, res, next) {433 res.status(err.status || 500)434 res.send(err.message)435 })436437 app.post('/', function (req, res) {438 res.json(req.body)439 })440441 return app442}