Skip to content

Commit 2327a08

Browse files
committed
update validate.test.js
1 parent 8499da3 commit 2327a08

File tree

2 files changed

+355
-9
lines changed

2 files changed

+355
-9
lines changed

lib/apiGateway/validate.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = {
1212
const serviceName = this.getServiceName(serviceProxy)
1313
await this.checkAllowedService(serviceName)
1414
const http = serviceProxy[serviceName]
15-
http.path = await this.getProxyPath(serviceProxy[serviceName])
16-
http.method = await this.getProxyMethod(serviceProxy[serviceName])
15+
http.path = await this.getProxyPath(serviceProxy[serviceName], serviceName)
16+
http.method = await this.getProxyMethod(serviceProxy[serviceName], serviceName)
1717

1818
if (serviceProxy[serviceName].cors) {
1919
http.cors = await this.getCors(serviceProxy[serviceName])
@@ -31,6 +31,10 @@ module.exports = {
3131
cors.maxAge = http.cors.maxAge
3232
}
3333

34+
if (_.has(http.cors, 'cacheControl')) {
35+
cors.cacheControl = http.cors.cacheControl
36+
}
37+
3438
corsPreflight[http.path] = cors
3539
}
3640

@@ -50,20 +54,24 @@ module.exports = {
5054
`Invalid APIG proxy "${serviceName}".`,
5155
` This plugin supported Proxies are: ${allowedProxies.join(', ')}.`
5256
].join('')
53-
console.log(errorMessage)
5457
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
5558
}
5659
},
5760

58-
async getProxyPath(proxy) {
59-
if (typeof proxy.path === 'string') {
61+
async getProxyPath(proxy, serviceName) {
62+
if (proxy.path && typeof proxy.path === 'string') {
6063
return proxy.path.replace(/^\//, '').replace(/\/$/, '')
6164
}
62-
return BbPromise.reject(new this.serverless.classes.Error('Invalid service proxy syntax'))
65+
66+
return BbPromise.reject(
67+
new this.serverless.classes.Error(
68+
`Missing or invalid "path" property in ${serviceName} proxy`
69+
)
70+
)
6371
},
6472

65-
async getProxyMethod(proxy) {
66-
if (typeof proxy.method === 'string') {
73+
async getProxyMethod(proxy, serviceName) {
74+
if (proxy.method && typeof proxy.method === 'string') {
6775
const method = proxy.method.toLowerCase()
6876

6977
const allowedMethods = ['get', 'post', 'put', 'patch', 'options', 'head', 'delete', 'any']
@@ -76,7 +84,11 @@ module.exports = {
7684
}
7785
return method
7886
}
79-
return BbPromise.reject(new this.serverless.classes.Error('Invalid service proxy syntax'))
87+
return BbPromise.reject(
88+
new this.serverless.classes.Error(
89+
`Missing or invalid "method" property in ${serviceName} proxy`
90+
)
91+
)
8092
},
8193

8294
async getCors(proxy) {
@@ -130,6 +142,13 @@ module.exports = {
130142
if (cors.methods.indexOf(proxy.method.toUpperCase()) === NOT_FOUND) {
131143
cors.methods.push(proxy.method.toUpperCase())
132144
}
145+
146+
if (_.has(cors, 'maxAge')) {
147+
if (!_.isInteger(cors.maxAge) || cors.maxAge < 1) {
148+
const errorMessage = 'maxAge should be an integer over 0'
149+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
150+
}
151+
}
133152
} else {
134153
cors.methods.push(proxy.method.toUpperCase())
135154
}

0 commit comments

Comments
 (0)