Skip to content

Commit 1e1e157

Browse files
authored
Merge pull request #7 from horike37/add-integration-test
Add integration tests
2 parents bba947e + 5335224 commit 1e1e157

File tree

11 files changed

+5313
-1081
lines changed

11 files changed

+5313
-1081
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ tmp
4242
.coveralls.yml
4343
tmpdirs-serverless
4444
.eslintcache
45+
.serverless

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ matrix:
88
env:
99
- DISABLE_TESTS=true
1010
- LINTING=true
11+
- node_js: '10.6'
12+
env:
13+
- INTEGRATION_TESTS=true
1114

1215
sudo: false
1316

@@ -17,6 +20,7 @@ install:
1720
script:
1821
- if [[ -z "$DISABLE_TESTS" ]]; then npm run test; fi
1922
- if [[ ! -z "$DISABLE_TESTS" && ! -z "$LINTING" ]]; then npm run lint; fi
23+
- if [[ ! -z "$INTEGRATION_TESTS" && $TRAVIS_BRANCH == "master" && $TRAVIS_PULL_REQUEST == "false" ]]; then npm run integration-test; fi
2024

2125
after_success:
2226
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
service: kinesis-proxy
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs10.x
6+
7+
plugins:
8+
localPath: './../'
9+
modules:
10+
- serverless-apigateway-service-proxy
11+
12+
custom:
13+
apiGatewayServiceProxies:
14+
- kinesis:
15+
path: /kinesis
16+
method: post
17+
streamName: { Ref: 'YourStream' }
18+
cors: true
19+
20+
resources:
21+
Resources:
22+
YourStream:
23+
Type: AWS::Kinesis::Stream
24+
Properties:
25+
ShardCount: 1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
6+
7+
describe('Kinesis Proxy Integration Test', () => {
8+
let endpoint
9+
let stackName
10+
let stage
11+
const config = '__tests__/integration/kinesis/service/serverless.yml'
12+
13+
beforeAll(async () => {
14+
stage = Math.random()
15+
.toString(32)
16+
.substring(2)
17+
stackName = 'kinesis-proxy-' + stage
18+
deployService(stage, config)
19+
endpoint = await getApiGatewayEndpoint(stackName)
20+
})
21+
22+
afterAll(() => {
23+
removeService(stage, config)
24+
})
25+
26+
it('should get correct response from kinesis proxy endpoint', async () => {
27+
const testEndpoint = `${endpoint}/kinesis`
28+
29+
const response = await fetch(testEndpoint, {
30+
method: 'POST',
31+
headers: { 'Content-Type': 'application/json' },
32+
body: JSON.stringify({ Data: 'some data', PartitionKey: 'some key' })
33+
})
34+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
35+
expect(response.status).to.be.equal(200)
36+
const body = await response.json()
37+
expect(body).to.have.own.property('ShardId')
38+
expect(body).to.have.own.property('SequenceNumber')
39+
})
40+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
service: sqs-proxy
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs10.x
6+
7+
plugins:
8+
localPath: './../'
9+
modules:
10+
- serverless-apigateway-service-proxy
11+
12+
custom:
13+
apiGatewayServiceProxies:
14+
- sqs:
15+
path: /sqs
16+
method: post
17+
queueName: {"Fn::GetAtt":[ "SQSQueue", "QueueName" ]}
18+
cors: true
19+
20+
resources:
21+
Resources:
22+
SQSQueue:
23+
Type: "AWS::SQS::Queue"

__tests__/integration/sqs/tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const fetch = require('node-fetch')
5+
const { deployService, removeService, getApiGatewayEndpoint } = require('./../../utils')
6+
7+
describe('SQS Proxy Integration Test', () => {
8+
let endpoint
9+
let stackName
10+
let stage
11+
const config = '__tests__/integration/sqs/service/serverless.yml'
12+
13+
beforeAll(async () => {
14+
stage = Math.random()
15+
.toString(32)
16+
.substring(2)
17+
stackName = 'sqs-proxy-' + stage
18+
deployService(stage, config)
19+
endpoint = await getApiGatewayEndpoint(stackName)
20+
})
21+
22+
afterAll(() => {
23+
removeService(stage, config)
24+
})
25+
26+
it('should get correct response from sqs proxy endpoint', async () => {
27+
const testEndpoint = `${endpoint}/sqs`
28+
29+
const response = await fetch(testEndpoint, {
30+
method: 'POST',
31+
headers: { 'Content-Type': 'application/json' },
32+
body: JSON.stringify({ message: 'testtest' })
33+
})
34+
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
35+
expect(response.status).to.be.equal(200)
36+
const body = await response.json()
37+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property(
38+
'MD5OfMessageAttributes'
39+
)
40+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MD5OfMessageBody')
41+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('MessageId')
42+
expect(body.SendMessageResponse.SendMessageResult).to.have.own.property('SequenceNumber')
43+
expect(body.SendMessageResponse.ResponseMetadata).to.have.own.property('RequestId')
44+
})
45+
})

__tests__/setup-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
jest.setTimeout(300000)

__tests__/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const _ = require('lodash')
4+
const execSync = require('child_process').execSync
5+
const aws = require('aws-sdk')
6+
const cloudformation = new aws.CloudFormation({ region: 'us-east-1' })
7+
8+
module.exports = {
9+
async getApiGatewayEndpoint(stackName) {
10+
const result = await cloudformation.describeStacks({ StackName: stackName }).promise()
11+
12+
const endpointOutput = _.find(result.Stacks[0].Outputs, { OutputKey: 'ServiceEndpoint' })
13+
.OutputValue
14+
return endpointOutput.match(/https:\/\/.+\.execute-api\..+\.amazonaws\.com.+/)[0]
15+
},
16+
17+
deployService(stage, config) {
18+
execSync(`npx serverless deploy --stage ${stage} --config ${config}`, {
19+
stdio: 'inherit'
20+
})
21+
},
22+
23+
removeService(stage, config) {
24+
execSync(`npx serverless remove --stage ${stage} --config ${config}`, { stdio: 'inherit' })
25+
}
26+
}

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ServerlessApigatewayServiceProxy {
106106
let message = ''
107107
let serviceProxyMessages = ''
108108

109-
const endpointInfo = this.gatheredData.info.endpoint
109+
const endpointInfo = this.gatheredData.info.endpoints
110110
message += `${chalk.yellow.underline('Serverless APIGateway Service Proxy OutPuts')}\n`
111111
message += `${chalk.yellow('endpoints:')}`
112112

0 commit comments

Comments
 (0)