diff --git a/oauth-1.0a.js b/oauth-1.0a.js index 5c15dd4..3ef053f 100644 --- a/oauth-1.0a.js +++ b/oauth-1.0a.js @@ -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 @@ -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(); }; /** diff --git a/package.json b/package.json index ba878f3..dc9f1d3 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,8 @@ "istanbul": "^0.4.5", "mocha": "^4.0.1", "request": "~2.33.0" + }, + "dependencies": { + "whatwg-url": "^6.4.0" } } diff --git a/test/getBaseUrl.js b/test/getBaseUrl.js new file mode 100644 index 0000000..b788d51 --- /dev/null +++ b/test/getBaseUrl.js @@ -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/'); + }); +});