Merge pull request #5161 from kalarrs/master

Updated aws provider to invoke .promise on methods that support it. Otherwise falls back to .send with a callback
This commit is contained in:
Philipp Muens 2019-01-24 13:09:03 +01:00 committed by GitHub
commit dfb36b8503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

@ -271,10 +271,10 @@ class AwsProvider {
// TODO: Add listeners, put Debug statments here...
// req.on('send', function (r) {console.log(r)});
return BbPromise.fromCallback(cb => {
const promise = req.promise ? req.promise() : BbPromise.fromCallback(cb => {
req.send(cb);
})
.catch(err => {
});
return promise.catch(err => {
let message = err.message;
if (err.message === 'Missing credentials in config') {
const errorMessage = [

View File

@ -98,7 +98,7 @@ describe('AwsProvider', () => {
'${opt:stage, \'prod\'}',
];
stages.forEach(stage => {
it(`should not throw an error before variable population
it(`should not throw an error before variable population
even if http event is present and stage is ${stage}`, () => {
const config = {
stage,
@ -286,6 +286,39 @@ describe('AwsProvider', () => {
});
});
it('should call correct aws method with a promise', () => {
// mocking API Gateway for testing
class FakeAPIGateway {
constructor(credentials) {
this.credentials = credentials;
}
getRestApis() {
return {
promise: () => BbPromise.resolve({ called: true }),
};
}
}
awsProvider.sdk = {
APIGateway: FakeAPIGateway,
};
awsProvider.serverless.service.environment = {
vars: {},
stages: {
dev: {
vars: {
profile: 'default',
},
regions: {},
},
},
};
return awsProvider.request('APIGateway', 'getRestApis', {}).then(data => {
expect(data.called).to.equal(true);
});
});
it('should request to the specified region if region in options set', () => {
// mocking S3 for testing
@ -316,7 +349,7 @@ describe('AwsProvider', () => {
},
},
};
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
expect(awsProvider.getCredentials().region).to.eql(options.region);
return awsProvider
.request('CloudFormation',
@ -326,7 +359,7 @@ describe('AwsProvider', () => {
.then(data => {
expect(data).to.eql({ region: 'ap-northeast-1' });
// Requesting different region should not affect region in credentials
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
expect(awsProvider.getCredentials().region).to.eql(options.region);
});
});
it('should retry if error code is 429', (done) => {