Skip to content

Commit

Permalink
Exclude default ports 80/443 from base URL
Browse files Browse the repository at this point in the history
Fix #59.
  • Loading branch information
dinoboff committed Dec 23, 2017
1 parent 0992347 commit fc9e62b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
35 changes: 34 additions & 1 deletion oauth-1.0a.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,18 @@ OAuth.prototype.getSigningKey = function(token_secret) {
* @return {String}
*/
OAuth.prototype.getBaseUrl = function(url) {
return url.split('?')[0];
var parsed = this.parseUrl(url);
var port = parsed.port;
var protocol = parsed.protocol.toLowerCase();

if (
(port === ':80' && protocol === 'http') ||
(port === ':443' && protocol === 'https')
) {
return parsed.protocol + '://' + parsed.hostname + parsed.pathname;
}

return parsed.protocol + '://' + parsed.hostname + parsed.port + parsed.pathname;
};

/**
Expand Down Expand Up @@ -245,6 +256,28 @@ OAuth.prototype.deParamUrl = function(url) {
return this.deParam(tmp[1]);
};

Object.defineProperty(OAuth, 'urlPattern', {
value: /^(https?):\/\/([^:/?#]+)(\:\d+)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/
});

OAuth.prototype.parseUrl = function(url) {
var match = OAuth.urlPattern.exec(url);
var components;

if (match == null || match.len < 7) {
throw new Error('Invalid URL: "' + url + '".');
}

return {
protocol: match[1],
hostname: match[2],
port: match[3] || '',
pathname: match[4] || '',
search: match[5] || '',
hash: match[6] || ''
};
}

/**
* Percent Encode
* @param {String} str
Expand Down
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 fc9e62b

Please sign in to comment.