-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Wrap native string responses from lambda invocation endpoint
- Loading branch information
1 parent
77b3659
commit 3bd294b
Showing
6 changed files
with
128 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsJSONObject.fixture.js
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,35 @@ | ||
import { resolve } from 'path' | ||
import LambdaFunction from '../../../LambdaFunction.js' | ||
|
||
export default class LambdaFunctionThatReturnsJSONObject { | ||
options = {} | ||
serverless = { | ||
config: { | ||
serverlessPath: '', | ||
servicePath: resolve(__dirname), | ||
}, | ||
service: { | ||
provider: { | ||
runtime: 'nodejs12.x', | ||
}, | ||
}, | ||
} | ||
|
||
listFunctionNames() { | ||
return ['foo'] | ||
} | ||
|
||
getByFunctionName(functionName) { | ||
const functionDefinition = { | ||
handler: | ||
'../../fixtures/lambdaFunction.fixture.asyncFunctionHandlerObject', | ||
} | ||
|
||
return new LambdaFunction( | ||
functionName, | ||
functionDefinition, | ||
this.serverless, | ||
this.options, | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsNativeString.fixture.js
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,34 @@ | ||
import { resolve } from 'path' | ||
import LambdaFunction from '../../../LambdaFunction.js' | ||
|
||
export default class LambdaFunctionThatReturnsNativeString { | ||
options = {} | ||
serverless = { | ||
config: { | ||
serverlessPath: '', | ||
servicePath: resolve(__dirname), | ||
}, | ||
service: { | ||
provider: { | ||
runtime: 'nodejs12.x', | ||
}, | ||
}, | ||
} | ||
|
||
listFunctionNames() { | ||
return ['foo'] | ||
} | ||
|
||
getByFunctionName(functionName) { | ||
const functionDefinition = { | ||
handler: '../../fixtures/lambdaFunction.fixture.asyncFunctionHandler', | ||
} | ||
|
||
return new LambdaFunction( | ||
functionName, | ||
functionDefinition, | ||
this.serverless, | ||
this.options, | ||
) | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/lambda/__tests__/routes/invocations/InvocationsController.test.js
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,43 @@ | ||
import InvocationsController from '../../../routes/invocations/InvocationsController.js' | ||
import LambdaFunctionThatReturnsJSONObject from '../../fixtures/Lambda/LambdaFunctionThatReturnsJSONObject.fixture.js' | ||
import LambdaFunctionThatReturnsNativeString from '../../fixtures/Lambda/LambdaFunctionThatReturnsNativeString.fixture.js' | ||
|
||
jest.mock('../../../../serverlessLog') | ||
|
||
describe('InvocationController', () => { | ||
const functionName = 'foo' | ||
|
||
describe('when event type is "RequestResponse"', () => { | ||
const eventType = 'RequestResponse' | ||
|
||
test('should return json object if lambda response is json', async () => { | ||
const expected = { | ||
Payload: { | ||
foo: 'bar', | ||
}, | ||
StatusCode: 200, | ||
} | ||
|
||
const invocationController = new InvocationsController( | ||
new LambdaFunctionThatReturnsJSONObject(), | ||
) | ||
const result = await invocationController.invoke(functionName, eventType) | ||
|
||
expect(result).toStrictEqual(expected) | ||
}) | ||
|
||
test('should wrap native string responses with ""', async () => { | ||
const expected = { | ||
Payload: '"foo"', | ||
StatusCode: 200, | ||
} | ||
|
||
const invocationController = new InvocationsController( | ||
new LambdaFunctionThatReturnsNativeString(), | ||
) | ||
const result = await invocationController.invoke(functionName, eventType) | ||
|
||
expect(result).toStrictEqual(expected) | ||
}) | ||
}) | ||
}) |
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