Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
request: add support for streaming bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
aredridel authored and othiym23 committed Sep 3, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0b595c4 commit f11a002
Showing 2 changed files with 86 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
@@ -146,13 +146,39 @@ function makeRequest (uri, params, cb_) {
this.log.http('request', params.method, parsed.href || '/')

var done = requestDone.call(this, params.method, uri, cb)
var req = request(opts, decodeResponseBody(done))
var req = request(opts, params.streaming ? undefined : decodeResponseBody(done))

req.on('error', cb)
req.on('socket', function (s) {
s.on('error', cb)
})

if (params.streaming) {
req.on('response', function (response) {

if (response.statusCode >= 400) {
var parts = []
response.on('data', function (data) {
parts.push(data)
})
response.on('end', function () {
decodeResponseBody(done)(null, response, Buffer.concat(parts))
})
} else {

response.on('end', function () {
// don't ever re-use connections that had server errors.
// those sockets connect to the Bad Place!
if (response.socket && response.statusCode > 500) {
response.socket.destroy()
}
})

return cb(null, response)
}
})
}

if (params.body && (params.body instanceof Stream)) {
params.body.pipe(req)
}
59 changes: 59 additions & 0 deletions test/fetch-streaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var test = require('tap').test
var concat = require('concat-stream')

var server = require('./lib/server.js')
var common = require('./lib/common.js')
var client = common.freshClient()

var testData = JSON.stringify({test: true})
var errorData = JSON.stringify({error: 'it went bad'})

test('streaming fetch', function (t) {
server.expect('/test', function (req, res) {
t.equal(req.method, 'GET', 'got expected method')

res.writeHead(200, {
'content-type': 'application/json'
})

res.end(testData)
})

server.expect('/error', function (req, res) {
t.equal(req.method, 'GET', 'got expected method')

res.writeHead(401, {
'content-type': 'application/json'
})

res.end(errorData)
})

client.fetch(
'http://localhost:1337/test',
{ streaming: true },
function (er, res) {
t.ifError(er, 'loaded successfully')

var sink = concat(function (data) {
t.deepEqual(data.toString(), testData)
client.fetch(
'http://localhost:1337/error',
{ streaming: true },
function (er, res) {
t.ok(er, 'got an error')
server.close()
t.end()
}
)

})

res.on('error', function (error) {
t.ifError(error, 'no errors on stream')
})

res.pipe(sink)
}
)
})

0 comments on commit f11a002

Please sign in to comment.