Skip to content

Commit

Permalink
allow replying with a stream as returned by node core http client met…
Browse files Browse the repository at this point in the history
…hods, closes #2301
  • Loading branch information
nlf committed Dec 18, 2014
1 parent 0aea0f3 commit bc313d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/response.js
Expand Up @@ -447,7 +447,7 @@ internals.Response.prototype._marshal = function (next) {
internals.Response.prototype._streamify = function (source, next) {

if (source instanceof Stream) {
if (source._readableState.objectMode) {
if (source.socket ? source.socket._readableState.objectMode : source._readableState.objectMode) {
return next(Boom.badImplementation('Cannot reply with stream in object mode'));
}

Expand Down
28 changes: 28 additions & 0 deletions test/reply.js
@@ -1,5 +1,6 @@
// Load modules

var Http = require('http');
var Stream = require('stream');
var Bluebird = require('bluebird');
var Boom = require('boom');
Expand Down Expand Up @@ -249,6 +250,33 @@ describe('Reply', function () {
});
});

it('responds with an http client stream reply', function (done) {

var handler = function (request, reply) {

reply('just a string');
};

var streamHandler = function (request, reply) {

reply(Http.get(request.server.info + '/'));
};

var server = new Hapi.Server({ debug: false });
server.connection();
server.route({ method: 'GET', path: '/', handler: handler });
server.route({ method: 'GET', path: '/stream', handler: streamHandler });

server.start(function () {

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

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

it('errors on objectMode stream reply', function (done) {

var TestStream = function () {
Expand Down

0 comments on commit bc313d1

Please sign in to comment.