Skip to content

Commit

Permalink
[fixed] fix a bug where binary files were not being properly encoded
Browse files Browse the repository at this point in the history
when proxied

Proxy server is not working properly when it requests url which expected
to take binary data as response. The problem is encoding in request
options is not set properly. According to 'request' package document,
those requests expected for binary data should set encoding to null. Thus
a new parameter added to 'proxy' function is to set proper encoding to
different situation. [default: undefined]
  • Loading branch information
pilagod authored and jergason committed Jul 7, 2015
1 parent 03ad4b4 commit 3547da5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
.idea
npm-debug.log
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var debug = require('debug')('koa-pixie-proxy');
var hasColons = /:/;

function pixie(options) {
return function proxy(path) {
return function proxy(path, encoding) {
var shouldReplacePathParams = hasColons.test(path);

return function* (next) {
Expand All @@ -15,7 +15,8 @@ function pixie(options) {
url: options.host + (path || this.url),
method: this.method,
headers: this.headers,
qs: this.query
qs: this.query,
encoding: encoding
};

// if we have dynamic segments in the url
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"mocha": "2.0.1",
"supertest": "0.15.0"
}
}
}
30 changes: 29 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var koa = require('koa');
var supertest = require('supertest');
var assert = require('assert');
var http = require('http');
var fs = require('fs');
var serve = require('koa-static');
var router = require('koa-router');
var body = require('koa-body');


function getRandomPort() {
return Math.ceil(Math.random() * 5000 + 5000);
}
Expand Down Expand Up @@ -40,6 +40,7 @@ function makeTestServer() {
this.set('x-some-dumb-header', 'Im-set-yo');
this.body = this.request.body;
});

return http.createServer(app.callback())
}

Expand Down Expand Up @@ -218,4 +219,31 @@ describe('pixie-proxy', function() {
});
});
});

it('proxies request binary data(image, compressed file, etc.)', function(done){
var testServer = makeTestServer();
var PORT = getRandomPort();
testServer.listen(PORT, function() {

var app = koa();
app.use(body());
app.use(router(app));

var proxy = pixie({host: 'http://localhost:' + PORT});

app.get('/static/mystery.gif', proxy('', null));
supertest(http.createServer(app.callback()))
.get('/static/mystery.gif')
.expect(200)
.expect('Content-Type', 'image/gif')
.end(function(err, res){
assert.ifError(err);
fs.readFile(__dirname + '/static/mystery.gif', 'binary', function(err, data){
assert.equal(res.header['content-length'], data.length);
testServer.close();
done();
});
});
});
});
});

0 comments on commit 3547da5

Please sign in to comment.