-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
399 additions
and
143 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
__tests__/integration/kinesis/multiple-integrations/service/serverless.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
service: multiple-kinesis-proxy | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs10.x | ||
|
||
plugins: | ||
localPath: './../../../../../../' | ||
modules: | ||
- serverless-apigateway-service-proxy | ||
|
||
custom: | ||
apiGatewayServiceProxies: | ||
- kinesis: | ||
path: /kinesis1 | ||
method: post | ||
streamName: { Ref: 'YourStream1' } | ||
cors: true | ||
|
||
- kinesis: | ||
path: /kinesis2 | ||
method: post | ||
streamName: { Ref: 'YourStream2' } | ||
cors: true | ||
|
||
- kinesis: | ||
path: /kinesis3 | ||
method: post | ||
streamName: { Ref: 'YourStream3' } | ||
cors: true | ||
|
||
resources: | ||
Resources: | ||
YourStream1: | ||
Type: AWS::Kinesis::Stream | ||
Properties: | ||
ShardCount: 1 | ||
YourStream2: | ||
Type: AWS::Kinesis::Stream | ||
Properties: | ||
ShardCount: 1 | ||
YourStream3: | ||
Type: AWS::Kinesis::Stream | ||
Properties: | ||
ShardCount: 1 |
40 changes: 40 additions & 0 deletions
40
__tests__/integration/kinesis/multiple-integrations/tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const fetch = require('node-fetch') | ||
const { deployWithRandomStage, removeService } = require('../../../utils') | ||
|
||
describe('Multiple Kinesis Proxy Integrations Test', () => { | ||
let endpoint | ||
let stage | ||
const config = '__tests__/integration/kinesis/multiple-integrations/service/serverless.yml' | ||
|
||
beforeAll(async () => { | ||
const result = await deployWithRandomStage(config) | ||
stage = result.stage | ||
endpoint = result.endpoint | ||
}) | ||
|
||
afterAll(() => { | ||
removeService(stage, config) | ||
}) | ||
|
||
it('should get correct response from multiple kinesis proxy endpoints', async () => { | ||
const streams = ['kinesis1', 'kinesis2', 'kinesis3'] | ||
|
||
for (const stream of streams) { | ||
const testEndpoint = `${endpoint}/${stream}` | ||
|
||
const response = await fetch(testEndpoint, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ Data: `data for stream ${stream}`, PartitionKey: 'some key' }) | ||
}) | ||
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*') | ||
expect(response.status).to.be.equal(200) | ||
const body = await response.json() | ||
expect(body).to.have.own.property('ShardId') | ||
expect(body).to.have.own.property('SequenceNumber') | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
__tests__/integration/sqs/multiple-integrations/service/serverless.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
service: multiple-sqs-proxy | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs10.x | ||
|
||
plugins: | ||
localPath: './../../../../../../' | ||
modules: | ||
- serverless-apigateway-service-proxy | ||
|
||
custom: | ||
apiGatewayServiceProxies: | ||
- sqs: | ||
path: /sqs1 | ||
method: post | ||
queueName: { 'Fn::GetAtt': ['SQSQueue1', 'QueueName'] } | ||
cors: true | ||
|
||
- sqs: | ||
path: /sqs2 | ||
method: post | ||
queueName: { 'Fn::GetAtt': ['SQSQueue2', 'QueueName'] } | ||
cors: true | ||
|
||
- sqs: | ||
path: /sqs3 | ||
method: post | ||
queueName: { 'Fn::GetAtt': ['SQSQueue3', 'QueueName'] } | ||
cors: true | ||
|
||
resources: | ||
Resources: | ||
SQSQueue1: | ||
Type: 'AWS::SQS::Queue' | ||
SQSQueue2: | ||
Type: 'AWS::SQS::Queue' | ||
SQSQueue3: | ||
Type: 'AWS::SQS::Queue' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const fetch = require('node-fetch') | ||
const { deployWithRandomStage, removeService } = require('../../../utils') | ||
|
||
describe('Multiple SQS Proxy Integrations Test', () => { | ||
let endpoint | ||
let stage | ||
const config = '__tests__/integration/sqs/multiple-integrations/service/serverless.yml' | ||
|
||
beforeAll(async () => { | ||
const result = await deployWithRandomStage(config) | ||
stage = result.stage | ||
endpoint = result.endpoint | ||
}) | ||
|
||
afterAll(() => { | ||
removeService(stage, config) | ||
}) | ||
|
||
it('should get correct response from multiple sqs proxy endpoints', async () => { | ||
const queues = ['sqs1', 'sqs2', 'sqs3'] | ||
|
||
for (const queue of queues) { | ||
const testEndpoint = `${endpoint}/${queue}` | ||
const response = await fetch(testEndpoint, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ message: `message for ${queue}` }) | ||
}) | ||
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*') | ||
expect(response.status).to.be.equal(200) | ||
const body = await response.json() | ||
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property( | ||
'MD5OfMessageAttributes' | ||
) | ||
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MD5OfMessageBody') | ||
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MessageId') | ||
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('SequenceNumber') | ||
expect(body.SendMessageResponse.ResponseMetadata).to.have.own.property('RequestId') | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const yaml = require('js-yaml') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const execSync = require('child_process').execSync | ||
const aws = require('aws-sdk') | ||
const cloudformation = new aws.CloudFormation({ region: 'us-east-1' }) | ||
|
||
async function getApiGatewayEndpoint(stackName) { | ||
const result = await cloudformation.describeStacks({ StackName: stackName }).promise() | ||
|
||
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' }) | ||
.OutputValue | ||
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0] | ||
} | ||
|
||
function deployService(stage, config) { | ||
execSync(`npx serverless deploy --stage ${stage} --config ${path.basename(config)}`, { | ||
stdio: 'inherit', | ||
cwd: path.dirname(config) | ||
}) | ||
} | ||
|
||
function removeService(stage, config) { | ||
execSync(`npx serverless remove --stage ${stage} --config ${path.basename(config)}`, { | ||
stdio: 'inherit', | ||
cwd: path.dirname(config) | ||
}) | ||
} | ||
|
||
async function deployWithRandomStage(config) { | ||
const serviceName = yaml.safeLoad(fs.readFileSync(config)).service | ||
const stage = Math.random() | ||
.toString(32) | ||
.substring(2) | ||
const stackName = `${serviceName}-${stage}` | ||
deployService(stage, config) | ||
const endpoint = await getApiGatewayEndpoint(stackName) | ||
|
||
return { stage, endpoint } | ||
} | ||
|
||
module.exports = { | ||
async getApiGatewayEndpoint(stackName) { | ||
const result = await cloudformation.describeStacks({ StackName: stackName }).promise() | ||
|
||
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' }) | ||
.OutputValue | ||
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0] | ||
}, | ||
|
||
deployService(stage, config) { | ||
execSync(`npx serverless deploy --stage ${stage} --config ${config}`, { | ||
stdio: 'inherit' | ||
}) | ||
}, | ||
|
||
removeService(stage, config) { | ||
execSync(`npx serverless remove --stage ${stage} --config ${config}`, { stdio: 'inherit' }) | ||
} | ||
getApiGatewayEndpoint, | ||
deployService, | ||
removeService, | ||
deployWithRandomStage | ||
} |
Oops, something went wrong.