This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
request: add support for streaming bodies
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
}) |