Skip to content

Commit

Permalink
Added testcases, prettier format changes
Browse files Browse the repository at this point in the history
TestCases update + Prettier
  • Loading branch information
shrutiburman committed Aug 9, 2021
1 parent 6bbdbce commit 2baad21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/services/cli-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CliRequestClient {
constructor(commandName, logger, http, keytarFlag = false, extensions = ' ') {
this.commandName = commandName;
this.logger = logger;
this.helperLibrary = extensions;
this.pluginName = extensions;
this.http = http || require('axios');
if (process.env.HTTP_PROXY) {
/*
Expand Down Expand Up @@ -73,9 +73,7 @@ class CliRequestClient {
componentInfo.push(userAgentArr[3]); // nodejs version
componentInfo.push(this.commandName); // cli-command
componentInfo.push(this.keytarWord); // keytar flag
headers['User-Agent'] = `${this.helperLibrary} ${pkg.name}/${pkg.version} ${componentInfo
.filter(Boolean)
.join(' ')}`;
headers['User-Agent'] = `${this.pluginName} ${pkg.name}/${pkg.version} ${componentInfo.filter(Boolean).join(' ')}`;

const options = {
timeout: opts.timeout || 30000,
Expand Down
35 changes: 22 additions & 13 deletions test/services/cli-http-client.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const os = require('os');

const { expect, test } = require('@twilio/cli-test');

const CliRequestClient = require('../../src/services/cli-http-client');
const { TwilioCliError } = require('../../src/services/error');
const { Logger, LoggingLevel } = require('../../src/services/messaging/logging');
const pkg = require('../../package.json');
const os = require('os');

describe('services', () => {
describe('cli-http-client', () => {
Expand All @@ -25,7 +26,7 @@ describe('services', () => {
);
expect(client.commandName).to.equal('blah');
expect(client.keytarWord).to.equal('keytar');
expect(client.helperLibrary).to.equal('blah');
expect(client.pluginName).to.equal('blah');
const response = await client.request({
method: 'POST',
uri: 'https://foo.com/bar',
Expand All @@ -45,7 +46,7 @@ describe('services', () => {
const client = new CliRequestClient('blah', logger, { defaults: {} }, false, 'blah');
const httpAgent = client.http.defaults.httpsAgent;
expect(client.keytarWord).to.equal('');
expect(client.helperLibrary).to.equal('blah');
expect(client.pluginName).to.equal('blah');
expect(httpAgent.proxy.host).to.equal('someproxy.com');
expect(httpAgent.proxy.port).to.equal(8080);
});
Expand All @@ -66,14 +67,17 @@ describe('services', () => {
})
.it('throws a TwilioCliError on connection timeouts', async () => {
const client = new CliRequestClient('bleh', logger);
const request = client.request({ method: 'GET', uri: 'https://foo.com/bar'});
const request = client.request({ method: 'GET', uri: 'https://foo.com/bar' });
await expect(request).to.be.rejectedWith(TwilioCliError);
});

test
test
.nock('https://foo.com', (api) => {
api.get('/bar')
.reply(200,'', {'User-Agent': `twilio-cli/2.27.1 ${pkg.name}\/${pkg.version} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`});
api.get('/bar').reply(200, '', {
'User-Agent': `twilio-cli/2.27.1 ${pkg.name}\/${
pkg.version
} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`,
});
})
.it('correctly sets user-agent', async () => {
const client = new CliRequestClient('dummyCommand', logger, '', true, 'twilio-cli/2.27.1');
Expand All @@ -82,26 +86,31 @@ describe('services', () => {
uri: 'https://foo.com/bar',
});
expect(client.keytarWord).to.equal('keytar');
expect(client.lastRequest.headers['User-Agent']).to.equal("twilio-cli/2.27.1 @twilio/cli-core/5.27.1 (darwin x64) dummyCommand keytar");
expect(client.lastRequest.headers['User-Agent']).to.equal(
`twilio-cli/2.27.1 ${pkg.name}\/${pkg.version} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`,
);
expect(response.statusCode).to.equal(200);
});

test
test
.nock('https://foo.com', (api) => {
api.get('/bar')
.reply(200,'', {'User-Agent': ` ${pkg.name}\/${pkg.version} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`});
api.get('/bar').reply(200, '', {
'User-Agent': ` ${pkg.name}\/${pkg.version} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`,
});
})
.it('correctly sets user-agent with empty plugin value', async () => {
const client = new CliRequestClient('dummyCommand', logger, '', true, '');
const response = await client.request({
method: 'GET',
uri: 'https://foo.com/bar',
});
expect(client.lastRequest.headers['User-Agent']).to.equal(" @twilio/cli-core/5.27.1 (darwin x64) dummyCommand keytar");
expect(client.lastRequest.headers['User-Agent']).to.equal(
` ${pkg.name}\/${pkg.version} \(${os.platform()} ${os.arch()}\) dummyCommand keytar`,
);
expect(response.statusCode).to.equal(200);
});

test
test
.nock('https://foo.com', (api) => {
api.get('/bar?foo=bar&foo=baz').reply(200);
})
Expand Down

0 comments on commit 2baad21

Please sign in to comment.