|
| 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('#compileMethodsToKinesis()', () => { |
| 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 | + serverless.service.provider.compiledCloudFormationTemplate = { |
| 25 | + Resources: {} |
| 26 | + } |
| 27 | + serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should create corresponding resources when kinesis proxies are given', async () => { |
| 31 | + serverlessApigatewayServiceProxy.validated = { |
| 32 | + events: [ |
| 33 | + { |
| 34 | + serviceName: 'kinesis', |
| 35 | + http: { |
| 36 | + streamName: 'myStream', |
| 37 | + path: 'kinesis', |
| 38 | + method: 'post' |
| 39 | + } |
| 40 | + } |
| 41 | + ] |
| 42 | + } |
| 43 | + serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi' |
| 44 | + serverlessApigatewayServiceProxy.apiGatewayResources = { |
| 45 | + kinesis: { |
| 46 | + name: 'kinesis', |
| 47 | + resourceLogicalId: 'ApiGatewayResourceKinesis' |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + await expect(serverlessApigatewayServiceProxy.compileMethodsToKinesis()).to.be.fulfilled |
| 52 | + |
| 53 | + expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({ |
| 54 | + ApiGatewayMethodkinesisPost: { |
| 55 | + Type: 'AWS::ApiGateway::Method', |
| 56 | + Properties: { |
| 57 | + HttpMethod: 'POST', |
| 58 | + RequestParameters: {}, |
| 59 | + AuthorizationType: 'NONE', |
| 60 | + ApiKeyRequired: false, |
| 61 | + ResourceId: { Ref: 'ApiGatewayResourceKinesis' }, |
| 62 | + RestApiId: { Ref: 'ApiGatewayRestApi' }, |
| 63 | + Integration: { |
| 64 | + IntegrationHttpMethod: 'POST', |
| 65 | + Type: 'AWS', |
| 66 | + Credentials: { 'Fn::GetAtt': ['ApigatewayToKinesisRole', 'Arn'] }, |
| 67 | + Uri: { |
| 68 | + 'Fn::Join': [ |
| 69 | + '', |
| 70 | + ['arn:aws:apigateway:', { Ref: 'AWS::Region' }, ':kinesis:action/PutRecord'] |
| 71 | + ] |
| 72 | + }, |
| 73 | + PassthroughBehavior: 'NEVER', |
| 74 | + RequestTemplates: { |
| 75 | + 'application/json': { |
| 76 | + 'Fn::Join': [ |
| 77 | + '', |
| 78 | + [ |
| 79 | + '{', |
| 80 | + '"StreamName": "', |
| 81 | + '"myStream"', |
| 82 | + '",', |
| 83 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 84 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 85 | + '}' |
| 86 | + ] |
| 87 | + ] |
| 88 | + }, |
| 89 | + 'application/x-www-form-urlencoded': { |
| 90 | + 'Fn::Join': [ |
| 91 | + '', |
| 92 | + [ |
| 93 | + '{', |
| 94 | + '"StreamName": "', |
| 95 | + '"myStream"', |
| 96 | + '",', |
| 97 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 98 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 99 | + '}' |
| 100 | + ] |
| 101 | + ] |
| 102 | + } |
| 103 | + }, |
| 104 | + IntegrationResponses: [ |
| 105 | + { |
| 106 | + StatusCode: 200, |
| 107 | + SelectionPattern: 200, |
| 108 | + ResponseParameters: {}, |
| 109 | + ResponseTemplates: {} |
| 110 | + }, |
| 111 | + { |
| 112 | + StatusCode: 400, |
| 113 | + SelectionPattern: 400, |
| 114 | + ResponseParameters: {}, |
| 115 | + ResponseTemplates: {} |
| 116 | + } |
| 117 | + ] |
| 118 | + }, |
| 119 | + MethodResponses: [ |
| 120 | + { ResponseParameters: {}, ResponseModels: {}, StatusCode: 200 }, |
| 121 | + { ResponseParameters: {}, ResponseModels: {}, StatusCode: 400 } |
| 122 | + ] |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + it('should create corresponding resources when kinesis proxies are given with cors', async () => { |
| 129 | + serverlessApigatewayServiceProxy.validated = { |
| 130 | + events: [ |
| 131 | + { |
| 132 | + serviceName: 'kinesis', |
| 133 | + http: { |
| 134 | + streamName: 'myStream', |
| 135 | + path: 'kinesis', |
| 136 | + method: 'post', |
| 137 | + cors: { |
| 138 | + origins: ['*'], |
| 139 | + origin: '*', |
| 140 | + methods: ['OPTIONS', 'POST'], |
| 141 | + headers: [ |
| 142 | + 'Content-Type', |
| 143 | + 'X-Amz-Date', |
| 144 | + 'Authorization', |
| 145 | + 'X-Api-Key', |
| 146 | + 'X-Amz-Security-Token', |
| 147 | + 'X-Amz-User-Agent' |
| 148 | + ], |
| 149 | + allowCredentials: false |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi' |
| 156 | + serverlessApigatewayServiceProxy.apiGatewayResources = { |
| 157 | + kinesis: { |
| 158 | + name: 'kinesis', |
| 159 | + resourceLogicalId: 'ApiGatewayResourceKinesis' |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + await expect(serverlessApigatewayServiceProxy.compileMethodsToKinesis()).to.be.fulfilled |
| 164 | + |
| 165 | + expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({ |
| 166 | + ApiGatewayMethodkinesisPost: { |
| 167 | + Type: 'AWS::ApiGateway::Method', |
| 168 | + Properties: { |
| 169 | + HttpMethod: 'POST', |
| 170 | + RequestParameters: {}, |
| 171 | + AuthorizationType: 'NONE', |
| 172 | + ApiKeyRequired: false, |
| 173 | + ResourceId: { Ref: 'ApiGatewayResourceKinesis' }, |
| 174 | + RestApiId: { Ref: 'ApiGatewayRestApi' }, |
| 175 | + Integration: { |
| 176 | + IntegrationHttpMethod: 'POST', |
| 177 | + Type: 'AWS', |
| 178 | + Credentials: { 'Fn::GetAtt': ['ApigatewayToKinesisRole', 'Arn'] }, |
| 179 | + Uri: { |
| 180 | + 'Fn::Join': [ |
| 181 | + '', |
| 182 | + ['arn:aws:apigateway:', { Ref: 'AWS::Region' }, ':kinesis:action/PutRecord'] |
| 183 | + ] |
| 184 | + }, |
| 185 | + PassthroughBehavior: 'NEVER', |
| 186 | + RequestTemplates: { |
| 187 | + 'application/json': { |
| 188 | + 'Fn::Join': [ |
| 189 | + '', |
| 190 | + [ |
| 191 | + '{', |
| 192 | + '"StreamName": "', |
| 193 | + '"myStream"', |
| 194 | + '",', |
| 195 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 196 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 197 | + '}' |
| 198 | + ] |
| 199 | + ] |
| 200 | + }, |
| 201 | + 'application/x-www-form-urlencoded': { |
| 202 | + 'Fn::Join': [ |
| 203 | + '', |
| 204 | + [ |
| 205 | + '{', |
| 206 | + '"StreamName": "', |
| 207 | + '"myStream"', |
| 208 | + '",', |
| 209 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 210 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 211 | + '}' |
| 212 | + ] |
| 213 | + ] |
| 214 | + } |
| 215 | + }, |
| 216 | + IntegrationResponses: [ |
| 217 | + { |
| 218 | + StatusCode: 200, |
| 219 | + SelectionPattern: 200, |
| 220 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 221 | + ResponseTemplates: {} |
| 222 | + }, |
| 223 | + { |
| 224 | + StatusCode: 400, |
| 225 | + SelectionPattern: 400, |
| 226 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 227 | + ResponseTemplates: {} |
| 228 | + } |
| 229 | + ] |
| 230 | + }, |
| 231 | + MethodResponses: [ |
| 232 | + { |
| 233 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 234 | + ResponseModels: {}, |
| 235 | + StatusCode: 200 |
| 236 | + }, |
| 237 | + { |
| 238 | + ResponseParameters: { 'method.response.header.Access-Control-Allow-Origin': "'*'" }, |
| 239 | + ResponseModels: {}, |
| 240 | + StatusCode: 400 |
| 241 | + } |
| 242 | + ] |
| 243 | + } |
| 244 | + } |
| 245 | + }) |
| 246 | + }) |
| 247 | + |
| 248 | + it('should return the default template for application/json when one is not given', async () => { |
| 249 | + const httpWithoutRequestTemplate = { |
| 250 | + path: 'foo/bar1', |
| 251 | + method: 'post', |
| 252 | + request: { |
| 253 | + template: { |
| 254 | + 'application/x-www-form-urlencoded': 'custom template' |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + const resource = await serverlessApigatewayServiceProxy.getKinesisMethodIntegration( |
| 260 | + httpWithoutRequestTemplate |
| 261 | + ) |
| 262 | + |
| 263 | + expect( |
| 264 | + resource.Properties.Integration.RequestTemplates['application/json']['Fn::Join'] |
| 265 | + ).to.be.deep.equal([ |
| 266 | + '', |
| 267 | + [ |
| 268 | + '{', |
| 269 | + '"StreamName": "', |
| 270 | + '"undefined"', |
| 271 | + '",', |
| 272 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 273 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 274 | + '}' |
| 275 | + ] |
| 276 | + ]) |
| 277 | + }) |
| 278 | + |
| 279 | + it('should return a custom template for application/json when one is given', async () => { |
| 280 | + const httpWithRequestTemplate = { |
| 281 | + path: 'foo/bar1', |
| 282 | + method: 'post', |
| 283 | + request: { |
| 284 | + template: { |
| 285 | + 'application/json': 'custom template' |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + const resource = await serverlessApigatewayServiceProxy.getKinesisMethodIntegration( |
| 290 | + httpWithRequestTemplate |
| 291 | + ) |
| 292 | + expect(resource.Properties.Integration.RequestTemplates['application/json']).to.be.equal( |
| 293 | + 'custom template' |
| 294 | + ) |
| 295 | + }) |
| 296 | + |
| 297 | + it('should return the default for application/x-www-form-urlencoded when one is not given', async () => { |
| 298 | + const httpWithoutRequestTemplate = { |
| 299 | + path: 'foo/bar1', |
| 300 | + method: 'post', |
| 301 | + request: { |
| 302 | + template: { |
| 303 | + 'application/json': 'custom template' |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + const resource = await serverlessApigatewayServiceProxy.getKinesisMethodIntegration( |
| 308 | + httpWithoutRequestTemplate |
| 309 | + ) |
| 310 | + expect( |
| 311 | + resource.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded'][ |
| 312 | + 'Fn::Join' |
| 313 | + ] |
| 314 | + ).to.be.deep.equal([ |
| 315 | + '', |
| 316 | + [ |
| 317 | + '{', |
| 318 | + '"StreamName": "', |
| 319 | + '"undefined"', |
| 320 | + '",', |
| 321 | + '"Data": "$util.base64Encode($input.json(\'$.Data\'))",', |
| 322 | + '"PartitionKey": "$input.path(\'$.PartitionKey\')"', |
| 323 | + '}' |
| 324 | + ] |
| 325 | + ]) |
| 326 | + }) |
| 327 | + |
| 328 | + it('should return a custom template for application/x-www-form-urlencoded when one is given', async () => { |
| 329 | + const httpWithRequestTemplate = { |
| 330 | + path: 'foo/bar1', |
| 331 | + method: 'post', |
| 332 | + request: { |
| 333 | + template: { |
| 334 | + 'application/x-www-form-urlencoded': 'custom template' |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + const resource = await serverlessApigatewayServiceProxy.getKinesisMethodIntegration( |
| 339 | + httpWithRequestTemplate |
| 340 | + ) |
| 341 | + expect( |
| 342 | + resource.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded'] |
| 343 | + ).to.be.equal('custom template') |
| 344 | + }) |
| 345 | +}) |
0 commit comments