Skip to content

Commit

Permalink
Merge pull request #64 from tschaub/protocol
Browse files Browse the repository at this point in the history
Respect protocol
  • Loading branch information
James Halliday committed Sep 3, 2014
2 parents df11f4a + cf4f5a8 commit 7826c6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ http.request = function (params, cb) {
if (!params.host && params.hostname) {
params.host = params.hostname;
}

if (!params.scheme) params.scheme = window.location.protocol.split(':')[0];

if (!params.protocol) {
if (params.scheme) {
params.protocol = params.scheme + ':';
} else {
params.protocol = window.location.protocol;
}
}

if (!params.host) {
params.host = window.location.hostname || window.location.host;
}
Expand All @@ -25,7 +32,7 @@ http.request = function (params, cb) {
}
params.host = params.host.split(':')[0];
}
if (!params.port) params.port = params.scheme == 'https' ? 443 : 80;
if (!params.port) params.port = params.protocol == 'https:' ? 443 : 80;

var req = new Request(new xhrHttp, params);
if (cb) req.on('response', cb);
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Request = module.exports = function (xhr, params) {
self.xhr = xhr;
self.body = [];

self.uri = (params.scheme || 'http') + '://'
self.uri = (params.protocol || 'http:') + '//'
+ params.host
+ (params.port ? ':' + params.port : '')
+ (params.path || '/')
Expand Down
14 changes: 14 additions & 0 deletions test/request_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ test('Test full url object', function(t) {

});

test('Test alt protocol', function(t) {
var params = {
protocol: "foo:",
hostname: "localhost",
port: "3000",
path: "/bar"
};

var request = http.get(params, noop);

t.equal( request.uri, 'foo://localhost:3000/bar', 'Url should be correct');
t.end();

});

test('Test string as parameters', function(t) {
var url = '/api/foo';
Expand Down

0 comments on commit 7826c6e

Please sign in to comment.