Skip to content
This repository has been archived by the owner on Dec 8, 2017. It is now read-only.

Commit

Permalink
Merge pull request #47 from 18F/client-protocol-test
Browse files Browse the repository at this point in the history
requestFactory assignment in Client constructors
  • Loading branch information
Marco Segreto committed Feb 4, 2016
2 parents 0d3c0a2 + ad1454c commit 69b59a3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/github-client.js
Expand Up @@ -14,6 +14,7 @@ function GitHubClient(config) {
this.timeout = config.githubTimeout;
this.baseurl = url.parse(config.githubApiBaseUrl ||
GitHubClient.API_BASE_URL);
this.requestFactory = (this.baseurl.protocol === 'https:') ? https : http;
}

GitHubClient.API_BASE_URL = 'https://api.github.com/';
Expand Down Expand Up @@ -42,14 +43,13 @@ function getHttpOptions(client, repository, paramsStr) {
}

function makeApiCall(client, metadata, repository) {
var requestFactory = (client.baseurl.protocol === 'https:') ? https : http,
paramsStr = JSON.stringify({
var paramsStr = JSON.stringify({
title: metadata.title,
body: metadata.url
});
return new Promise(function(resolve, reject) {
var httpOptions = getHttpOptions(client, repository, paramsStr),
req = requestFactory.request(httpOptions, function(res) {
req = client.requestFactory.request(httpOptions, function(res) {
handleResponse(res, resolve, reject);
});

Expand Down
5 changes: 2 additions & 3 deletions lib/slack-client.js
Expand Up @@ -14,6 +14,7 @@ function SlackClient(robotSlackClient, config) {
this.timeout = config.slackTimeout;
this.successReaction = config.successReaction;
this.baseurl = url.parse(config.slackApiBaseUrl || SlackClient.API_BASE_URL);
this.requestFactory = (this.baseurl.protocol === 'https:') ? https : http;
}

SlackClient.API_BASE_URL = 'https://slack.com/api/';
Expand Down Expand Up @@ -52,15 +53,13 @@ function getHttpOptions(client, method, queryParams) {
}

function makeApiCall(client, method, params) {
var requestFactory = (client.baseurl.protocol === 'https:') ? https : http;

return new Promise(function(resolve, reject) {
var httpOptions, req;

params.token = process.env.HUBOT_SLACK_TOKEN;
httpOptions = getHttpOptions(client, method, params);

req = requestFactory.request(httpOptions, function(res) {
req = client.requestFactory.request(httpOptions, function(res) {
handleResponse(method, res, resolve, reject);
});

Expand Down
2 changes: 2 additions & 0 deletions test/github-client-test.js
Expand Up @@ -49,11 +49,13 @@ describe('GitHubClient', function() {
it('should parse the local server URL', function() {
url.format(githubClient.baseurl).should.eql(
githubApiServer.address() + '/');
githubClient.requestFactory.globalAgent.protocol.should.eql('http:');
});

it('should parse API_BASE_URL if config base URL undefined', function() {
var githubClient = new GitHubClient(helpers.baseConfig());
url.format(githubClient.baseurl).should.eql(GitHubClient.API_BASE_URL);
githubClient.requestFactory.globalAgent.protocol.should.eql('https:');
});
});

Expand Down
2 changes: 2 additions & 0 deletions test/slack-client-test.js
Expand Up @@ -49,11 +49,13 @@ describe('SlackClient', function() {
it('should parse the local server URL', function() {
url.format(slackClient.baseurl).should.eql(
slackApiServer.address() + '/api/');
slackClient.requestFactory.globalAgent.protocol.should.eql('http:');
});

it('should parse API_BASE_URL if config base URL undefined', function() {
var slackClient = new SlackClient(undefined, helpers.baseConfig());
url.format(slackClient.baseurl).should.eql(SlackClient.API_BASE_URL);
slackClient.requestFactory.globalAgent.protocol.should.eql('https:');
});
});

Expand Down

0 comments on commit 69b59a3

Please sign in to comment.