Skip to content

Commit 1ab27ce

Browse files
author
Nik 'Fire Eater' Krimm
committed
[#399]: Clarifies behavior on empty response body.
1 parent 03f6c03 commit 1ab27ce

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,32 @@ Promise form:
142142

143143
```js
144144
app.use(proxy('localhost:12346', {
145-
filter: function (req, res) {
146-
return new Promise(function (resolve) {
145+
filter: function (req, res) {
146+
return new Promise(function (resolve) {
147147
resolve(req.method === 'GET');
148-
});
148+
});
149149
}
150150
}));
151151
```
152152

153153
Note that in the previous example, `resolve(false)` will execute the happy path
154154
for filter here (skipping the rest of the proxy, and calling `next()`).
155-
`reject()` will also skip the rest of proxy and call `next()`.
155+
`reject()` will also skip the rest of proxy and call `next()`.
156156

157157
#### userResDecorator (was: intercept) (supports Promise)
158158

159159
You can modify the proxy's response before sending it to the client.
160160

161+
Note: `proxyResData` will be an empty buffer when the proxied response is empty.
162+
161163
```js
162164
app.use('/proxy', proxy('www.google.com', {
163165
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
166+
if (proxyResData.length == 0) {
167+
// body is empty, don't try to parse it
168+
// return alternate document instead.
169+
return JSON.stringify({});
170+
}
164171
data = JSON.parse(proxyResData.toString('utf8'));
165172
data.newProperty = 'exciting data';
166173
return JSON.stringify(data);
@@ -562,9 +569,9 @@ app.use('/', proxy('internalhost.example.com', {
562569
| --- | --- |
563570
| 1.5.1 | Fixes bug in stringifying debug messages. |
564571
| 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. |
565-
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
572+
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
566573
| 1.3.0 | DEPRECATED. Critical bug in the `filter` api. `filter` now supports Promises. Update linter to eslint. |
567-
| 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. |
574+
| 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. |
568575
| 1.1.0 | Add step to allow response headers to be modified.
569576
| 1.0.7 | Update dependencies. Improve docs on promise rejection. Fix promise rejection on body limit. Improve debug output. |
570577
| 1.0.6 | Fixes preserveHostHdr not working, skip userResDecorator on 304, add maybeSkipToNext, test improvements and cleanup. |

test/userResDecorator.js

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7+
var http = require('http');
78

89
describe('userResDecorator', function () {
910

@@ -39,6 +40,44 @@ describe('userResDecorator', function () {
3940
});
4041
});
4142

43+
describe('when handling a response with no body', function () {
44+
this.timeout(10000);
45+
46+
var app;
47+
var noBodyTarget;
48+
var serverReference;
49+
var responseCode = 200;
50+
51+
beforeEach(function () {
52+
app = express();
53+
noBodyTarget = new http.Server();
54+
noBodyTarget.on('request', function (req, res) {
55+
res.writeHead(responseCode, { 'Content-Length': '0' });
56+
res.end();
57+
});
58+
serverReference = noBodyTarget.listen(12346);
59+
});
60+
61+
afterEach(function () {
62+
serverReference.close();
63+
});
64+
65+
66+
it('returns an empty Buffer for the proxyResData', function (done) {
67+
app.use('/proxy', proxy('http://127.0.0.1:12346', {
68+
userResDecorator: function (proxyRes, proxyResData, /* userReq, userRes */) {
69+
assert(Buffer.isBuffer(proxyResData));
70+
assert(proxyResData.length == 0);
71+
}
72+
}));
73+
74+
request(app)
75+
.get('/proxy')
76+
.expect(200)
77+
.end(done);
78+
});
79+
});
80+
4281
it('has access to original response', function (done) {
4382
var app = express();
4483
app.use(proxy('httpbin.org', {

0 commit comments

Comments
 (0)