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

Commit 58a14fc

Browse files
shevchenkostchock
authored andcommitted
Mapping response code (#16)
* fixed problem with missing responce headers * added test * updated patch version * fixed mapping response codes * added object existence validation * modify forming response method * added new test * added dependOn section * updated version
1 parent ddd4811 commit 58a14fc

File tree

3 files changed

+124
-30
lines changed

3 files changed

+124
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-aws-documentation",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"description": "Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway",
55
"main": "src/index.js",
66
"scripts": {

src/index.spec.js

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const proxyquire = require('proxyquire');
22

3-
describe('ServerlessAWSDocumentation', function() {
3+
describe('ServerlessAWSDocumentation', function () {
44
const awsMock = {
55
APIGateway: {}
66
};
@@ -60,20 +60,20 @@ describe('ServerlessAWSDocumentation', function() {
6060
},
6161
};
6262

63-
this.serverlessMock.providers.aws.naming.getMethodLogicalId.and.callFake((resourcename, method) => {
64-
return `${resourcename}_${method}`;
65-
});
63+
this.serverlessMock.providers.aws.naming.getMethodLogicalId.and.callFake((resourcename, method) => {
64+
return `${resourcename}_${method}`;
65+
});
6666

67-
this.serverlessMock.providers.aws.naming.normalizePath.and.callFake((path) => {
68-
return path.replace(/\//g, '');
69-
});
67+
this.serverlessMock.providers.aws.naming.normalizePath.and.callFake((path) => {
68+
return path.replace(/\//g, '');
69+
});
7070

71-
this.optionsMock = {};
71+
this.optionsMock = {};
7272

73-
this.plugin = new ServerlessAWSDocumentation(this.serverlessMock, this.optionsMock);
73+
this.plugin = new ServerlessAWSDocumentation(this.serverlessMock, this.optionsMock);
7474
});
7575

76-
describe('before deploy', function() {
76+
describe('before deploy', function () {
7777

7878
it('should init', function () {
7979
delete this.serverlessMock.variables.service.custom;
@@ -305,6 +305,89 @@ describe('ServerlessAWSDocumentation', function() {
305305
});
306306
});
307307

308+
it('should only add response methods whith existence MethodResponses to ApiGateway methods', function () {
309+
this.serverlessMock.variables.service.custom.documentation.models = [];
310+
this.serverlessMock.service._functionNames = ['test'];
311+
this.serverlessMock.service._functions = {
312+
test: {
313+
events: [{
314+
http: {
315+
path: 'some/path',
316+
method: 'post',
317+
cors: true,
318+
private: true,
319+
documentation: {
320+
methodResponses: [
321+
{
322+
statusCode: 200,
323+
responseModels: {
324+
'application/json': 'CreateResponse',
325+
},
326+
},
327+
{
328+
statusCode: 404,
329+
responseModels: {
330+
'application/json': 'ErrorResponse'
331+
},
332+
},
333+
],
334+
}
335+
},
336+
}],
337+
},
338+
};
339+
340+
const resources = this.serverlessMock.service.provider.compiledCloudFormationTemplate.Resources;
341+
resources.somepath_post = {
342+
some: 'configuration',
343+
Properties: {
344+
MethodResponses: [{
345+
StatusCode: 200,
346+
},
347+
{
348+
StatusCode: 404,
349+
}],
350+
},
351+
};
352+
353+
this.plugin.beforeDeploy();
354+
355+
expect(this.serverlessMock.service.provider.compiledCloudFormationTemplate).toEqual({
356+
Resources: {
357+
ExistingResource: {
358+
with: 'configuration',
359+
},
360+
somepath_post: {
361+
some: 'configuration',
362+
DependsOn: ['CreateResponseModel', 'ErrorResponseModel'],
363+
Properties: {
364+
MethodResponses: [{
365+
StatusCode: 200,
366+
ResponseModels: {
367+
'application/json': 'CreateResponse',
368+
},
369+
},
370+
{
371+
StatusCode: 404,
372+
ResponseModels: {
373+
'application/json': 'ErrorResponse'
374+
},
375+
}],
376+
},
377+
},
378+
},
379+
Outputs: {
380+
AwsDocApiId: {
381+
Description: 'API ID',
382+
Value: {
383+
Ref: 'ApiGatewayRestApi',
384+
},
385+
}
386+
},
387+
});
388+
});
389+
390+
308391
it('should only add response methods with response headers to ApiGateway methods', function () {
309392
this.serverlessMock.variables.service.custom.documentation.models = [];
310393
this.serverlessMock.service._functionNames = ['test', 'blub'];
@@ -808,7 +891,7 @@ describe('ServerlessAWSDocumentation', function() {
808891
});
809892
});
810893

811-
describe('after deploy', function() {
894+
describe('after deploy', function () {
812895
beforeEach(function () {
813896
this.apiGatewayMock = jasmine.createSpyObj([
814897
'getDocumentationParts',
@@ -835,10 +918,10 @@ describe('ServerlessAWSDocumentation', function() {
835918
it('should get stack description', function () {
836919
this.optionsMock.stage = 'megastage';
837920
this.optionsMock.region = 'hyperregion';
838-
this.serverlessMock.providers.aws.request.and.returnValue(new Promise(() => {}));
921+
this.serverlessMock.providers.aws.request.and.returnValue(new Promise(() => { }));
839922
this.serverlessMock.providers.aws.naming.getStackName.and.returnValue('superstack');
840923
this.plugin.afterDeploy();
841-
expect(this.serverlessMock.providers.aws.request).toHaveBeenCalledWith('CloudFormation', 'describeStacks', { StackName: 'superstack'}, 'megastage', 'hyperregion');
924+
expect(this.serverlessMock.providers.aws.request).toHaveBeenCalledWith('CloudFormation', 'describeStacks', { StackName: 'superstack' }, 'megastage', 'hyperregion');
842925
});
843926

844927
it('should build documentation with deploying and upload to api gateway', function (done) {

src/models.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,36 @@ module.exports = {
2323

2424
addMethodResponses: function addMethodResponses(resource, documentation) {
2525
if (documentation.methodResponses) {
26-
resource.Properties.MethodResponses = [];
26+
if (resource.Properties.MethodResponses) {
27+
resource.Properties.MethodResponses.forEach(originalResponse => {
28+
documentation.methodResponses.forEach(response => {
29+
if (originalResponse.StatusCode === response.statusCode) {
30+
originalResponse.ResponseModels = response.responseModels;
31+
this.addModelDependencies(originalResponse.ResponseModels, resource);
32+
}
33+
});
34+
});
35+
} else {
36+
resource.Properties.MethodResponses = [];
2737

28-
documentation.methodResponses.forEach(response => {
29-
const _response = {
30-
StatusCode: response.statusCode,
31-
ResponseModels: response.responseModels,
32-
};
38+
documentation.methodResponses.forEach(response => {
39+
const _response = {
40+
StatusCode: response.statusCode,
41+
ResponseModels: response.responseModels
42+
};
3343

34-
if(response.responseHeaders){
35-
const methodResponseHeaders = {};
36-
response.responseHeaders.forEach(header => {
37-
methodResponseHeaders[`method.response.header.${header.name}`] = true
38-
});
39-
_response.ResponseParameters = methodResponseHeaders;
40-
}
44+
if (response.responseHeaders) {
45+
const methodResponseHeaders = {};
46+
response.responseHeaders.forEach(header => {
47+
methodResponseHeaders[`method.response.header.${header.name}`] = true
48+
});
49+
_response.ResponseParameters = methodResponseHeaders;
50+
}
4151

42-
this.addModelDependencies(_response.ResponseModels, resource);
43-
resource.Properties.MethodResponses.push(_response);
44-
});
52+
this.addModelDependencies(_response.ResponseModels, resource);
53+
resource.Properties.MethodResponses.push(_response);
54+
});
55+
}
4556
}
4657
},
4758

0 commit comments

Comments
 (0)