Commit bace6141 authored by nanahira's avatar nanahira

Merge branch '34-hostname-config' of https://github.com/tidwell/node-challonge

parents a5bbf28f 53209133
......@@ -7,9 +7,10 @@ const util = require('../util');
* @class Client
* @param {object} options configuration options for this instance
* @param {string} options.apiKey Your challonge API Key
* @param {string} [options.subdomain] - Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.subdomain] Sets the subdomain and automatically passes tournament[subdomain] and prefixes the subdomain to tournament urls. If you don't want to pass a subdomain to the constructor, and want to use an organization (or multiple organizations), you must use client.setSubdomain('subdomain') before making api calls.
* @param {string} [options.format] The format of the response data. Defaults to 'json'. If set to 'json', will return javascript objects. Anything else (including 'xml') will return the raw text string.
* @param {boolean} [options.massageProperties] If the response object should be massaged into camelCase properties when using json format. Defaults to true.
* @param {string} [options.apiHostname] If you need to change the domain that the library makes requests to, pass the string domain. This gets passed as the hostname to the config for the https library (so you cant add a path fragment on the end, just the domain). Defaults to 'api.challonge.com'
* @description
* Constructor function for the Client base responsible for communicating with Challonge API
* createClient takes one argument for configuration and returns an instance of the api client.
......@@ -34,6 +35,9 @@ const Client = exports.Client = function(options) {
if (!this.options.format) {
this.options.format = 'json';
}
if (!this.options.apiHostname) {
this.options.apiHostname = 'api.challonge.com';
}
this.setSubdomain(this.options.subdomain);
......@@ -94,7 +98,7 @@ Client.prototype.makeRequest = function(obj) {
path = versionPaths[this.options.get('version')] + (path ? path : '') + '.' + this.options.get('format') + '?' + serialized;
// create options for the https call
const options = {
hostname: 'api.challonge.com',
hostname: this.options.get('apiHostname'),
path: encodeURI(path),
method: method,
headers: {
......
......@@ -35,6 +35,13 @@ describe('Client Class', () => {
}).options.massageProperties).toBe(false);
});
it('should set the apiHostname by default, or override if passed', () => {
expect(new Client().options.apiHostname).toBe('api.challonge.com');
expect(new Client({
apiHostname: 'something.else.com'
}).options.apiHostname).toBe('something.else.com');
});
it('should set the format to json by default, or override if passed', () => {
expect(new Client().options.format).toBe('json');
expect(new Client({
......
const https = require('https');
const Challonge = require('./challonge');
describe('Challonge object', () => {
......@@ -48,4 +49,22 @@ describe('Challonge object', () => {
expect(client.matches.setSubdomain).toHaveBeenCalled();
});
});
it('should allow you to proxy to another domain if needed', () => {
const client = Challonge.createClient({
apiHostname: 'myother.domain.com'
});
client.setSubdomain('somedomain');
spyOn(https, 'request').and.returnValue({
end: () => {}
});
client.tournaments.index({
id: 25
});
expect(https.request.calls.allArgs()[0][0].hostname).toEqual('myother.domain.com');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment