-
Notifications
You must be signed in to change notification settings - Fork 164
/
soap-stub.js
203 lines (188 loc) · 5.04 KB
/
soap-stub.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
// Node module: strong-soap
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var g = require('./lib/globalize');
var _ = require('lodash');
var aliasedClientStubs = {};
var clientStubs = {};
/**
* This module stubs the soap module to allow for offline
* testing and stubbed clients. All clients' methods are stubbed with sinon and have
* additional functionality:
*
* <ul>
* <li>.respondWithError() - Responds with the mocked client's sample error response.</li>
* <li>.respondWithSuccess() - Responds with the mocked client's sample success response.</li>
* </ul>
*
* Register a client by calling `.registerClient(urlToWsdl, clientStub)`. For an
* example client stub, see ./soap-stub-client-example.js.
*
* @property {Boolean} errOnCreateClient returns an error to the createClient method when set to true.
*/
module.exports = {
createClient: createClient,
createErroringStub: createErroringStub,
createErroringStubAsync: createErroringStubAsync,
createRespondingStub: createRespondingStub,
createRespondingStubAsync: createRespondingStubAsync,
errOnCreateClient: false,
getStub: getStub,
registerClient: registerClient,
reset: reset
};
/**
* Return a stubbed client based on the value of wsdlUrl.
*
* @throws if wsdlUrl is unknown.
*
* @param {String} wsdlUrl
* @param {Object} options
* @param {Function} cb
* @return {Object}
*/
function createClient(wsdlUrl, options, cb) {
if (!cb) {
cb = options;
options = {};
}
if (this.errOnCreateClient) {
return setTimeout(cb.bind(null, new Error(g.f('forced error on {{createClient}}'))));
}
var client = getStub(wsdlUrl);
if (client) {
resetStubbedMethods(client);
setTimeout(cb.bind(null, null, client));
} else {
setTimeout(cb.bind(null, new Error(g.f('no client stubbed for %s', wsdlUrl))));
}
}
/**
* Returns a method that calls all callbacks given to the method it is attached
* to with the given error.
*
* <pre>
* myClientStub.someMethod.errorOnCall = createErroringStub(error);
*
* // elsewhere
*
* myClientStub.someMethod.errorOnCall();
* </pre>
*
* @param {?} object anything
* @return {Function}
*/
function createErroringStub(err) {
return function() {
this.args.forEach(function(argSet) {
setTimeout(argSet[1].bind(null, err));
});
this.yields(err);
};
}
/**
* Returns a method that rejects all promises given to the method it is attached
* to with the given error.
*
* <pre>
* myClientStub.someMethod.errorOnCall = createErroringStubAsync(error);
*
* // elsewhere
*
* myClientStub.someMethod.errorOnCall();
* </pre>
*
* @param {?} object anything
* @return {Function}
*/
function createErroringStubAsync(err) {
return function() {
this.args.forEach(function(argSet) {
setTimeout(argSet[1].bind(null, err));
});
this.rejects(err);
};
}
/**
* Returns a method that calls all callbacks given to the method it is attached
* to with the given response.
*
* <pre>
* myClientStub.someMethod.respondWithError = createRespondingStub(errorResponse);
*
* // elsewhere
*
* myClientStub.someMethod.respondWithError();
* </pre>
*
* @param {?} object anything
* @return {Function}
*/
function createRespondingStub(object) {
return function() {
this.args.forEach(function(argSet) {
setTimeout(argSet[1].bind(null, null, object));
});
this.yields(null, object);
};
}
/**
* Returns a method that resolves all promises given to the method it is attached
* to with the given response.
*
* <pre>
* myClientStub.someMethod.respondWithError = createRespondingStubAsync(errorResponse);
*
* // elsewhere
*
* myClientStub.someMethod.respondWithError();
* </pre>
*
* @param {?} object anything
* @return {Function}
*/
function createRespondingStubAsync(object) {
return function() {
this.args.forEach(function(argSet) {
setTimeout(argSet[1].bind(null, null, object));
});
this.resolves(object);
};
}
/**
* Registers a stubbed client with soap-stub. urlToWsdl is the path you will use
* in your app.
*
* @param {String} alias A simple name to refer to the clientStub as.
* @param {String} urlToWsdl May be file system URL or http URL.
* @param {Object} clientStub A client with stubbed methods.
*/
function registerClient(alias, urlToWsdl, clientStub) {
aliasedClientStubs[alias] = clientStub;
clientStubs[urlToWsdl] = clientStub;
}
/**
* Resets state associated with clientStubs.
*/
function reset() {
_.forEach(clientStubs, resetStubbedMethods);
this.errOnCreateClient = false;
}
/**
* Returns a previously registered client stub.
*
* @param {String} aliasOrWsdlUrl
* @return {Object} clientStub
*/
function getStub(aliasOrWsdlUrl) {
return aliasedClientStubs[aliasOrWsdlUrl] || clientStubs[aliasOrWsdlUrl];
}
function resetStubbedMethods(client) {
Object.keys(client).forEach(function(method) {
method = client[method];
if (typeof method === 'function' && typeof method.reset === 'function') {
method.reset();
}
});
}