/
mock.js
171 lines (152 loc) · 4.52 KB
/
mock.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
/**
* @file HTTP mocking
* @since 0.2.2
*/
/*#ifndef(UMD)*/
"use strict";
/*global _GPF_HTTP_METHODS*/ // HTTP Methods
/*global _gpfHost*/ // Host type
/*global _gpfHttpSetRequestImplIf*/ // Set the http request implementation if the host matches
/*#endif*/
/**
* Mocked response callback
*
* @callback gpf.typedef.mockedResponseCallback
*
* @param {gpf.typedef.httpRequestSettings} HTTP request settings
* @param {...String} captured Regular expression capturing group values
* @return {Promise<gpf.typedef.httpRequestResponse>|undefined}
* @since 0.2.2
*/
/**
* Definition of a mocked request
*
* @typedef gpf.typedef.mockedRequest
* @property {gpf.http.methods} method HTTP method to mock
* @property {RegExp} url Regular expression matching the URL to mock
* @property {gpf.typedef.mockedResponseCallback} response Function that generates response when mocking applies
* @since 0.2.2
*/
/**
* Mocked request identifier
*
* @typedef gpf.typedef.mockedRequestID
* @since 0.2.2
*/
/**
* Maps request method to an array of mocked requests
*
* @type {Object}
* @since 0.2.2
*/
var _gpfHttpMockedRequests = {};
/**
* Match the provided request with the mocked one
*
* @param {gpf.typedef.mockedRequest} mockedRequest Mocked request to match
* @param {gpf.typedef.httpRequestSettings} request Request to match
* @return {Promise<gpf.typedef.httpRequestResponse>|undefined} undefined if not matching
* @since 0.2.2
*/
function _gpfHttMockMatchRequest (mockedRequest, request) {
var url = mockedRequest.url,
match;
url.lastIndex = 0;
match = url.exec(request.url);
if (match) {
return mockedRequest.response.apply(mockedRequest, [request].concat([].slice.call(match, 1)));
}
}
/**
* Match the provided request to the list of mocked ones
*
* @param {gpf.typedef.mockedRequest[]} mockedRequests List of mocked requests for the given method
* @param {gpf.typedef.httpRequestSettings} request Request to match
* @return {Promise<gpf.typedef.httpRequestResponse>|undefined} undefined if no mocked request matches
* @since 0.2.2
*/
function _gpfHttMockMatch (mockedRequests, request) {
var result;
mockedRequests.every(function (mockedRequest) {
result = _gpfHttMockMatchRequest(mockedRequest, request);
return result === undefined;
});
return result;
}
/**
* Check if the provided request match any of the mocked one
*
* @param {gpf.typedef.httpRequestSettings} request Request to check
* @return {Promise<gpf.typedef.httpRequestResponse>|undefined} undefined if no mocked request matches
* @since 0.2.2
*/
function _gpfHttpMockCheck (request) {
return _gpfHttMockMatch(_gpfHttpMockedRequests[request.method], request);
}
var _gpfHttpMockLastId = 0;
/**
* Add a mocked request
*
* @param {gpf.typedef.mockedRequest} definition Mocked request definition
* @return {gpf.typedef.mockedRequestID} Mocked request identifier, to be used with {@link gpf.http.mock.remove}
* @see gpf.http
* @since 0.2.2
*/
function _gpfHttpMockAdd (definition) {
var method = definition.method,
id;
++_gpfHttpMockLastId;
id = method + "." + _gpfHttpMockLastId;
_gpfHttpMockedRequests[method].unshift(Object.assign({
id: id
}, definition));
return id;
}
/**
* Removes a mocked request
*
* @param {gpf.typedef.mockedRequestID} id Mocked request identifier returned by {@link gpf.http.mock}
* @since 0.2.2
*/
function _gpfHttpMockRemove (id) {
var method = id.split(".")[0];
_gpfHttpMockedRequests[method] = _gpfHttpMockedRequests[method].filter(function (mockedRequest) {
return mockedRequest.id !== id;
});
}
/**
* Clears all mocked requests
* @since 0.2.2
*/
function _gpfHttpMockReset () {
Object.keys(_GPF_HTTP_METHODS).forEach(function (method) {
_gpfHttpMockedRequests[method] = [];
});
}
_gpfHttpMockReset();
/**
* @gpf:sameas _gpfHttpMockAdd
* @since 0.2.2
*/
gpf.http.mock = _gpfHttpMockAdd;
/**
* @gpf:sameas _gpfHttpMockRemove
* @since 0.2.2
*/
gpf.http.mock.remove = _gpfHttpMockRemove;
/**
* @gpf:sameas _gpfHttpMockReset
* @since 0.2.2
*/
gpf.http.mock.reset = _gpfHttpMockReset;
// Hook the mocking algorithm on top of host specific implementation
var _gpfHttpMockRequestImpl;
function _gpfHttpMockImpl (request, resolve, reject) {
var mockedResult = _gpfHttpMockCheck(request);
if (undefined === mockedResult) {
_gpfHttpMockRequestImpl(request, resolve, reject);
} else {
resolve(mockedResult);
}
}
_gpfHttpMockRequestImpl = _gpfHttpSetRequestImplIf(_gpfHost, _gpfHttpMockImpl);