-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
204: Handle proxies when using Slack WebClient (#205)
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { WebClient } = require('@slack/web-api'); | ||
const HttpsProxyAgent = require('https-proxy-agent'); | ||
|
||
/** | ||
* | ||
* @param {string} botToken token used to authenticate as the Slack Bot user | ||
* @param {string?} httpsProxy (optional) URL for the proxy to use for HTTPS requests | ||
* @returns { WebClient } a WebClient configured with the bot token and proxy agent (if needed) | ||
*/ | ||
function createWebClient(botToken, httpsProxy) { | ||
if (httpsProxy) { | ||
const httpsProxyAgent = new HttpsProxyAgent(httpsProxy); | ||
return new WebClient(botToken, { agent: httpsProxyAgent }); | ||
} | ||
return new WebClient(botToken); | ||
} | ||
|
||
module.exports = { | ||
createWebClient, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { assert } = require('chai'); | ||
const sinon = require('sinon'); | ||
const rewiremock = require('rewiremock/node'); | ||
|
||
/* eslint-disable-next-line global-require */ | ||
rewiremock(() => require('@slack/web-api')).with({ | ||
WebClient: class { | ||
constructor(token, options) { | ||
this.token = token; | ||
this.options = options || {}; | ||
} | ||
}, | ||
}); | ||
rewiremock.enable(); | ||
const { createWebClient } = require('../src/web-client'); | ||
|
||
rewiremock.disable(); | ||
|
||
describe('web-client', () => { | ||
beforeEach(() => { | ||
sinon.reset(); | ||
}); | ||
|
||
it('should create WebClient with an https proxy agent if given a proxy URL', async () => { | ||
const web = createWebClient('xoxb-xxxxx', 'http://test.proxy:8080/'); | ||
assert.equal(typeof web.options.agent, 'object'); | ||
}); | ||
|
||
it('should create WebClient with default settings if not given a proxy URL', async () => { | ||
const web = createWebClient('xoxb-xxxxx'); | ||
assert.equal(web.options.agent, undefined); | ||
}); | ||
}); |