Permalink
Browse files

Added test for a readable stream body.

  • Loading branch information...
1 parent 1275711 commit bd239a4912a36db38af69d3c135414fbf056700f @slaskis slaskis committed May 2, 2011
Showing with 27 additions and 8 deletions.
  1. +1 −1 lib/schemes/http.js
  2. +7 −3 lib/utils.js
  3. +19 −4 test/test-open-uri.js
View
@@ -48,7 +48,7 @@ module.exports = function http(uri,opts,output){
// Body Content-Length
if( typeof opts.body == "string" || Buffer.isBuffer( opts.body ) )
uri.headers["content-length"] = opts.body.length;
-
+
var req = require(uri.scheme).request(uri,function(res){
// Follow Redirects
if( opts.follow !== false && res.statusCode > 299 && res.statusCode < 400 && res.headers.location ){
View
@@ -22,9 +22,13 @@ exports.buffer = function buffer(type,path,stream,output){
stream.on("error",function(err){ error(output, err); }) // TODO Close the stream on error?
stream.on("timeout",function(){ error(output,new Error("[OpenURI] Connection timed out.")); }) // TODO Close the stream on timeout?
stream.on("data",function(chunk){ buf.push(chunk); len += chunk.length; })
- stream.on("end",function(){
- var data = parse(type,buf,len) || parse(mimeType,buf,len) || parse.default(buf,len);
- output(null,data,stream)
+ stream.on("end",function(){
+ try {
+ var data = parse(type,buf,len) || parse(mimeType,buf,len) || parse.default(buf,len);
+ output(null,data,stream)
+ } catch(e){
+ utils.error(output,e)
+ }
})
}
View
@@ -16,10 +16,10 @@ function writeStream(){
// A simple test echo server
function echo(port,fn){
var http = require("http").createServer(function(req,res){
- res.writeHead(200, {
- 'Content-Type': req.headers["content-type"] || 'text/plain',
- 'Content-Length': req.headers["content-length"]
- })
+ var headers = {'Content-Type': req.headers["content-type"] || 'text/plain'}
+ if( req.headers["content-length"] )
+ headers["Content-Length"] = req.headers["content-length"];
+ res.writeHead(200, headers)
req.pipe(res)
})
http.listen(port,function(){fn(http)})
@@ -93,6 +93,21 @@ exports["POST some json to a website"] = function(){
})
})
}
+
+
+exports["POST stream text to a website"] = function(){
+ echo(++port,function(server){
+ var file = require("fs").createReadStream("README.md");
+ open("http://localhost:"+port,{method:"POST",body:file},function(err,dump,res){
+ server.close()
+ assert.ifError(err)
+ assert.equal(res.headers["content-type"],"text/plain")
+ assert.type(dump,"string")
+ assert.eql(dump,require("fs").readFileSync("README.md","utf8"))
+ })
+ })
+}
+
exports["GET a relative file"] = function(beforeExit){
var loaded = false;
open("README.md",function(err,log){

0 comments on commit bd239a4

Please sign in to comment.