Skip to content

Commit

Permalink
Merge pull request #6987 from tinexw/6949
Browse files Browse the repository at this point in the history
Add support for contentHandling - Fixes gh-6949
  • Loading branch information
medikoo committed Nov 22, 2019
2 parents c77cf01 + 08f55eb commit 9a81af5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/providers/aws/events/apigateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,22 @@ provider:

In your Lambda function you need to ensure that the correct `content-type` header is set. Furthermore you might want to return the response body in base64 format.

To convert the request or response payload, you can set the [contentHandling](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html) property.

```yml
functions:
create:
handler: posts.create
events:
- http:
path: posts/create
method: post
request:
contentHandling: CONVERT_TO_TEXT
response:
contentHandling: CONVERT_TO_TEXT
```

## AWS X-Ray Tracing

API Gateway supports a form of out of the box distributed tracing via [AWS X-Ray](https://aws.amazon.com/xray/) though enabling [active tracing](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-xray.html). To enable this feature for your serverless application's API Gateway add the following to your `serverless.yml`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,35 @@ describe('#compileMethods()', () => {
});
});

it('should use defined content-handling behavior', () => {
awsCompileApigEvents.validated.events = [
{
functionName: 'First',
http: {
method: 'GET',
path: 'users/list',
integration: 'AWS',
request: {
contentHandling: 'CONVERT_TO_TEXT',
},
response: {
statusCodes: {
200: {
pattern: '',
},
},
},
},
},
];
return awsCompileApigEvents.compileMethods().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ApiGatewayMethodUsersListGet.Properties.Integration.ContentHandling
).to.equal('CONVERT_TO_TEXT');
});
});

it('should set custom request templates', () => {
awsCompileApigEvents.validated.events = [
{
Expand Down Expand Up @@ -1310,6 +1339,7 @@ describe('#compileMethods()', () => {
SelectionPattern: 'foo',
ResponseParameters: {},
ResponseTemplates: {},
ContentHandling: undefined,
});
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
Expand All @@ -1319,6 +1349,7 @@ describe('#compileMethods()', () => {
SelectionPattern: '',
ResponseParameters: {},
ResponseTemplates: {},
ContentHandling: undefined,
});
});
});
Expand Down Expand Up @@ -1484,6 +1515,34 @@ describe('#compileMethods()', () => {
});
});

it('should use defined content-handling behavior', () => {
awsCompileApigEvents.validated.events = [
{
functionName: 'First',
http: {
method: 'get',
path: 'users/list',
integration: 'AWS',
response: {
contentHandling: 'CONVERT_TO_BINARY',
statusCodes: {
200: {
pattern: '',
},
},
},
},
},
];
return awsCompileApigEvents.compileMethods().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ApiGatewayMethodUsersListGet.Properties.Integration.IntegrationResponses[0]
.ContentHandling
).to.equal('CONVERT_TO_BINARY');
});
});

it('should handle root resource methods', () => {
awsCompileApigEvents.validated.events = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ module.exports = {
if (type === 'AWS' || type === 'HTTP' || type === 'MOCK') {
_.assign(integration, {
PassthroughBehavior: http.request && http.request.passThrough,
ContentHandling: http.request && http.request.contentHandling,
RequestTemplates: this.getIntegrationRequestTemplates(http, type === 'AWS'),
IntegrationResponses: this.getIntegrationResponses(http),
});
}
if (
((type === 'AWS' || type === 'HTTP' || type === 'HTTP_PROXY') &&
(http.request && !_.isEmpty(http.request.parameters))) ||
http.request &&
!_.isEmpty(http.request.parameters)) ||
http.async
) {
_.assign(integration, {
Expand Down Expand Up @@ -151,6 +153,7 @@ module.exports = {
SelectionPattern: config.pattern || '',
ResponseParameters: responseParameters,
ResponseTemplates: {},
ContentHandling: http.response.contentHandling,
};

if (config.headers) {
Expand Down

0 comments on commit 9a81af5

Please sign in to comment.