Skip to content

Commit

Permalink
Merge pull request #47 from phillipj/support-explicit-https
Browse files Browse the repository at this point in the history
Add support for `https` parameter to force https even on non-443 port
  • Loading branch information
phillipj committed Jan 14, 2016
2 parents 51cc232 + 7057a90 commit ffe53dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The parameter can be a string representing the server's hostname, or an object w

Options:
- **hostname**: hostname where Plex Server runs
- **port**: port number Plex Server is listening on (optional, default: 32400)
- **port**: port number Plex Server is listening on (optional, default: `32400`)
- **https**: (optional, default: `false`)
- **username**: plex.tv username (optional / required for PlexHome)
- **password**: plex.tv password (optional / required for PlexHome)
- **token**: plex.tv authentication token (optional)
Expand Down
6 changes: 6 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function PlexAPI(options, deprecatedPort) {

this.hostname = hostname;
this.port = deprecatedPort || opts.port || PLEX_SERVER_PORT;
this.https = opts.https;
this.username = opts.username;
this.password = opts.password;
this.authToken = opts.token;
Expand Down Expand Up @@ -193,6 +194,11 @@ PlexAPI.prototype._generateRelativeUrl = function _generateRelativeUrl(relativeU
};

PlexAPI.prototype._serverScheme = function _serverScheme() {
if (typeof this.https !== 'undefined') {
// If https is supplied by the user, always do what it says
return this.https ? 'https://' : 'http://';
}
// Otherwise, use https if it's on port 443, the standard https port.
return this.port === 443 ? 'https://' : 'http://';
};

Expand Down
22 changes: 22 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var expect = require('expect.js');

var PlexAPI = require('..');

describe('_serverScheme', function() {
it('should use http by default', function() {
var api = new PlexAPI({hostname: 'localhost'});
expect(api._serverScheme()).to.equal('http://');
});
it('should use https when port 443 is specified', function() {
var api = new PlexAPI({hostname: 'localhost', port: 443});
expect(api._serverScheme()).to.equal('https://');
});
it('should use https when the https parameter is true', function() {
var api = new PlexAPI({hostname: 'localhost', https: true});
expect(api._serverScheme()).to.equal('https://');
});
it('should use http when the https parameter is false, even on port 443', function() {
var api = new PlexAPI({hostname: 'localhost', port: 443, https: false});
expect(api._serverScheme()).to.equal('http://');
});
});

0 comments on commit ffe53dc

Please sign in to comment.