Skip to content

Commit c5574ea

Browse files
authored
Incremental cleanup and organization of test files. (#561)
* Improve clarity of test names. * improve timeout tests * minor tweaks to organization * expand headers test
1 parent 2048e8f commit c5574ea

11 files changed

+841
-653
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
],
152152
"lines-around-comment": [
153153
2,
154-
{ "afterLineComment": true, "allowBlockEnd": true }
154+
{ "afterLineComment": false, "allowBlockEnd": true }
155155
],
156156
"semi": [
157157
2,

README.md

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# express-http-proxy [![NPM version](https://badge.fury.io/js/express-http-proxy.svg)](http://badge.fury.io/js/express-http-proxy) [![Build Status](https://travis-ci.org/villadora/express-http-proxy.svg?branch=master)](https://travis-ci.org/villadora/express-http-proxy)
1+
# express-http-proxy [![NPM version](https://badge.fury.io/js/express-http-proxy.svg)](http://badge.fury.io/js/express-http-proxy) [![Build Status](https://travis-ci.org/villadora/express-http-proxy.svg?branch=master)](https://travis-ci.org/villadora/express-http-proxy)
22

33

44
Express middleware to proxy request to another host and pass response back to original caller.
@@ -24,6 +24,51 @@ var app = require('express')();
2424
app.use('/proxy', proxy('www.google.com'));
2525
```
2626

27+
### 30k view
28+
29+
The proxy middleware:
30+
* proxies request to your server to an arbitrary server, and
31+
* provide hooks to decorate and filter requests to the proxy target, and
32+
* provide hooks you to decorate and filter proxy responses before returning them to the client.
33+
34+
```
35+
36+
Client Express App Proxy Middleware Target Server
37+
| | | |
38+
| HTTP Request | | |
39+
|-------------------------->| | |
40+
| | Request | |
41+
| |--------------------------->| |
42+
| | | +------------------------+ |
43+
| | | | Request Preprocessing | |
44+
| | | | 1. filter requests | |
45+
| | | | 2. resolve proxy host | |
46+
| | | | 3. decorate proxy opts | |
47+
| | | | 4. decorate proxy req | |
48+
| | | | 5. resolve req path | |
49+
| | | +------------------------+ |
50+
| | | Forwarded Request |
51+
| | |---------------------------->|
52+
| | | |
53+
| | | Response with Headers |
54+
| | |<----------------------------|
55+
| | | |
56+
| | | +------------------------+ |
57+
| | | | Response Processing | |
58+
| | | | 1. skip to next? | |
59+
| | | | 2. copy proxy headers | |
60+
| | | | 3. decorate headers | |
61+
| | | | 4. decorate response | |
62+
| | | +------------------------+ |
63+
| | | |
64+
| | Modified Response | |
65+
| |<---------------------------| |
66+
| Final Response | | |
67+
|<--------------------------| | |
68+
| | | |
69+
70+
```
71+
2772
### Streaming
2873

2974
Proxy requests and user responses are piped/streamed/chunked by default.
@@ -74,7 +119,7 @@ function selectProxyHost() {
74119
app.use('/', proxy(selectProxyHost));
75120
```
76121

77-
Notie: Host is only the host name. Any params after in url will be ignored. For ``http://google.com/myPath`, ``myPath`` will be ignored because the host name is ``google.com``.
122+
Notie: Host is only the host name. Any params after in url will be ignored. For ``http://google.com/myPath`, ``myPath`` will be ignored because the host name is ``google.com``.
78123
See ``proxyReqPathResolver`` for more detailed path information.
79124

80125

@@ -172,17 +217,17 @@ Promise form:
172217

173218
```js
174219
app.use(proxy('localhost:12346', {
175-
filter: function (req, res) {
176-
return new Promise(function (resolve) {
220+
filter: function (req, res) {
221+
return new Promise(function (resolve) {
177222
resolve(req.method === 'GET');
178-
});
223+
});
179224
}
180225
}));
181226
```
182227

183228
Note that in the previous example, `resolve(false)` will execute the happy path
184229
for filter here (skipping the rest of the proxy, and calling `next()`).
185-
`reject()` will also skip the rest of proxy and call `next()`.
230+
`reject()` will also skip the rest of proxy and call `next()`.
186231

187232
#### userResDecorator (was: intercept) (supports Promise)
188233

@@ -276,8 +321,8 @@ first request.
276321

277322
When a `userResHeaderDecorator` is defined, the return of this method will replace (rather than be merged on to) the headers for `userRes`.
278323

279-
> Note that by default, headers from the PROXY response CLOBBER all headers that may have previously been set on the userResponse.
280-
> Authors have the option of constructing any combination of proxyRes and userRes headers in the `userResHeaderDecorator`.
324+
> Note that by default, headers from the PROXY response CLOBBER all headers that may have previously been set on the userResponse.
325+
> Authors have the option of constructing any combination of proxyRes and userRes headers in the `userResHeaderDecorator`.
281326
> Check the tests for this method for examples.
282327
283328

@@ -608,9 +653,9 @@ app.use('/', proxy('internalhost.example.com', {
608653
| 1.6.0 | Do gzip and gunzip aysyncronously. Test and documentation improvements, dependency updates. |
609654
| 1.5.1 | Fixes bug in stringifying debug messages. |
610655
| 1.5.0 | Fixes bug in `filter` signature. Fix bug in skipToNextHandler, add expressHttpProxy value to user res when skipped. Add tests for host as ip address. |
611-
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
656+
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
612657
| 1.3.0 | DEPRECATED. Critical bug in the `filter` api. `filter` now supports Promises. Update linter to eslint. |
613-
| 1.2.0 | Auto-stream when no decorations are made to req/res. Improved docs, fixes issues in maybeSkipToNexthandler, allow authors to manage error handling. |
658+
| 1.2.0 | Auto-stream when no decorations are made to req/res. Improved docs, fixes issues in maybeSkipToNexthandler, allow authors to manage error handling. |
614659
| 1.1.0 | Add step to allow response headers to be modified.
615660
| 1.0.7 | Update dependencies. Improve docs on promise rejection. Fix promise rejection on body limit. Improve debug output. |
616661
| 1.0.6 | Fixes preserveHostHdr not working, skip userResDecorator on 304, add maybeSkipToNext, test improvements and cleanup. |

test/decorateUserResHeaders.js

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7-
var proxyTarget = require('./support/proxyTarget');
87
var TIMEOUT = require('./constants');
98

10-
describe('when userResHeaderDecorator is defined', function () {
11-
this.timeout(TIMEOUT.STANDARD);
12-
9+
describe('userResHeaderDecorator', function () {
1310
var app;
14-
var serverReference;
15-
16-
afterEach(function () {
17-
serverReference.close();
18-
});
11+
var serverReference;
1912

2013
beforeEach(function () {
2114
app = express();
@@ -32,80 +25,84 @@ describe('when userResHeaderDecorator is defined', function () {
3225
serverReference.close();
3326
});
3427

35-
it('can delete a header', function (done) {
36-
app.use('/proxy', proxy('http://127.0.0.1:12345', {
37-
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
38-
delete headers['x-my-secret-header'];
39-
return headers;
40-
}
41-
}));
28+
describe('header modification', function () {
29+
it('can remove headers from the response', function (done) {
30+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
31+
userResHeaderDecorator: function (headers) {
32+
delete headers['x-my-secret-header'];
33+
return headers;
34+
}
35+
}));
4236

43-
app.use(function (req, res) {
44-
res.sendStatus(200);
37+
request(app)
38+
.get('/proxy')
39+
.expect(function (res) {
40+
assert(Object.keys(res.headers).indexOf('x-my-not-so-secret-header') > -1);
41+
assert(Object.keys(res.headers).indexOf('x-my-secret-header') === -1);
42+
})
43+
.end(done);
4544
});
4645

47-
request(app)
48-
.get('/proxy')
49-
.expect(function (res) {
50-
assert(Object.keys(res.headers).indexOf('x-my-not-so-secret-header') > -1);
51-
assert(Object.keys(res.headers).indexOf('x-my-secret-header') === -1);
52-
})
53-
.end(done);
54-
});
55-
56-
it('provides an interface for updating headers', function (done) {
57-
app.use('/proxy', proxy('http://127.0.0.1:12345', {
58-
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
59-
headers.boltedonheader = 'franky';
60-
return headers;
61-
}
62-
}));
46+
it('can add new headers to the response', function (done) {
47+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
48+
userResHeaderDecorator: function (headers) {
49+
headers.boltedonheader = 'franky';
50+
return headers;
51+
}
52+
}));
6353

64-
app.use(function (req, res) {
65-
res.sendStatus(200);
54+
request(app)
55+
.get('/proxy')
56+
.expect(function (res) {
57+
assert(res.headers.boltedonheader === 'franky');
58+
})
59+
.end(done);
6660
});
67-
68-
request(app)
69-
.get('/proxy')
70-
.expect(function (res) {
71-
assert(res.headers.boltedonheader === 'franky');
72-
})
73-
.end(done);
7461
});
7562

76-
it('author has option to copy proxyResponse headers to userResponse', function (done) {
77-
app.use('/proxy', proxy('http://127.0.0.1:12345', {
78-
userResHeaderDecorator: function (headers, userReq) { // proxyReq
79-
// Copy specific headers from the proxy request to the user response
80-
//
81-
// We can copy them to new name
82-
if (userReq.headers['x-custom-header']) {
83-
headers['x-proxied-custom-header'] = userReq.headers['x-custom-header'];
84-
}
85-
if (userReq.headers['x-user-agent']) {
86-
headers['x-proxied-user-agent'] = userReq.headers['x-user-agent'];
63+
describe('header proxying', function () {
64+
it('can copy request headers to response with new names', function (done) {
65+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
66+
userResHeaderDecorator: function (headers, userReq) {
67+
if (userReq.headers['x-custom-header']) {
68+
headers['x-proxied-custom-header'] = userReq.headers['x-custom-header'];
69+
}
70+
if (userReq.headers['x-user-agent']) {
71+
headers['x-proxied-user-agent'] = userReq.headers['x-user-agent'];
72+
}
73+
return headers;
8774
}
75+
}));
8876

89-
// We can copy them to the same name
90-
headers['x-copied-header-1'] = userReq.headers['x-copied-header-1'];
91-
headers['x-copied-header-2'] = userReq.headers['x-copied-header-2'];
92-
return headers;
93-
}
94-
}));
77+
request(app)
78+
.get('/proxy')
79+
.set('x-custom-header', 'custom-value')
80+
.set('x-user-agent', 'test-agent')
81+
.expect(function (res) {
82+
assert.equal(res.headers['x-proxied-custom-header'], 'custom-value');
83+
assert.equal(res.headers['x-proxied-user-agent'], 'test-agent');
84+
})
85+
.end(done);
86+
});
9587

96-
request(app)
97-
.get('/proxy')
98-
.set('x-custom-header', 'custom-value')
99-
.set('x-user-agent', 'test-agent')
100-
.set('x-copied-header-1', 'value1')
101-
.set('x-copied-header-2', 'value2')
102-
.expect(function (res) {
103-
// Verify the original headers were proxied to the response
104-
assert.equal(res.headers['x-proxied-custom-header'], 'custom-value');
105-
assert.equal(res.headers['x-proxied-user-agent'], 'test-agent');
106-
assert.equal(res.headers['x-copied-header-1'], 'value1');
107-
assert.equal(res.headers['x-copied-header-2'], 'value2');
108-
})
109-
.end(done);
88+
it('can copy request headers to response with same names', function (done) {
89+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
90+
userResHeaderDecorator: function (headers, userReq) {
91+
headers['x-copied-header-1'] = userReq.headers['x-copied-header-1'];
92+
headers['x-copied-header-2'] = userReq.headers['x-copied-header-2'];
93+
return headers;
94+
}
95+
}));
96+
97+
request(app)
98+
.get('/proxy')
99+
.set('x-copied-header-1', 'value1')
100+
.set('x-copied-header-2', 'value2')
101+
.expect(function (res) {
102+
assert.equal(res.headers['x-copied-header-1'], 'value1');
103+
assert.equal(res.headers['x-copied-header-2'], 'value2');
104+
})
105+
.end(done);
106+
});
110107
});
111108
});

0 commit comments

Comments
 (0)