Skip to content

Commit

Permalink
Exclude default ports 80/443
Browse files Browse the repository at this point in the history
Fix ddo#59
  • Loading branch information
dinoboff committed Dec 22, 2017
1 parent 4dc406a commit 10dfd6d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion oauth-1.0a.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') {
module.exports = OAuth;
}

if (typeof(URL) === 'undefined') {
var URL = require('whatwg-url').URL;
}

/**
* Constructor
* @param {Object} opts consumer key and secret
Expand Down Expand Up @@ -194,7 +198,11 @@ OAuth.prototype.getSigningKey = function(token_secret) {
* @return {String}
*/
OAuth.prototype.getBaseUrl = function(url) {
return url.split('?')[0];
var parsed = new URL(url);

parsed.search = '';

return parsed.toString();
};

/**
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"istanbul": "^0.4.5",
"mocha": "^4.0.1",
"request": "~2.33.0"
},
"dependencies": {
"whatwg-url": "^6.4.0"
}
}
33 changes: 33 additions & 0 deletions test/getBaseUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var expect = require('chai').expect;
var OAuth = require('../oauth-1.0a');

describe('#getBaseUrl', function() {
var oauth = new OAuth({
consumer: {}
});

beforeEach(function () {
oauth = new OAuth({
consumer: {}
});
});

it('should return base url', function () {
expect(oauth.getBaseUrl('http://example.com/path/')).to.equal('http://example.com/path/');
expect(oauth.getBaseUrl('http://example.com/path/?foo=bar')).to.equal('http://example.com/path/');
});

it('should exclude default port number', function () {
expect(oauth.getBaseUrl('http://example.com/')).to.equal('http://example.com/');
expect(oauth.getBaseUrl('http://example.com:80/')).to.equal('http://example.com/');
expect(oauth.getBaseUrl('https://example.com/')).to.equal('https://example.com/');
expect(oauth.getBaseUrl('https://example.com:443/')).to.equal('https://example.com/');
});

it('should include non-default port number', function () {
expect(oauth.getBaseUrl('http://example.com:8080/')).to.equal('http://example.com:8080/');
expect(oauth.getBaseUrl('http://example.com:443/')).to.equal('http://example.com:443/');
expect(oauth.getBaseUrl('https://example.com:8080/')).to.equal('https://example.com:8080/');
expect(oauth.getBaseUrl('https://example.com:80/')).to.equal('https://example.com:80/');
});
});

0 comments on commit 10dfd6d

Please sign in to comment.