Skip to content
Myr

express.text.js

This file

Path

test/express.text.js

578 lines · 17.1 KiB · JavaScript

Last changed by

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

History of this file

Take it

1'use strict'23var assert = require('assert')4var asyncHooks = tryRequire('async_hooks')5var Buffer = require('safe-buffer').Buffer6var express = require('..')7var request = require('supertest')89var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'10  ? describe11  : describe.skip1213describe('express.text()', function () {14  before(function () {15    this.app = createApp()16  })1718  it('should parse text/plain', function (done) {19    request(this.app)20      .post('/')21      .set('Content-Type', 'text/plain')22      .send('user is tobi')23      .expect(200, '"user is tobi"', done)24  })2526  it('should 400 when invalid content-length', function (done) {27    var app = express()2829    app.use(function (req, res, next) {30      req.headers['content-length'] = '20' // bad length31      next()32    })3334    app.use(express.text())3536    app.post('/', function (req, res) {37      res.json(req.body)38    })3940    request(app)41      .post('/')42      .set('Content-Type', 'text/plain')43      .send('user')44      .expect(400, /content length/, done)45  })4647  it('should handle Content-Length: 0', function (done) {48    request(createApp({ limit: '1kb' }))49      .post('/')50      .set('Content-Type', 'text/plain')51      .set('Content-Length', '0')52      .expect(200, '""', done)53  })5455  it('should handle empty message-body', function (done) {56    request(createApp({ limit: '1kb' }))57      .post('/')58      .set('Content-Type', 'text/plain')59      .set('Transfer-Encoding', 'chunked')60      .send('')61      .expect(200, '""', done)62  })6364  it('should handle duplicated middleware', function (done) {65    var app = express()6667    app.use(express.text())68    app.use(express.text())6970    app.post('/', function (req, res) {71      res.json(req.body)72    })7374    request(app)75      .post('/')76      .set('Content-Type', 'text/plain')77      .send('user is tobi')78      .expect(200, '"user is tobi"', done)79  })8081  describe('with defaultCharset option', function () {82    it('should change default charset', function (done) {83      var server = createApp({ defaultCharset: 'koi8-r' })84      var test = request(server).post('/')85      test.set('Content-Type', 'text/plain')86      test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))87      test.expect(200, '"name is нет"', done)88    })8990    it('should honor content-type charset', function (done) {91      var server = createApp({ defaultCharset: 'koi8-r' })92      var test = request(server).post('/')93      test.set('Content-Type', 'text/plain; charset=utf-8')94      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))95      test.expect(200, '"name is 论"', done)96    })97  })9899  describe('with limit option', function () {100    it('should 413 when over limit with Content-Length', function (done) {101      var buf = Buffer.alloc(1028, '.')102      request(createApp({ limit: '1kb' }))103        .post('/')104        .set('Content-Type', 'text/plain')105        .set('Content-Length', '1028')106        .send(buf.toString())107        .expect(413, done)108    })109110    it('should 413 when over limit with chunked encoding', function (done) {111      var app = createApp({ limit: '1kb' })112      var buf = Buffer.alloc(1028, '.')113      var test = request(app).post('/')114      test.set('Content-Type', 'text/plain')115      test.set('Transfer-Encoding', 'chunked')116      test.write(buf.toString())117      test.expect(413, done)118    })119120    it('should 413 when inflated body over limit', function (done) {121      var app = createApp({ limit: '1kb' })122      var test = request(app).post('/')123      test.set('Content-Encoding', 'gzip')124      test.set('Content-Type', 'text/plain')125      test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a14704040000', 'hex'))126      test.expect(413, done)127    })128129    it('should accept number of bytes', function (done) {130      var buf = Buffer.alloc(1028, '.')131      request(createApp({ limit: 1024 }))132        .post('/')133        .set('Content-Type', 'text/plain')134        .send(buf.toString())135        .expect(413, done)136    })137138    it('should not change when options altered', function (done) {139      var buf = Buffer.alloc(1028, '.')140      var options = { limit: '1kb' }141      var app = createApp(options)142143      options.limit = '100kb'144145      request(app)146        .post('/')147        .set('Content-Type', 'text/plain')148        .send(buf.toString())149        .expect(413, done)150    })151152    it('should not hang response', function (done) {153      var app = createApp({ limit: '8kb' })154      var buf = Buffer.alloc(10240, '.')155      var test = request(app).post('/')156      test.set('Content-Type', 'text/plain')157      test.write(buf)158      test.write(buf)159      test.write(buf)160      test.expect(413, done)161    })162163    it('should not error when inflating', function (done) {164      var app = createApp({ limit: '1kb' })165      var test = request(app).post('/')166      test.set('Content-Encoding', 'gzip')167      test.set('Content-Type', 'text/plain')168      test.write(Buffer.from('1f8b080000000000000ad3d31b05a360148c64000087e5a1470404', 'hex'))169      setTimeout(function () {170        test.expect(413, done)171      }, 100)172    })173  })174175  describe('with inflate option', function () {176    describe('when false', function () {177      before(function () {178        this.app = createApp({ inflate: false })179      })180181      it('should not accept content-encoding', function (done) {182        var test = request(this.app).post('/')183        test.set('Content-Encoding', 'gzip')184        test.set('Content-Type', 'text/plain')185        test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))186        test.expect(415, '[encoding.unsupported] content encoding unsupported', done)187      })188    })189190    describe('when true', function () {191      before(function () {192        this.app = createApp({ inflate: true })193      })194195      it('should accept content-encoding', function (done) {196        var test = request(this.app).post('/')197        test.set('Content-Encoding', 'gzip')198        test.set('Content-Type', 'text/plain')199        test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))200        test.expect(200, '"name is 论"', done)201      })202    })203  })204205  describe('with type option', function () {206    describe('when "text/html"', function () {207      before(function () {208        this.app = createApp({ type: 'text/html' })209      })210211      it('should parse for custom type', function (done) {212        request(this.app)213          .post('/')214          .set('Content-Type', 'text/html')215          .send('<b>tobi</b>')216          .expect(200, '"<b>tobi</b>"', done)217      })218219      it('should ignore standard type', function (done) {220        request(this.app)221          .post('/')222          .set('Content-Type', 'text/plain')223          .send('user is tobi')224          .expect(200, '', done)225      })226    })227228    describe('when ["text/html", "text/plain"]', function () {229      before(function () {230        this.app = createApp({ type: ['text/html', 'text/plain'] })231      })232233      it('should parse "text/html"', function (done) {234        request(this.app)235          .post('/')236          .set('Content-Type', 'text/html')237          .send('<b>tobi</b>')238          .expect(200, '"<b>tobi</b>"', done)239      })240241      it('should parse "text/plain"', function (done) {242        request(this.app)243          .post('/')244          .set('Content-Type', 'text/plain')245          .send('tobi')246          .expect(200, '"tobi"', done)247      })248249      it('should ignore "text/xml"', function (done) {250        request(this.app)251          .post('/')252          .set('Content-Type', 'text/xml')253          .send('<user>tobi</user>')254          .expect(200, '', done)255      })256    })257258    describe('when a function', function () {259      it('should parse when truthy value returned', function (done) {260        var app = createApp({ type: accept })261262        function accept (req) {263          return req.headers['content-type'] === 'text/vnd.something'264        }265266        request(app)267          .post('/')268          .set('Content-Type', 'text/vnd.something')269          .send('user is tobi')270          .expect(200, '"user is tobi"', done)271      })272273      it('should work without content-type', function (done) {274        var app = createApp({ type: accept })275276        function accept (req) {277          return true278        }279280        var test = request(app).post('/')281        test.write('user is tobi')282        test.expect(200, '"user is tobi"', done)283      })284285      it('should not invoke without a body', function (done) {286        var app = createApp({ type: accept })287288        function accept (req) {289          throw new Error('oops!')290        }291292        request(app)293          .get('/')294          .expect(404, done)295      })296    })297  })298299  describe('with verify option', function () {300    it('should assert value is function', function () {301      assert.throws(createApp.bind(null, { verify: 'lol' }),302        /TypeError: option verify must be function/)303    })304305    it('should error from verify', function (done) {306      var app = createApp({307        verify: function (req, res, buf) {308          if (buf[0] === 0x20) throw new Error('no leading space')309        }310      })311312      request(app)313        .post('/')314        .set('Content-Type', 'text/plain')315        .send(' user is tobi')316        .expect(403, '[entity.verify.failed] no leading space', done)317    })318319    it('should allow custom codes', function (done) {320      var app = createApp({321        verify: function (req, res, buf) {322          if (buf[0] !== 0x20) return323          var err = new Error('no leading space')324          err.status = 400325          throw err326        }327      })328329      request(app)330        .post('/')331        .set('Content-Type', 'text/plain')332        .send(' user is tobi')333        .expect(400, '[entity.verify.failed] no leading space', done)334    })335336    it('should allow pass-through', function (done) {337      var app = createApp({338        verify: function (req, res, buf) {339          if (buf[0] === 0x20) throw new Error('no leading space')340        }341      })342343      request(app)344        .post('/')345        .set('Content-Type', 'text/plain')346        .send('user is tobi')347        .expect(200, '"user is tobi"', done)348    })349350    it('should 415 on unknown charset prior to verify', function (done) {351      var app = createApp({352        verify: function (req, res, buf) {353          throw new Error('unexpected verify call')354        }355      })356357      var test = request(app).post('/')358      test.set('Content-Type', 'text/plain; charset=x-bogus')359      test.write(Buffer.from('00000000', 'hex'))360      test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)361    })362  })363364  describeAsyncHooks('async local storage', function () {365    before(function () {366      var app = express()367      var store = { foo: 'bar' }368369      app.use(function (req, res, next) {370        req.asyncLocalStorage = new asyncHooks.AsyncLocalStorage()371        req.asyncLocalStorage.run(store, next)372      })373374      app.use(express.text())375376      app.use(function (req, res, next) {377        var local = req.asyncLocalStorage.getStore()378379        if (local) {380          res.setHeader('x-store-foo', String(local.foo))381        }382383        next()384      })385386      app.use(function (err, req, res, next) {387        var local = req.asyncLocalStorage.getStore()388389        if (local) {390          res.setHeader('x-store-foo', String(local.foo))391        }392393        res.status(err.status || 500)394        res.send('[' + err.type + '] ' + err.message)395      })396397      app.post('/', function (req, res) {398        res.json(req.body)399      })400401      this.app = app402    })403404    it('should presist store', function (done) {405      request(this.app)406        .post('/')407        .set('Content-Type', 'text/plain')408        .send('user is tobi')409        .expect(200)410        .expect('x-store-foo', 'bar')411        .expect('"user is tobi"')412        .end(done)413    })414415    it('should presist store when unmatched content-type', function (done) {416      request(this.app)417        .post('/')418        .set('Content-Type', 'application/fizzbuzz')419        .send('buzz')420        .expect(200)421        .expect('x-store-foo', 'bar')422        .end(done)423    })424425    it('should presist store when inflated', function (done) {426      var test = request(this.app).post('/')427      test.set('Content-Encoding', 'gzip')428      test.set('Content-Type', 'text/plain')429      test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))430      test.expect(200)431      test.expect('x-store-foo', 'bar')432      test.expect('"name is 论"')433      test.end(done)434    })435436    it('should presist store when inflate error', function (done) {437      var test = request(this.app).post('/')438      test.set('Content-Encoding', 'gzip')439      test.set('Content-Type', 'text/plain')440      test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b0000', 'hex'))441      test.expect(400)442      test.expect('x-store-foo', 'bar')443      test.end(done)444    })445446    it('should presist store when limit exceeded', function (done) {447      request(this.app)448        .post('/')449        .set('Content-Type', 'text/plain')450        .send('user is ' + Buffer.alloc(1024 * 100, '.').toString())451        .expect(413)452        .expect('x-store-foo', 'bar')453        .end(done)454    })455  })456457  describe('charset', function () {458    before(function () {459      this.app = createApp()460    })461462    it('should parse utf-8', function (done) {463      var test = request(this.app).post('/')464      test.set('Content-Type', 'text/plain; charset=utf-8')465      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))466      test.expect(200, '"name is 论"', done)467    })468469    it('should parse codepage charsets', function (done) {470      var test = request(this.app).post('/')471      test.set('Content-Type', 'text/plain; charset=koi8-r')472      test.write(Buffer.from('6e616d6520697320cec5d4', 'hex'))473      test.expect(200, '"name is нет"', done)474    })475476    it('should parse when content-length != char length', function (done) {477      var test = request(this.app).post('/')478      test.set('Content-Type', 'text/plain; charset=utf-8')479      test.set('Content-Length', '11')480      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))481      test.expect(200, '"name is 论"', done)482    })483484    it('should default to utf-8', function (done) {485      var test = request(this.app).post('/')486      test.set('Content-Type', 'text/plain')487      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))488      test.expect(200, '"name is 论"', done)489    })490491    it('should 415 on unknown charset', function (done) {492      var test = request(this.app).post('/')493      test.set('Content-Type', 'text/plain; charset=x-bogus')494      test.write(Buffer.from('00000000', 'hex'))495      test.expect(415, '[charset.unsupported] unsupported charset "X-BOGUS"', done)496    })497  })498499  describe('encoding', function () {500    before(function () {501      this.app = createApp({ limit: '10kb' })502    })503504    it('should parse without encoding', function (done) {505      var test = request(this.app).post('/')506      test.set('Content-Type', 'text/plain')507      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))508      test.expect(200, '"name is 论"', done)509    })510511    it('should support identity encoding', function (done) {512      var test = request(this.app).post('/')513      test.set('Content-Encoding', 'identity')514      test.set('Content-Type', 'text/plain')515      test.write(Buffer.from('6e616d6520697320e8aeba', 'hex'))516      test.expect(200, '"name is 论"', done)517    })518519    it('should support gzip encoding', function (done) {520      var test = request(this.app).post('/')521      test.set('Content-Encoding', 'gzip')522      test.set('Content-Type', 'text/plain')523      test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))524      test.expect(200, '"name is 论"', done)525    })526527    it('should support deflate encoding', function (done) {528      var test = request(this.app).post('/')529      test.set('Content-Encoding', 'deflate')530      test.set('Content-Type', 'text/plain')531      test.write(Buffer.from('789ccb4bcc4d55c82c5678b16e17001a6f050e', 'hex'))532      test.expect(200, '"name is 论"', done)533    })534535    it('should be case-insensitive', function (done) {536      var test = request(this.app).post('/')537      test.set('Content-Encoding', 'GZIP')538      test.set('Content-Type', 'text/plain')539      test.write(Buffer.from('1f8b080000000000000bcb4bcc4d55c82c5678b16e170072b3e0200b000000', 'hex'))540      test.expect(200, '"name is 论"', done)541    })542543    it('should 415 on unknown encoding', function (done) {544      var test = request(this.app).post('/')545      test.set('Content-Encoding', 'nulls')546      test.set('Content-Type', 'text/plain')547      test.write(Buffer.from('000000000000', 'hex'))548      test.expect(415, '[encoding.unsupported] unsupported content encoding "nulls"', done)549    })550  })551})552553function createApp (options) {554  var app = express()555556  app.use(express.text(options))557558  app.use(function (err, req, res, next) {559    res.status(err.status || 500)560    res.send(String(req.headers['x-error-property']561      ? err[req.headers['x-error-property']]562      : ('[' + err.type + '] ' + err.message)))563  })564565  app.post('/', function (req, res) {566    res.json(req.body)567  })568569  return app570}571572function tryRequire (name) {573  try {574    return require(name)575  } catch (e) {576    return {}577  }578}