-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsServiceMock.js
137 lines (114 loc) · 4.04 KB
/
awsServiceMock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* AWS Service Mock Module
*/
const AWS = require('aws-sdk');
const sinon = require('sinon');
let cachedStubs = {}; // eslint-disable-line prefer-const, no-unused-vars
/**
* Creates a key to use for caching the AWS stubs
* @param {string} service - Service name
* @param {string} method - Method name
* @returns {string} key - Unique key from service and method names
*/
const getKey = function (service, method) { // eslint-disable-line func-names
const key = `${service.toLowerCase()}_${method.toLowerCase()}`;
return key;
};
/**
* Creates a key to use for caching the AWS stubs
* @param {string} service - Service name
* @param {string} method - Method name
* @returns {string} key - Unique key from service and method names
*/
const processAwsRequest = function (awsMockCallback) { // eslint-disable-line func-names
const requestKey = getKey(this.service.serviceIdentifier, this.operation);
if (!cachedStubs[requestKey]) {
throw new Error(`No stub response for ${this.service.serviceIdentifier}.${this.operation}`);
}
let response = new AWS.Response(); // eslint-disable-line prefer-const
response.request = this.httpRequest;
const callback = function (err, data) { // eslint-disable-line func-names
response.data = data;
response.error = err;
response.retryCount = 0;
response.redirectCount = 0;
awsMockCallback.call(response, response.error, response.data);
};
const possibleData = cachedStubs[requestKey](this.params, callback);
if (typeof possibleData !== 'undefined') {
callback(null, possibleData);
}
};
/**
* Used to initialize this module by stubbing the AWS Request send method
*/
const stubAwsRequestSend = function () { // eslint-disable-line func-names
sinon.stub(AWS.Request.prototype, 'send').callsFake(processAwsRequest);
};
/**
* Create an AWS mock for a service, method and callback
* @param {string} service - AWS service name
* @param {string} method - AWS service method name
* @param {function} func - Callback function for mock
*/
const createAwsMock = function (service, method, func) { // eslint-disable-line func-names
const stubKey = getKey(service, method);
// Initialize the stub with a temporary empty fuction
cachedStubs[stubKey] = () => {};
sinon.stub(cachedStubs, stubKey).callsFake(func);
// Override the default stub restore behavior
cachedStubs[stubKey].restore = function () { // eslint-disable-line func-names
cachedStubs[stubKey] = null;
};
};
/**
* Get an AWS mock for a service and method
* @param {string} service - AWS service name
* @param {string} method - AWS service method name
* @return {Object} stub - Sinon stub for testing
*/
const getAwsMock = function (service, method) { // eslint-disable-line func-names
const stubKey = getKey(service, method);
const stub = cachedStubs[stubKey];
return stub;
};
/**
* Remove an AWS stub from the cachedStubs
* @param {string} service - AWS service name
* @param {string} method - AWS service method name
*/
const deleteAwsMock = function (service, method) { // eslint-disable-line func-names
const stubKey = getKey(service, method);
if (cachedStubs[stubKey]) {
cachedStubs[stubKey].restore();
delete cachedStubs[stubKey];
}
};
/**
* Replace an AWS mock for a service, method and with a new callback
* @param {string} service - AWS service name
* @param {string} method - AWS service method name
* @param {function} func - Callback function for mock
*/
const updateAwsMock = function (service, method, func) { // eslint-disable-line func-names
// Remove the old mock if it exists
deleteAwsMock(service, method, func);
// Create the new AwsMock
createAwsMock(service, method, func);
};
/**
* Used restore the AWS Request send method
*/
const restoreAwsRequestSend = function () { // eslint-disable-line func-names
AWS.Request.prototype.send.restore();
};
// Initialize the library to stub the AWS send function for mocking
stubAwsRequestSend();
module.exports = {
stubAwsRequestSend,
restoreAwsRequestSend,
createAwsMock,
getAwsMock,
deleteAwsMock,
updateAwsMock,
};