Skip to content

Commit

Permalink
feat: add custom HTTP header parameter support (twilio#200)
Browse files Browse the repository at this point in the history
* feat: add custom HTTP header parameter support
  • Loading branch information
eshanholtz committed Jul 16, 2020
1 parent cc87c60 commit 1fef00e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/services/twilio-api/api-command-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ class ApiCommandRunner {

const pathParams = {};
const queryParams = {};
const headerParams = {};

// Build the path and query params based on the parameters defined with the API action.
// Build the path, query, and header params based on the parameters defined with the API action.
actionParams.forEach(parameter => {
const destination = (parameter.in === 'path' ? pathParams : queryParams);
const destination = (parameter.in === 'path' ? pathParams : (parameter.in === 'header' ? headerParams : queryParams));
this.addParameter(parameter, destination);
});

Expand All @@ -87,7 +88,8 @@ class ApiCommandRunner {
domain: domainName,
path: path,
pathParams: pathParams,
data: queryParams
data: queryParams,
headers: headerParams
});

// TODO: Possible extender event: "afterInvokeApi"
Expand Down
42 changes: 42 additions & 0 deletions test/base-commands/twilio-api-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe('base-commands', () => {
}
};

const syncMapItemUpdateActionDefinition = {
domainName: 'sync',
commandName: 'update',
path: '/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Sid}.json',
actionName: 'update',
action: {
parameters: [
{ name: 'Sid', in: 'path', schema: { type: 'string' } },
{ name: 'MapSid', in: 'path', schema: { type: 'string' } },
{ name: 'ServiceSid', in: 'path', schema: { type: 'string' } },
{ name: 'Key', in: 'query', schema: { type: 'string' } },
{ name: 'If-Match', in: 'header', schema: { type: 'string' } }
]
}
};

const getCommandClass = (actionDefinition = callCreateActionDefinition) => {
const NewCommandClass = class extends TwilioApiCommand {
};
Expand Down Expand Up @@ -240,6 +256,32 @@ describe('base-commands', () => {
.it('creates a call with an invalid parameter', ctx => {
expect(ctx.stdout).to.contain(fakeCallResponse.sid);
});

test
.twilioFakeProfile(ConfigData)
.twilioCliEnv(Config)
.stdout()
.stderr()
.twilioCreateCommand(getCommandClass(syncMapItemUpdateActionDefinition), [
'--sid',
'unique_name',
'--map-sid',
'MP0123',
'--service-sid',
'IS1234',
'--key',
'test_key',
'--if-match',
'revision'
])
.do(ctx => {
ctx.testCmd.twilioApi = { update: sinon.stub().returns({}) };
return ctx.testCmd.run();
})
.it('adds header parameters', ctx => {
const syncOptions = ctx.testCmd.twilioApi.update.firstCall.firstArg;
expect(syncOptions.headers).to.include({ 'If-Match': 'revision' });
});
});
});
});

0 comments on commit 1fef00e

Please sign in to comment.