Skip to content

Commit ef7c956

Browse files
authored
Improve organization and rigor of tests (#562)
* [adhoc] Tidying up cookie, and reqBodyParser tests. * factor urlParsing to not use httpbin
1 parent c5574ea commit ef7c956

File tree

8 files changed

+272
-200
lines changed

8 files changed

+272
-200
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ node_modules
77

88
coverage/
99
coverage.lcov
10-
.nyc_output/
10+
.nyc_output/
11+
tmp/

test/bodyEncoding.js

Lines changed: 0 additions & 161 deletions
This file was deleted.

test/catchingErrors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe('when server responds with an error', function () {
1919
app = express();
2020
});
2121

22-
afterEach(function () {
23-
serverReference.close();
22+
afterEach(async function () {
23+
await serverReference.close();
2424
});
2525

2626
var STATUS_CODES = [

test/cookies.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,44 @@ var proxyRouteFn = [{
1111
method: 'get',
1212
path: '/cookieTest',
1313
fn: function (req, res) {
14-
Object.keys(req.cookies).forEach(function (key) {
14+
// This stub proxy server method simply copies the cookies from the inbound
15+
// request to the outbound response. This lets us check the proxy reponse
16+
// for the cookies on the request.
17+
Object.keys(req.cookies).forEach(key => {
1518
res.cookie(key, req.cookies[key]);
1619
});
1720
res.sendStatus(200);
1821
}
1922
}];
2023

21-
describe('proxies cookie', function () {
22-
this.timeout(TIMEOUT.STANDARD);
24+
describe("cookies", () => {
25+
describe('when cookies are sent on the user request', function () {
26+
this.timeout(TIMEOUT.STANDARD);
2327

24-
var app;
25-
var proxyServer;
28+
var app;
29+
var proxyServer;
2630

27-
beforeEach(function () {
28-
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
29-
app = express();
30-
app.use(proxy('localhost:12346'));
31-
});
31+
beforeEach(function () {
32+
proxyServer = proxyTarget(12346, 100, proxyRouteFn);
33+
app = express();
34+
app.use(proxy('localhost:12346'));
35+
});
3236

33-
afterEach(function () {
34-
proxyServer.close();
35-
});
37+
afterEach(function () {
38+
proxyServer.close();
39+
});
3640

37-
it('set cookie', function (done) {
38-
request(app)
39-
.get('/cookieTest')
40-
.set('Cookie', 'myApp-token=12345667')
41-
.end(function (err, res) {
42-
var cookiesMatch = res.headers['set-cookie'].filter(function (item) {
43-
return item.match(/myApp-token=12345667/);
41+
it('they are copied to the proxy request', function (done) {
42+
request(app)
43+
.get('/cookieTest')
44+
.set('Cookie', 'myApp-token=12345667')
45+
.end(function (err, res) {
46+
var cookiesMatch = res.headers['set-cookie'].filter(function (item) {
47+
return item.match(/myApp-token=12345667/);
48+
});
49+
assert(cookiesMatch);
50+
done(err);
4451
});
45-
assert(cookiesMatch);
46-
done(err);
47-
});
52+
});
4853
});
4954
});

test/proxyReqPathResolver.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('resolveProxyReqPath', function () {
1515

1616
this.timeout(TIMEOUT.STANDARD);
1717

18-
before(function () {
18+
beforeEach(function () {
1919
var handlers = [{
2020
method: 'get',
2121
path: '/working',
@@ -27,8 +27,8 @@ describe('resolveProxyReqPath', function () {
2727
server = proxyTarget(12345, 100, handlers);
2828
});
2929

30-
after(function () {
31-
server.close();
30+
afterEach(async () => {
31+
await server.close();
3232
});
3333

3434
aliases.forEach(function (alias) {

test/rawBodyLimit.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)