|
| 1 | +'use strict'; |
| 2 | +var getRawBody = require('raw-body'); |
| 3 | +var assert = require('assert'); |
| 4 | +var express = require('express'); |
| 5 | +var request = require('supertest'); |
| 6 | +var fs = require('fs'); |
| 7 | +var os = require('os'); |
| 8 | +var proxy = require('../'); |
| 9 | +var startProxyTarget = require('./support/proxyTarget'); |
| 10 | +var TIMEOUT = require('./constants'); |
| 11 | + |
| 12 | +const PORT_NUMBER =8109; |
| 13 | + |
| 14 | +const pngHex = '89504e470d0a1a0a0' + |
| 15 | + '000000d4948445200' + |
| 16 | + '00000100000001080' + |
| 17 | + '60000001f15c48900' + |
| 18 | + '00000a49444154789' + |
| 19 | + 'c6300010000050001' + |
| 20 | + '0d0a2db4000000004' + |
| 21 | + '9454e44ae426082'; |
| 22 | +const pngData = Buffer.from(pngHex, 'hex'); |
| 23 | + |
| 24 | +const processStuff = (req, res, next) => { |
| 25 | + getRawBody(req, { |
| 26 | + length: req.headers['content-length'], |
| 27 | + }).then((rawBody) => { |
| 28 | + res.json({rawBody, headers: req.headers}); |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +describe('limit', function () { |
| 33 | + var server; |
| 34 | + |
| 35 | + beforeEach(function () { |
| 36 | + server = startProxyTarget(8109); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async function () { |
| 40 | + await server.close(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not fail on large limit', function (done) { |
| 44 | + var filename = os.tmpdir() + '/express-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png'; |
| 45 | + var app = express(); |
| 46 | + app.use(proxy('localhost:8109', { |
| 47 | + parseReqBody: false, |
| 48 | + limit: '20gb', |
| 49 | + })); |
| 50 | + fs.writeFile(filename, pngData, function (err) { |
| 51 | + if (err) { throw err; } |
| 52 | + request(app) |
| 53 | + .post('/post') |
| 54 | + .attach('image', filename) |
| 55 | + .end(function (err) { |
| 56 | + fs.unlinkSync(filename); |
| 57 | + assert(err === null); |
| 58 | + // This test is both broken and I think unnecessary. |
| 59 | + // Its broken because http.bin no longer supports /post, but this test assertion is based on the old |
| 60 | + // httpbin behavior. |
| 61 | + // The assertion in the decorateRequest above verifies the test title. |
| 62 | + //var response = new Buffer(res.body.attachment.data).toString('base64'); |
| 63 | + //assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data'); |
| 64 | + |
| 65 | + done(err); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + it('should fail with an error when exceeding limit', function (done) { |
| 70 | + var app = express(); |
| 71 | + app.use(proxy('localhost:8109', { |
| 72 | + limit: 1, |
| 73 | + })); |
| 74 | + // silence jshint warning about unused vars - express error handler *needs* 4 args |
| 75 | + app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars |
| 76 | + res.json(err); |
| 77 | + }); |
| 78 | + request(app) |
| 79 | + .post('/post') |
| 80 | + .send({ some: 'json' }) |
| 81 | + .end(function (err, response) { |
| 82 | + assert(response.body.message === 'request entity too large'); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments