Skip to content

Commit

Permalink
Merge pull request #11 from hansman/master
Browse files Browse the repository at this point in the history
Prefer hostname over host in http(s)? request options
  • Loading branch information
ctide committed Jan 11, 2014
2 parents 07921d9 + 0a7d723 commit 8fa415b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions fakeweb.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var fs = require('fs')


function interceptable(uri, method) {

if(typeof method === "undefined")
{
method = "GET";
Expand Down Expand Up @@ -125,7 +125,7 @@ function Fakeweb() {
https.request = function(options, callback) {
var uri;
if (options.port) {
uri = "https://" + options.host + ":" + options.port + options.path;
uri = "https://" + (options.hostname || options.host) + ":" + options.port + options.path;
} else {
uri = "https://" + options.host + options.path;
}
Expand All @@ -140,7 +140,7 @@ function Fakeweb() {
http.request = function(options, callback) {
var uri;
if (options.port) {
uri = "http://" + options.host + ":" + options.port + options.path;
uri = "http://" + (options.hostname || options.host) + ":" + options.port + options.path;
} else {
uri = "http://" + options.host + options.path;
}
Expand Down
38 changes: 38 additions & 0 deletions test/fakeweb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,43 @@ vows.describe('Fakeweb').addBatch({
'binary files' : function(err, resp, body) {
assert.equal(body, fs.readFileSync(path.join(__dirname, 'fixtures', 'grimace.jpg'), 'binary'));
}
},
"will prefer hostname over host in http request options": {
topic: function() {
var self = this;
var data = '';
fakeweb.registerUri({uri: 'http://hostname.com:80/', body: 'hostname'});
var req = http.request({hostname: 'hostname.com', host: 'hostname.com:80', port: '80', path: '/', method: 'GET'}, function(res) {
res.on('data', function(chunk) {
data += chunk;
});
res.on('close', function() {
self.callback(undefined, res, data);
});
});
req.end();
},
"correctly on the response" : function(err, resp, body) {
assert.equal(resp.statusCode, 200);
}
},
"will prefer hostname over host in https request options": {
topic: function() {
var self = this;
var data = '';
fakeweb.registerUri({uri: 'https://hostname.com:80/', body: 'hostname'});
var req = https.request({hostname: 'hostname.com', host: 'hostname.com:80', port: '80', path: '/', method: 'GET'}, function(res) {
res.on('data', function(chunk) {
data += chunk;
});
res.on('close', function() {
self.callback(undefined, res, data);
});
});
req.end();
},
"correctly on the response" : function(err, resp, body) {
assert.equal(resp.statusCode, 200);
}
}
}).export(module);

0 comments on commit 8fa415b

Please sign in to comment.