Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from stoneChen/master
Browse files Browse the repository at this point in the history
Add missing option `limit` and  optimize the promise
  • Loading branch information
Nicholas Simmons authored Jul 24, 2017
2 parents a644fc8 + d90da5f commit 491c2cf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
6 changes: 2 additions & 4 deletions app/steps/buildProxyReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ function buildProxyReq(Container) {
var parseBody = (!options.parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(ctx, options);
var createReqOptions = requestOptions.create(ctx, options, host);

return new Promise(function(resolve) {
Promise
return Promise
.all([parseBody, createReqOptions])
.then(function(responseArray) {
Container.proxy.bodyContent = responseArray[0];
Container.proxy.reqBuilder = responseArray[1];
resolve(Container);
return Container;
});
});
}

module.exports = buildProxyReq;
3 changes: 2 additions & 1 deletion lib/resolveOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function resolveOptions(options) {
https: options.https,
port: options.port,
reqAsBuffer: options.reqAsBuffer,
timeout: options.timeout
timeout: options.timeout,
limit: options.limit,
};
}

Expand Down
69 changes: 44 additions & 25 deletions test/bodyEncoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('body encoding', function() {
'0d0a2db4000000004' +
'9454e44ae426082';
var pngData = new Buffer(pngHex, 'hex');
var largePngData = new Buffer(pngHex.repeat(100000), 'hex'); // 6.7MB

it('allow raw data', function(done) {
var filename = os.tmpdir() + '/koa-better-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
Expand Down Expand Up @@ -54,6 +55,49 @@ describe('body encoding', function() {

});

// In this case, the package `raw-body` will print error stack which does not mater.
it('should get 413 by posting file which is larger than 1mb without setting limit', function(done) {
var filename = os.tmpdir() + '/koa-better-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
var app = new Koa();
app.use(proxy('localhost:8109', {
}));
fs.writeFile(filename, largePngData, function(err) {
if (err) { throw err; }
agent(app.callback())
.post('/post')
.attach('image', filename)
.expect(413)
.end(function(err) {
fs.unlink(filename);
assert(err === null);
if (err) { return done(err); }
done();
});
});
});

it('should not fail on large limit', function(done) {
var filename = os.tmpdir() + '/koa-better-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
var app = new Koa();
app.use(proxy('localhost:8109', {
// This case `parseReqBody` should not be set to false,
limit: '20gb',
}));
fs.writeFile(filename, largePngData, function(err) {
if (err) { throw err; }
agent(app.callback())
.post('/post')
.attach('image', filename)
.expect(200)
.end(function(err) {
fs.unlink(filename);
assert(err === null);
if (err) { return done(err); }
done();
});
});
});

describe('when user sets parseReqBody', function() {

it('should not parse body', function(done) {
Expand Down Expand Up @@ -85,31 +129,6 @@ describe('body encoding', function() {
});
});

it('should not fail on large limit', function(done) {
var filename = os.tmpdir() + '/koa-better-http-proxy-test-' + (new Date()).getTime() + '-png-transparent.png';
var app = new Koa();
app.use(proxy('localhost:8109', {
parseReqBody: false,
limit: '20gb',
}));
fs.writeFile(filename, pngData, function(err) {
if (err) { throw err; }
agent(app.callback())
.post('/post')
.attach('image', filename)
.end(function(err) {
fs.unlink(filename);
assert(err === null);
// This test is both broken and I think unnecessary.
// Its broken because http.bin no longer supports /post, but this test assertion is based on the old
// httpbin behavior.
// The assertion in the proxyReqOptDecorator above verifies the test title.
//var response = new Buffer(res.body.attachment.data).toString('base64');
//assert(response.indexOf(pngData.toString('base64')) >= 0, 'response should include original raw data');
done(err);
});
});
});
});

describe('when user sets reqBodyEncoding', function() {
Expand Down

0 comments on commit 491c2cf

Please sign in to comment.