Skip to content

Commit 8499da3

Browse files
committed
add validate.js test
1 parent e6cfb94 commit 8499da3

File tree

5 files changed

+128
-81
lines changed

5 files changed

+128
-81
lines changed

lib/apiGateway/methods.test.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,40 @@ describe('#getAllServiceProxies()', () => {
2525
})
2626

2727
describe('#getMethodResponses()', () => {
28-
it('should return a corresponding methodResponses resource', () => {
29-
return
30-
serverlessApigatewayServiceProxy
31-
.getMethodResponses()
32-
.to.eventually.be.fulfilled.then((json) => {
33-
expect(json.Properties).to.have.property('MethodResponses')
34-
})
28+
it('should return a corresponding methodResponses resource', async () => {
29+
await expect(
30+
serverlessApigatewayServiceProxy.getMethodResponses()
31+
).to.eventually.have.nested.property('Properties.MethodResponses')
3532
})
3633

37-
it('should set Access-Control-Allow-Origin header when cors is true', () => {
38-
return
39-
serverlessApigatewayServiceProxy
40-
.getMethodResponses({
34+
it('should set Access-Control-Allow-Origin header when cors is true', async () => {
35+
await expect(
36+
serverlessApigatewayServiceProxy.getMethodResponses({
4137
cors: {
42-
origins: ['*', 'http://example.com']
38+
origin: '*'
4339
}
4440
})
45-
.to.eventually.be.fulfilled.then((json) => {
46-
expect(
47-
json.Properties.MethodResponses[0].ResponseParameters[
48-
'method.response.header.Access-Control-Allow-Origin'
49-
]
50-
).to.equal("'*,http://example.com'")
51-
52-
expect(
53-
serverlessApigatewayServiceProxy.getMethodResponses({
54-
cors: {
55-
origin: '*'
56-
}
57-
}).Properties.MethodResponses[0].ResponseParameters[
58-
'method.response.header.Access-Control-Allow-Origin'
59-
]
60-
).to.equal("'*'")
61-
})
41+
).to.be.fulfilled.then((json) => {
42+
expect(
43+
json.Properties.MethodResponses[0].ResponseParameters[
44+
'method.response.header.Access-Control-Allow-Origin'
45+
]
46+
).to.equal("'*'")
47+
})
6248

63-
return
64-
serverlessApigatewayServiceProxy
65-
.getMethodResponses({
49+
await expect(
50+
serverlessApigatewayServiceProxy.getMethodResponses({
6651
cors: {
67-
origin: '*'
52+
origins: ['*', 'http://example.com']
6853
}
6954
})
70-
.to.eventually.be.fulfilled.then((json) => {
71-
expect(
72-
json.Properties.MethodResponses[0].ResponseParameters[
73-
'method.response.header.Access-Control-Allow-Origin'
74-
]
75-
).to.equal("'*'")
76-
})
55+
).to.be.fulfilled.then((json) => {
56+
expect(
57+
json.Properties.MethodResponses[0].ResponseParameters[
58+
'method.response.header.Access-Control-Allow-Origin'
59+
]
60+
).to.equal("'*,http://example.com'")
61+
})
7762
})
7863
})
7964
})

lib/apiGateway/validate.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,32 @@ module.exports = {
99
const corsPreflight = {}
1010
await BbPromise.all(
1111
this.getAllServiceProxies().map(async (serviceProxy) => {
12-
Object.keys(serviceProxy).forEach(async (functionName) => {
13-
await this.checkAllowedService(functionName)
14-
const http = serviceProxy[functionName]
15-
http.path = await this.getProxyPath(serviceProxy[functionName])
16-
http.method = await this.getProxyMethod(serviceProxy[functionName])
17-
18-
if (serviceProxy[functionName].cors) {
19-
http.cors = await this.getCors(serviceProxy[functionName])
20-
21-
const cors = corsPreflight[http.path] || {}
22-
23-
cors.headers = _.union(http.cors.headers, cors.headers)
24-
cors.methods = _.union(http.cors.methods, cors.methods)
25-
cors.origins = _.union(http.cors.origins, cors.origins)
26-
cors.origin = http.cors.origin || '*'
27-
cors.allowCredentials = cors.allowCredentials || http.cors.allowCredentials
28-
29-
// when merging, last one defined wins
30-
if (_.has(http.cors, 'maxAge')) {
31-
cors.maxAge = http.cors.maxAge
32-
}
33-
34-
corsPreflight[http.path] = cors
12+
const serviceName = this.getServiceName(serviceProxy)
13+
await this.checkAllowedService(serviceName)
14+
const http = serviceProxy[serviceName]
15+
http.path = await this.getProxyPath(serviceProxy[serviceName])
16+
http.method = await this.getProxyMethod(serviceProxy[serviceName])
17+
18+
if (serviceProxy[serviceName].cors) {
19+
http.cors = await this.getCors(serviceProxy[serviceName])
20+
21+
const cors = corsPreflight[http.path] || {}
22+
23+
cors.headers = _.union(http.cors.headers, cors.headers)
24+
cors.methods = _.union(http.cors.methods, cors.methods)
25+
cors.origins = _.union(http.cors.origins, cors.origins)
26+
cors.origin = http.cors.origin || '*'
27+
cors.allowCredentials = cors.allowCredentials || http.cors.allowCredentials
28+
29+
// when merging, last one defined wins
30+
if (_.has(http.cors, 'maxAge')) {
31+
cors.maxAge = http.cors.maxAge
3532
}
3633

37-
events.push({ functionName, http })
38-
})
34+
corsPreflight[http.path] = cors
35+
}
36+
37+
events.push({ serviceName, http })
3938
})
4039
)
4140
return {
@@ -51,6 +50,7 @@ module.exports = {
5150
`Invalid APIG proxy "${serviceName}".`,
5251
` This plugin supported Proxies are: ${allowedProxies.join(', ')}.`
5352
].join('')
53+
console.log(errorMessage)
5454
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
5555
}
5656
},

lib/apiGateway/validate.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const chai = require('chai')
4+
const Serverless = require('serverless/lib/Serverless')
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
6+
const ServerlessApigatewayServiceProxy = require('./../index')
7+
8+
chai.use(require('chai-as-promised'))
9+
const expect = require('chai').expect
10+
11+
describe('#validateServiceProxies()', () => {
12+
let serverless
13+
let serverlessApigatewayServiceProxy
14+
15+
beforeEach(() => {
16+
serverless = new Serverless()
17+
serverless.servicePath = true
18+
serverless.service.service = 'apigw-service-proxy'
19+
const options = {
20+
stage: 'dev',
21+
region: 'us-east-1'
22+
}
23+
serverless.setProvider('aws', new AwsProvider(serverless))
24+
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
25+
})
26+
27+
it('should reject an invalid proxies', async () => {
28+
serverlessApigatewayServiceProxy.serverless.service.custom = {
29+
apiGatewayServiceProxies: [
30+
{
31+
xxxxx: {
32+
path: '/kinesis',
33+
method: 'post'
34+
}
35+
}
36+
]
37+
}
38+
39+
await expect(serverlessApigatewayServiceProxy.validateServiceProxies()).to.be.rejectedWith(
40+
'Invalid APIG proxy "xxxxx". This plugin supported Proxies are: kinesis, sqs.'
41+
)
42+
})
43+
})

lib/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ module.exports = {
66
return this.serverless.service.custom.apiGatewayServiceProxies
77
}
88
return []
9+
},
10+
11+
getServiceName(serviceProxy) {
12+
return Object.keys(serviceProxy)[0]
913
}
1014
}

lib/utils.test.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Serverless = require('serverless/lib/Serverless')
55
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
66
const ServerlessApigatewayServiceProxy = require('./index')
77

8-
describe('#getAllServiceProxies()', () => {
8+
describe('#utils()', () => {
99
let serverless
1010
let serverlessApigatewayServiceProxy
1111

@@ -21,29 +21,44 @@ describe('#getAllServiceProxies()', () => {
2121
serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options)
2222
})
2323

24-
it('should return this plugin configutration', () => {
25-
serverless.service.custom = {
26-
apiGatewayServiceProxies: [
24+
describe('#getAllServiceProxies()', () => {
25+
it('should return this plugin configutration', () => {
26+
serverless.service.custom = {
27+
apiGatewayServiceProxies: [
28+
{
29+
kinesis: {
30+
path: '/kinesis',
31+
method: 'post'
32+
}
33+
}
34+
]
35+
}
36+
37+
expect(serverlessApigatewayServiceProxy.getAllServiceProxies()).to.deep.equal([
2738
{
2839
kinesis: {
2940
path: '/kinesis',
3041
method: 'post'
3142
}
3243
}
33-
]
34-
}
44+
])
45+
})
3546

36-
expect(serverlessApigatewayServiceProxy.getAllServiceProxies()).to.deep.equal([
37-
{
38-
kinesis: {
39-
path: '/kinesis',
40-
method: 'post'
41-
}
42-
}
43-
])
47+
it('should return empty if no configuration', () => {
48+
expect(serverlessApigatewayServiceProxy.getAllServiceProxies()).to.be.empty
49+
})
4450
})
4551

46-
it('should return empty if no configuration', () => {
47-
expect(serverlessApigatewayServiceProxy.getAllServiceProxies()).to.be.empty
52+
describe('#getServiceName()', () => {
53+
it('should return corresponding service name', () => {
54+
expect(
55+
serverlessApigatewayServiceProxy.getServiceName({
56+
kinesis: {
57+
path: '/kinesis',
58+
method: 'post'
59+
}
60+
})
61+
).to.be.equal('kinesis')
62+
})
4863
})
4964
})

0 commit comments

Comments
 (0)