-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eran Hammer
committed
May 12, 2013
1 parent
fafa52d
commit f2ce1af
Showing
4 changed files
with
144 additions
and
59 deletions.
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,110 @@ | ||
// Load modules | ||
|
||
var Url = require('url'); | ||
var Http = require('http'); | ||
var Https = require('https'); | ||
var Stream = require('stream'); | ||
var Utils = require('./utils'); | ||
var Boom = require('boom'); | ||
|
||
|
||
// Declare internals | ||
|
||
var internals = {}; | ||
|
||
|
||
// Create and configure server instance | ||
|
||
exports.request = function (method, url, options, callback) { | ||
|
||
var uri = Url.parse(url); | ||
uri.method = method.toUpperCase(); | ||
uri.headers = options.headers; | ||
|
||
var agent = (uri.protocol === 'https:' ? Https : Http); | ||
var req = agent.request(uri); | ||
|
||
var isFinished = false; | ||
var finish = function (err, res) { | ||
|
||
if (!isFinished) { | ||
isFinished = true; | ||
|
||
req.removeAllListeners(); | ||
|
||
return callback(err, res); | ||
} | ||
}; | ||
|
||
req.once('error', finish); | ||
|
||
req.once('response', function (res) { | ||
|
||
// res.statusCode | ||
// res.headers | ||
// res Readable stream | ||
|
||
return finish(null, res); | ||
}); | ||
|
||
if (uri.method !== 'GET' && | ||
uri.method !== 'HEAD' && | ||
options.payload !== null && | ||
options.payload !== undefined) { // Value can be falsey | ||
|
||
if (options.payload instanceof Stream) { | ||
options.payload.pipe(req); | ||
return; | ||
} | ||
|
||
req.write(options.payload); | ||
} | ||
|
||
req.end(); | ||
}; | ||
|
||
|
||
exports.parse = function (res, callback) { | ||
|
||
var Writer = function () { | ||
|
||
Stream.Writable.call(this); | ||
this.buffers = []; | ||
this.length = 0; | ||
|
||
return this; | ||
}; | ||
|
||
Utils.inherits(Writer, Stream.Writable); | ||
|
||
Writer.prototype._write = function (chunk, encoding, next) { | ||
|
||
this.legnth += chunk.length; | ||
this.buffers.push(chunk); | ||
next(); | ||
}; | ||
|
||
var isFinished = false; | ||
var finish = function (err, buffer) { | ||
|
||
if (!isFinished) { | ||
isFinished = true; | ||
|
||
writer.removeAllListeners(); | ||
res.removeAllListeners(); | ||
|
||
return callback(err || (buffer ? null : Boom.internal('Client request closed')), buffer); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hueniverse
Contributor
|
||
} | ||
}; | ||
|
||
var writer = new Writer(); | ||
writer.once('finish', function () { | ||
|
||
return finish(null, Buffer.concat(writer.buffers, writer.length)); | ||
}); | ||
|
||
res.once('error', finish); | ||
res.once('close', finish); | ||
|
||
res.pipe(writer); | ||
}; |
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
Are you planning on writing unit tests for client.js ?
It's hard to know if this line is fully covered via integration tests of proxy.js.
If so, https://github.com/thlorenz/proxyquire is an interesting way to stub or mock the http require