Skip to content

Commit

Permalink
fix sls test when using shortform HTTP event (#296)
Browse files Browse the repository at this point in the history
fix `sls test` when using shortform HTTP event
  • Loading branch information
dschep committed Oct 10, 2019
2 parents 95d1d2c + 3eba6e1 commit 872e12d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
19 changes: 16 additions & 3 deletions lib/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,22 @@ module.exports.test = async ctx => {

const funcs = ctx.sls.service.functions || {};
for (const testSpec of tests || []) {
const method =
testSpec.endpoint.method || funcs[testSpec.endpoint.function].events[0].http.method;
const path = testSpec.endpoint.path || funcs[testSpec.endpoint.function].events[0].http.path;
let method = testSpec.endpoint.method;
if (!method) {
if (typeof funcs[testSpec.endpoint.function].events[0].http === 'string') {
method = funcs[testSpec.endpoint.function].events[0].http.split(' ')[0];
} else {
method = funcs[testSpec.endpoint.function].events[0].http.method;
}
}
let path = testSpec.endpoint.path;
if (!path) {
if (typeof funcs[testSpec.endpoint.function].events[0].http === 'string') {
path = funcs[testSpec.endpoint.function].events[0].http.split(' ')[1];
} else {
path = funcs[testSpec.endpoint.function].events[0].http.path;
}
}
const testName = `${method.toUpperCase()} ${path} - ${testSpec.name}`;
try {
numTests += 1;
Expand Down
117 changes: 115 additions & 2 deletions lib/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const proxyquire = require('proxyquire');
const sinon = require('sinon');
const chalk = require('chalk');

let endpoint; // endpoint spec to be returned by readFile

const runTest = sinon.stub().resolves();
const { test: testFunc } = proxyquire('./', {
'./runTest': runTest,
Expand All @@ -13,7 +15,7 @@ const { test: testFunc } = proxyquire('./', {
JSON.stringify([
{
name: 'foobar',
endpoint: { function: 'blah', path: 'blah', method: 'post' },
endpoint,
response: { body: 'foobar' },
request: { headers: { Foo: 'bar' } },
},
Expand All @@ -33,12 +35,13 @@ describe('test', () => {
});

it('calls runTest', async () => {
endpoint = { function: 'blah', path: 'blah', method: 'post' };
const ctx = {
sls: {
enterpriseEnabled: true,
processedInput: { options: {} },
cli: { log: sinon.spy() },
service: { functions: [] },
service: { functions: {} },
},
provider: {
naming: {
Expand Down Expand Up @@ -78,6 +81,116 @@ describe('test', () => {
]);
});

it('calls runTest with longform http event', async () => {
endpoint = { function: 'blah' };
const ctx = {
sls: {
enterpriseEnabled: true,
processedInput: { options: {} },
cli: { log: sinon.spy() },
service: {
functions: {
blah: { events: [{ http: { path: 'blah', method: 'post' } }] },
},
},
},
provider: {
naming: {
getServiceEndpointRegex: () => 'http',
getStackName: () => 'stack',
},
request: async () => ({
Stacks: [{ Outputs: [{ OutputKey: 'http', OutputValue: 'https://example.com' }] }],
}),
},
};
await testFunc(ctx);
expect(runTest.args[0][0]).to.deep.equal(
{
name: 'foobar',
endpoint: {
function: 'blah',
method: 'post',
path: 'blah',
},
response: { body: 'foobar' },
request: { headers: { Foo: 'bar' } },
},
'blah',
'post',
'https://example.com'
);
expect(ctx.sls.cli.log.args).to.deep.equal([
[
`Test Results:
Summary --------------------------------------------------
`,
],
[`Test Summary: ${chalk.green('1 passed')}, ${chalk.red('0 failed')}`],
]);
expect(process.stdout.write.args).to.deep.equal([
[' running - POST blah - foobar'],
[`\r ${chalk.green('passed')} - POST blah - foobar\n`],
['\n'],
]);
});

it('calls runTest with shortform http event', async () => {
endpoint = { function: 'blah' };
const ctx = {
sls: {
enterpriseEnabled: true,
processedInput: { options: {} },
cli: { log: sinon.spy() },
service: {
functions: {
blah: { events: [{ http: 'POST blah' }] },
},
},
},
provider: {
naming: {
getServiceEndpointRegex: () => 'http',
getStackName: () => 'stack',
},
request: async () => ({
Stacks: [{ Outputs: [{ OutputKey: 'http', OutputValue: 'https://example.com' }] }],
}),
},
};
await testFunc(ctx);
expect(runTest.args[0][0]).to.deep.equal(
{
name: 'foobar',
endpoint: {
function: 'blah',
method: 'post',
path: 'blah',
},
response: { body: 'foobar' },
request: { headers: { Foo: 'bar' } },
},
'blah',
'post',
'https://example.com'
);
expect(ctx.sls.cli.log.args).to.deep.equal([
[
`Test Results:
Summary --------------------------------------------------
`,
],
[`Test Summary: ${chalk.green('1 passed')}, ${chalk.red('0 failed')}`],
]);
expect(process.stdout.write.args).to.deep.equal([
[' running - POST blah - foobar'],
[`\r ${chalk.green('passed')} - POST blah - foobar\n`],
['\n'],
]);
});

it('logs message if sfe not enabled', async () => {
const ctx = { sls: { cli: { log: sinon.spy() } } };
await testFunc(ctx);
Expand Down

0 comments on commit 872e12d

Please sign in to comment.