Skip to content

Commit

Permalink
cache bug and 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 13, 2013
1 parent 759c6fc commit bea40f1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ exports.parse = function (res, callback) {

res.once('error', function (err) {

return finish(Boom.internal('Proxy response error', err));
return finish(Boom.internal('Client response error', err));
});

res.once('close', function () {
Expand Down
5 changes: 3 additions & 2 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internals.Proxy.prototype.handler = function () {
return request.reply(err);
}

return self.settings.postResponse(request, self.settings, res, buffer);
return self.settings.postResponse(request, self.settings, res, buffer.toString());
});
});
});
Expand Down Expand Up @@ -136,4 +136,5 @@ internals.postResponse = function (request, settings, res, payload) {
if (contentType) {
response.type(contentType);
}
};
};

51 changes: 40 additions & 11 deletions test/integration/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Fs = require('fs');
var Zlib = require('zlib');
var Request = require('request');
var Hapi = require('../..');
var Client = require('../../lib/client');


// Declare internals
Expand Down Expand Up @@ -39,26 +40,28 @@ describe('Proxy', function () {
}

var profile = {
'id': 'fa0dbda9b1b',
'name': 'John Doe'
id: 'fa0dbda9b1b',
name: 'John Doe'
};

this.reply(profile).state('test', '123');
};

var activeCount = 0;
var activeItem = function () {

this.reply({
'id': '55cf687663',
'name': 'Active Item'
id: '55cf687663',
name: 'Active Item',
count: activeCount++
});
};

var item = function () {

this.reply({
'id': '55cf687663',
'name': 'Item'
id: '55cf687663',
name: 'Item'
}).created('http://example.com');
};

Expand Down Expand Up @@ -134,7 +137,8 @@ describe('Proxy', function () {
{ method: 'GET', path: '/gzipstream', handler: gzipStreamHandler },
{ method: 'GET', path: '/redirect', handler: redirectHandler },
{ method: 'POST', path: '/post1', handler: function () { this.reply.redirect('/post2').rewritable(false); } },
{ method: 'POST', path: '/post2', handler: function () { this.reply(this.payload); } }
{ method: 'POST', path: '/post2', handler: function () { this.reply(this.payload); } },
{ method: 'GET', path: '/cached', handler: profile }
]);

var mapUri = function (request, callback) {
Expand Down Expand Up @@ -169,7 +173,8 @@ describe('Proxy', function () {
{ method: 'GET', path: '/googler', handler: { proxy: { mapUri: function (request, callback) { callback(null, 'http://google.com'); }, redirects: 1 } } },
{ method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: backendPort, passThrough: true, redirects: 2 } } },
{ method: 'POST', path: '/post1', handler: { proxy: { host: 'localhost', port: backendPort, redirects: 3 } }, config: { payload: 'stream' } },
{ method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8' } } }
{ method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8' } } },
{ method: 'GET', path: '/cached', handler: { proxy: { host: 'localhost', port: backendPort } }, config: { cache: routeCache } }
]);

server.state('auto', { autoValue: 'xyz' });
Expand Down Expand Up @@ -296,13 +301,20 @@ describe('Proxy', function () {
});
});

it('forwards on the response when making a GET request to a route that also accepts a POST', function (done) {
it('request a cached proxy route', function (done) {

server.inject('/item', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('Active Item');
done();
var counter = res.result.count;

server.inject('/item', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result.count).to.equal(counter);
done();
});
});
});

Expand Down Expand Up @@ -438,4 +450,21 @@ describe('Proxy', function () {
done();
});
});
});

it('errors on invalid response stream', function (done) {

var orig = Client.parse;
Client.parse = function (res, callback) {

Client.parse = orig;
callback(Hapi.error.internal('Fake error'));
};

server.inject('/cached', function (res) {

expect(res.statusCode).to.equal(500);
done();
});
});
});

0 comments on commit bea40f1

Please sign in to comment.