Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

Commit

Permalink
fix: fixed problem with missing response headers (#12)
Browse files Browse the repository at this point in the history
adds response headers to method responses as response parameters
  • Loading branch information
shevchenkos authored and tchock committed Jan 13, 2017
1 parent 5137dfb commit ddd4811
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-aws-documentation",
"version": "0.5.4",
"version": "0.5.5",
"description": "Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway",
"main": "src/index.js",
"scripts": {
Expand Down
159 changes: 159 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,165 @@ describe('ServerlessAWSDocumentation', function() {
});
});

it('should only add response methods with response headers to ApiGateway methods', function () {
this.serverlessMock.variables.service.custom.documentation.models = [];
this.serverlessMock.service._functionNames = ['test', 'blub'];
this.serverlessMock.service._functions = {
test: {
events: [{
http: {
path: 'some/path',
method: 'post',
cors: true,
private: true,
documentation: {
methodResponses: [
{
statusCode: 200,
responseModels: {
'application/json': 'CreateResponse',
},
responseHeaders: [{
name: 'x-header',
description: 'THE header',
}, {
name: 'x-other-header',
description: 'THE other header',
}],
},
{
statusCode: 400,
responseModels: {
'application/json': 'ErrorResponse'
},
responseHeaders: [{
name: 'x-header',
description: 'THE header',
}],
},
{
statusCode: 404,
responseModels: {
'application/json': 'ErrorResponse'
},
responseHeaders: [{
name: 'x-header',
description: 'THE header',
}],
},
],
}
},
}],
},
blub: {
events: [{
http: {
path: 'some/other/path',
method: 'get',
cors: true,
private: true,
documentation: {
methodResponses: [
{
statusCode: 204,
responseModels: {
'application/json': 'CrazyResponse',
},
responseHeaders: [{
name: 'x-header',
description: 'THE header',
}, {
name: 'x-other-header',
description: 'THE other header',
}],
},
],
},
},
}],
},
};

const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
resources.someotherpath_get = {
some: 'other_configuration',
Properties: {},
};
resources.somepath_post = {
some: 'configuration',
Properties: {},
};

this.plugin.beforeDeploy();

expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
Resources: {
ExistingResource: {
with: 'configuration',
},
somepath_post: {
some: 'configuration',
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
Properties: {
MethodResponses: [{
StatusCode: 200,
ResponseModels: {
'application/json': 'CreateResponse',
},
ResponseParameters: {
'method.response.header.x-header': true,
'method.response.header.x-other-header': true,
},
},
{
StatusCode: 400,
ResponseModels: {
'application/json': 'ErrorResponse'
},
ResponseParameters: {
'method.response.header.x-header': true,
},
},
{
StatusCode: 404,
ResponseModels: {
'application/json': 'ErrorResponse'
},
ResponseParameters: {
'method.response.header.x-header': true,
},
}],
},
},
someotherpath_get: {
some: 'other_configuration',
DependsOn: ['CrazyResponseModel'],
Properties: {
MethodResponses: [{
StatusCode: 204,
ResponseModels: {
'application/json': 'CrazyResponse',
},
ResponseParameters: {
'method.response.header.x-header': true,
'method.response.header.x-other-header': true,
},
}],
}
},
},
Outputs: {
AwsDocApiId: {
Description: 'API ID',
Value: {
Ref: 'ApiGatewayRestApi',
},
}
},
});
});

it('should only add request models to ApiGateway methods', function () {
this.serverlessMock.variables.service.custom.documentation.models = [];
this.serverlessMock.service._functionNames = ['test', 'blub'];
Expand Down
8 changes: 8 additions & 0 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ module.exports = {
ResponseModels: response.responseModels,
};

if(response.responseHeaders){
const methodResponseHeaders = {};
response.responseHeaders.forEach(header => {
methodResponseHeaders[`method.response.header.${header.name}`] = true
});
_response.ResponseParameters = methodResponseHeaders;
}

this.addModelDependencies(_response.ResponseModels, resource);
resource.Properties.MethodResponses.push(_response);
});
Expand Down

0 comments on commit ddd4811

Please sign in to comment.