forked from mock-server/mockserver-client-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyClient.js
More file actions
227 lines (220 loc) · 9.03 KB
/
proxyClient.js
File metadata and controls
227 lines (220 loc) · 9.03 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* mockserver
* http://mock-server.com
*
* Copyright (c) 2014 James Bloom
* Licensed under the Apache License, Version 2.0
*/
var proxyClient;
(function () {
"use strict";
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest : function (host, port, path, jsonBody, resolveCallback) {
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || ""));
var url = 'http://' + host + ':' + port + path;
return {
then: function (sucess, error) {
try {
var xmlhttp = new XMLHttpRequest();
xmlhttp.addEventListener("load", (function (sucess, error) {
return function () {
sucess && sucess({
statusCode: this.status,
description: this.status + " " + this.statusText,
body: this.responseText
});
};
})(sucess, error));
xmlhttp.open('PUT', url);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.send(body);
} catch (e) {
error && error(e);
}
}
};
});
/**
* Start the client communicating to a MockServer proxy at the specified host and port
* for example:
*
* var client = proxyClient("localhost", 1080);
*
* @param host the host for the proxy to communicate with
* @param port the port for the proxy to communicate with
*/
proxyClient = function (host, port) {
var createResponseMatcher = function (path) {
return {
method: "",
path: path,
body: "",
headers: [],
cookies: [],
queryStringParameters: []
};
};
/**
* Verify a request has been sent for example:
*
* expect(client.verify({
* 'httpRequest': {
* 'method': 'POST',
* 'path': '/somePath'
* }
* })).toBeTruthy();
*
* @param request the http request that must be matched for this verification to pass
* @param count the number of times this request must be matched
* @param exact true if the count is matched as "equal to" or false if the count is matched as "greater than or equal to"
*/
var verify = function (request, count, exact) {
if (count === undefined) {
count = 1;
}
return {
then: function (sucess, error) {
return makeRequest(host, port, "/verify", {
"httpRequest": request,
"times": {
"count": count,
"exact": exact
}
}).then(function (result) {
if (result.statusCode !== 202) {
error && error(result.body);
} else {
sucess && sucess();
}
});
}
};
};
/**
* Verify a sequence of requests has been sent for example:
*
* client.verifySequence(
* {
* 'method': 'POST',
* 'path': '/first_request'
* },
* {
* 'method': 'POST',
* 'path': '/second_request'
* },
* {
* 'method': 'POST',
* 'path': '/third_request'
* }
* );
*
* @param arguments the list of http requests that must be matched for this verification to pass
*/
var verifySequence = function () {
var requestSequence = [];
for (var i = 0; i < arguments.length; i++) {
requestSequence.push(arguments[i]);
}
return {
then: function (sucess, error) {
return makeRequest(host, port, "/verifySequence", {
"httpRequests": requestSequence
}).then(function (result) {
if (result.statusCode !== 202) {
error && error(result.body);
} else {
sucess && sucess();
}
});
}
};
};
/**
* Reset the proxy by clearing all recorded requests
*/
var reset = function () {
return makeRequest(host, port, "/reset");
};
/**
* Clear all recorded requests that match the specified path
*
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to
* decide which recorded requests to cleared, however if an object is
* passed in the value will be treated as a full request matcher object
*/
var clear = function (pathOrRequestMatcher) {
if (typeof pathOrRequestMatcher === "string") {
return makeRequest(host, port, "/clear", createResponseMatcher(pathOrRequestMatcher));
} else if (pathOrRequestMatcher) {
return makeRequest(host, port, "/clear", pathOrRequestMatcher);
} else {
return makeRequest(host, port, "/clear", createResponseMatcher(".*"));
}
};
/**
* Retrieve the recorded requests that match the parameter, as follows:
* - use a string value to match on path,
* - use a request matcher object to match on a full request,
* - or use null to retrieve all requests
*
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however
* if an object is passed in the value will be treated as a full request
* matcher object, if null is passed in it will be treated as match all
*/
var retrieveRequests = function (pathOrRequestMatcher) {
return {
then: function (sucess, error) {
if (typeof pathOrRequestMatcher === "string") {
makeRequest(host, port, "/retrieve", createResponseMatcher(pathOrRequestMatcher))
.then(function (result) {
sucess(result.body && JSON.parse(result.body));
});
} else if (pathOrRequestMatcher) {
makeRequest(host, port, "/retrieve", pathOrRequestMatcher)
.then(function (result) {
sucess(result.body && JSON.parse(result.body));
});
} else {
makeRequest(host, port, "/retrieve", createResponseMatcher(".*"))
.then(function (result) {
sucess(result.body && JSON.parse(result.body));
});
}
}
};
};
/**
* Retrieve the setup expectations that match the parameter,
* the expectation is retrieved by matching the parameter
* on the expectations own request matcher, as follows:
* - use a string value to match on path,
* - use a request matcher object to match on a full request,
* - or use null to retrieve all requests
*
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to
* decide which recorded requests to cleared, however if an object is
* passed in the value will be treated as a full request matcher object
*/
var dumpToLogs = function (pathOrRequestMatcher) {
if (typeof pathOrRequestMatcher === "string") {
return makeRequest(host, port, "/dumpToLog", createResponseMatcher(pathOrRequestMatcher));
} else if (pathOrRequestMatcher) {
return makeRequest(host, port, "/dumpToLog", pathOrRequestMatcher);
} else {
return makeRequest(host, port, "/dumpToLog", createResponseMatcher(".*"));
}
};
return {
verify: verify,
verifySequence: verifySequence,
reset: reset,
clear: clear,
retrieveRequests: retrieveRequests,
dumpToLogs: dumpToLogs
};
};
if (typeof module !== 'undefined') {
module.exports = {
proxyClient: proxyClient
};
}
})();