Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#399]: Clarifies behavior on empty response body. #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,32 @@ Promise form:

```js
app.use(proxy('localhost:12346', {
filter: function (req, res) {
return new Promise(function (resolve) {
filter: function (req, res) {
return new Promise(function (resolve) {
resolve(req.method === 'GET');
});
});
}
}));
```

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

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

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

Note: `proxyResData` will be an empty buffer when the proxied response is empty.

```js
app.use('/proxy', proxy('www.google.com', {
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
if (proxyResData.length == 0) {
// body is empty, don't try to parse it
// return alternate document instead.
return JSON.stringify({});
}
data = JSON.parse(proxyResData.toString('utf8'));
data.newProperty = 'exciting data';
return JSON.stringify(data);
Expand Down Expand Up @@ -562,9 +569,9 @@ app.use('/', proxy('internalhost.example.com', {
| --- | --- |
| 1.5.1 | Fixes bug in stringifying debug messages. |
| 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. |
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
| 1.4.0 | DEPRECATED. Critical bug in the `filter` api.|
| 1.3.0 | DEPRECATED. Critical bug in the `filter` api. `filter` now supports Promises. Update linter to eslint. |
| 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. |
| 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. |
| 1.1.0 | Add step to allow response headers to be modified.
| 1.0.7 | Update dependencies. Improve docs on promise rejection. Fix promise rejection on body limit. Improve debug output. |
| 1.0.6 | Fixes preserveHostHdr not working, skip userResDecorator on 304, add maybeSkipToNext, test improvements and cleanup. |
Expand Down
39 changes: 39 additions & 0 deletions test/userResDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require('assert');
var express = require('express');
var request = require('supertest');
var proxy = require('../');
var http = require('http');

describe('userResDecorator', function () {

Expand Down Expand Up @@ -39,6 +40,44 @@ describe('userResDecorator', function () {
});
});

describe('when handling a response with no body', function () {
this.timeout(10000);

var app;
var noBodyTarget;
var serverReference;
var responseCode = 200;

beforeEach(function () {
app = express();
noBodyTarget = new http.Server();
noBodyTarget.on('request', function (req, res) {
res.writeHead(responseCode, { 'Content-Length': '0' });
res.end();
});
serverReference = noBodyTarget.listen(12346);
});

afterEach(function () {
serverReference.close();
});


it('returns an empty Buffer for the proxyResData', function (done) {
app.use('/proxy', proxy('http://127.0.0.1:12346', {
userResDecorator: function (proxyRes, proxyResData /*, userReq, userRes */) {
assert(Buffer.isBuffer(proxyResData));
assert(proxyResData.length === 0);
}
}));

request(app)
.get('/proxy')
.expect(200)
.end(done);
});
});

it('has access to original response', function (done) {
var app = express();
app.use(proxy('httpbin.org', {
Expand Down