Skip to content

Commit

Permalink
bug: force json body if json and remove excess headers
Browse files Browse the repository at this point in the history
  • Loading branch information
scottcorgan committed Oct 10, 2014
1 parent 8d78118 commit 51f630c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = function () {

// Remove request origin
delete req.headers.host;
delete req.headers.origin; // TODO: test this
delete req.headers.referer; // TODO: test this

stack.use(bodyParser.json());
stack.use(bodyParser.raw());
Expand All @@ -47,7 +49,15 @@ module.exports = function () {
timeout: (config.timeout)*1000 || DEFAULT_TIMEOUT
};

if (req.body) options.body = JSON.stringify(req.body);
if (req.body && Object.keys(req.body).length > 0) {
try {
var body = JSON.parse(req.body);
options.json = true;
}
catch (e) {}

options.body = JSON.stringify(req.body);
}

request(options).pipe(res);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superstatic-proxy",
"version": "1.0.0",
"version": "1.0.1",
"description": "A Superstatic service for HTTP proxying of AJAX requests",
"main": "index.js",
"directories": {
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ describe('Superstatic Proxy', function () {
.end(done);
});

it('proxies a DELETE request', function (done) {

request(app)
.delete('/__/proxy/api/users.json')
.expect(200)
.expect(function (data) {
expect(data.res.body.method).to.equal('DELETE');
})
.end(done);
});

it('proxies a request, ignoring the proxy name case', function (done) {
request(app)
.post('/__/proxy/Api/users.json')
Expand Down

0 comments on commit 51f630c

Please sign in to comment.