-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
420 lines (404 loc) · 11.5 KB
/
index.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
const { container } = codeceptjs;
/**
* This helper allows to **mock requests while running tests in Puppeteer or WebDriver**.
* For instance, you can block calls to 3rd-party services like Google Analytics, CDNs.
* Another way of using is to emulate requests from server by passing prepared data.
*
* MockRequest helper works in these [modes](https://netflix.github.io/pollyjs/#/configuration?id=mode):
*
* * passthrough (default) - mock prefefined HTTP requests
* * record - record all requests into a file
* * replay - replay all recorded requests from a file
*
* Combining record/replay modes allows testing websites with large datasets.
*
* To use in passthrough mode set rules to mock requests and they will be automatically intercepted and replaced:
*
* ```js
* // default mode
* I.mockRequest('GET', '/api/users', '[]');
* ```
*
* In record-replay mode start mocking to make HTTP requests recorded/replayed, and stop when you don't need to block requests anymore:
*
* ```js
* // record or replay all XHR for /users page
* I.startMocking();
* I.amOnPage('/users');
* I.stopMocking();
* ```
*
* ### Installations
*
* ```
* npm i @codeceptjs/mock-request --save-dev
* ```
*
* Requires Puppeteer helper or WebDriver helper enabled
*
* ### Configuration
*
* #### With Puppeteer
*
* Enable helper in config file:
*
* ```js
* helpers: {
* Puppeteer: {
* // regular Puppeteer config here
* },
* MockRequestHelper: {
* require: '@codeceptjs/mock-request',
* }
* }
* ```
*
* [Polly config options](https://netflix.github.io/pollyjs/#/configuration?id=configuration) can be passed as well:
*
* ```js
* // sample options
* helpers: {
* MockRequestHelper: {
* require: '@codeceptjs/mock-request',
* mode: record,
* recordIfMissing: true,
* recordFailedRequests: false,
* expiresIn: null,
* persisterOptions: {
* keepUnusedRequests: false
* fs: {
* recordingsDir: './data/requests',
* },
* },
* }
* }
* ```
*
* ---
*
* **TROUBLESHOOTING**: Puppeteer does not mock requests in headless mode:
*
* Problem: request mocking does not work and in debug mode you see this in output:
*
* ```
* Access to fetch at {url} from origin {url} has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
* ```
* Solution: update Puppeteer config to include `--disable-web-security` arguments:
*
* ```js
* Puppeteer: {
* show: false,
* chrome: {
* args: [
* '--disable-web-security',
* ],
* },
* },
* ```
*
* ---
*
*
* #### With WebDriver
*
* This helper partially works with WebDriver. It can intercept and mock requests **only on already loaded page**.
*
* ```js
* helpers: {
* WebDriver: {
* // regular WebDriver config here
* },
* MockRequestHelper: {
* require: '@codeceptjs/mock-request',
* }
* }
* ```
*
* > Record/Replay mode is not tested in WebDriver but technically can work with [REST Persister](https://netflix.github.io/pollyjs/#/examples?id=rest-persister)
*
* ## Usage
*
* ### 👻 Mock Requests
*
* To intercept API requests and mock them use following API
*
* * [startMocking()](#startMocking) - to enable request interception
* * [mockRequest()](#mockRequest) - to define mock in a simple way
* * [mockServer()](#mockServer) - to use PollyJS server API to define complex mocks
* * [stopMocking()](#stopMocking) - to stop intercepting requests and disable mocks.
*
* Calling `mockRequest` or `mockServer` will start mocking, if it was not enabled yet.
*
* ```js
* I.startMocking(); // optionally
* I.mockRequest('/google-analytics/*path', 200);
* // return an empty successful response
* I.mockRequest('GET', '/api/users', 200);
* // mock users api
* I.mockServer(server => {
* server.get('https://server.com/api/users*').
* intercept((req, res) => { res.status(200).json(users);
* });
* });
* I.click('Get users);
* I.stopMocking();
* ```
*
* ### 📼 Record & Replay
*
* > At this moment works only with Puppeteer
*
* Record & Replay mode allows you to record all xhr & fetch requests and save them to file.
* On next runs those requests can be replayed.
* By default, it stores all passed requests, but this behavior can be customized with `I.mockServer`
*
* Set mode via enironment variable, `replay` mode by default:
*
*
* ```js
* // enable replay mode
* helpers: {
* Puppeteer: {
* // regular Puppeteer config here
* },
* MockRequest: {
* require: '@codeceptjs/mock-request',
* mode: process.env.MOCK_MODE || 'replay',
* },
* }
* ```
*
* Interactions between `I.startMocking()` and `I.stopMocking()` will be recorded and saved to `data/requests` directory.
*
* ```js
* I.startMocking() // record requests under 'Test' name
* I.startMocking('users') // record requests under 'users' name
* ```
*
* Use `I.mockServer()` to customize which requests should be recorded and under which name:
*
* ```js
* I.startMocking();
* I.mockServer((server) => {
* // mock request only from ap1.com and api2.com and
* // store recording into two different files
* server.any('https://api1.com/*').passthrough(false).recordingName('api1');
* server.any('https://api2.com/*').passthrough(false).recordingName('api2');
* });
* ```
*
* To stop request recording/replaying use `I.stopMocking()`.
*
* 🎥 To record HTTP interactions execute tests with MOCK_MODE environment variable set as "record":
*
* ```
* MOCK_MODE=record npx codeceptjs run --debug
* ```
*
* 📼 To replay them launch tests without environment variable:
*
* ```
* npx codeceptjs run --debug
* ```
*/
class MockRequest {
constructor(config) {
this.options = config;
this.connector = null;
}
async _after() {
try {
await this.stopMocking();
} catch (err) {
}
this.connector = null;
}
async _initializeConnector() {
const helpers = container.helpers();
if (helpers.Puppeteer) {
// recreate if page changed
if (this.connector
&& this.connector.page
&& this.connector.page === helpers.Puppeteer.page) {
return;
}
const PuppeteerConnector = require('./connector/Puppeteer');
this.connector = new PuppeteerConnector(helpers.Puppeteer, this.options);
}
if (!this.connector && helpers.WebDriver) {
const WebDriverConnector = require('./connector/WebDriver');
this.connector = new WebDriverConnector(helpers.WebDriver, this.options);
}
if (!this.connector) {
throw new Error('Puppeteer or WebDriver helper required to mock requests')
}
}
/**
* Starts mocking of http requests.
* In record mode starts recording of all requests.
* In replay mode blocks all requests and replaces them with saved.
*
* If inside one test you plan to record/replay requests in several places, provide [recording name](https://netflix.github.io/pollyjs/#/api?id=recordingname) as the parameter.
*
* ```js
* // start mocking requests for a test
* I.startMocking();
*
* // start mocking requests for main page
* I.startMocking('main-page');
* // do actions
* I.stopMocking();
* I.startMocking('login-page');
* ```
*
* To update [PollyJS configuration](https://netflix.github.io/pollyjs/#/configuration) use secon argument:
*
* ```js
* // change mode
* I.startMocking('comments', { mode: 'replay' });
*
* // override config
* I.startMocking('users-loaded', {
* recordFailedRequests: true
* })
* ```
*
* @param {*} title
*/
async startMocking(title = 'Test', config = {}) {
this._initializeConnector();
await this.connector.connect(title, config);
}
/**
* Use PollyJS [Server Routes API](https://netflix.github.io/pollyjs/#/server/overview) to declare mocks via callback function:
*
* ```js
* I.mockServer((server) => {
* // basic usage
* server.get('/api/v2/users').intercept((req, res) => {
* res.sendStatus(200).json({ users });
* });
*
* // passthrough requests to "/api/v2"
* server.get('/api/v1').passthrough();
* });
* ```
*
* In record replay mode you can define which routes should be recorded and where to store them:
*
* ```js
* I.startMocking('mock');
* I.mockServer((server) => {
*
* // record requests from cdn1.com and save them to data/recording/xml
* server.any('https://cdn1.com/*').passthrough(false).recordingName('xml');
*
* // record requests from cdn2.com and save them to data/recording/svg
* server.any('https://cdn2.com/*').passthrough(false).recordingName('svg');
*
* // record requests from /api and save them to data/recording/mock (default)
* server.any('/api/*').passthrough(false);
* });
* ```
*
*/
async mockServer(configFn) {
this._initializeConnector();
await this.connector.checkConnection();
await this.connector.mockServer(configFn);
}
/**
* Forces record mode for mocking.
* Requires mocking to be started.
*
* ```js
* I.recordMocking();
* ```
*
*/
async recordMocking() {
assertMockingStarted(this.connector);
await this.connector.record();
}
/**
* Forces replay mode for mocking.
* Requires mocking to be started.
*
* ```js
* I.replayMocking();
* ```
*
*/
async replayMocking() {
assertMockingStarted(this.connector);
await this.connector.replay();
}
/**
* Forces passthrough mode for mocking.
* Requires mocking to be started.
*
* ```js
* I.passthroughMocking();
* ```
*
*/
async passthroughMocking() {
assertMockingStarted(this.connector);
await this.connector.passthrough();
}
/**
* Waits for all requests handled by MockRequests to be resolved:
*
* ```js
* I.flushMocking();
* ```
*/
async flushMocking() {
assertMockingStarted(this.connector);
await this.connector.flush();
}
/**
* Stops mocking requests.
* Must be called to save recorded requests into faile.
*
* ```js
* I.stopMocking();
* ```
*/
async stopMocking() {
if (this.connector) this.connector.disconnect();
}
async after() {
if (this.connector) this.connector.disconnect();
}
/**
* Mock response status
*
* ```js
* I.mockRequest('GET', '/api/users', 200);
* I.mockRequest('ANY', '/secretsRoutes/*', 403);
* I.mockRequest('POST', '/secrets', { secrets: 'fakeSecrets' });
* I.mockRequest('GET', '/api/users/1', 404, 'User not found');
* ```
*
* Multiple requests
*
* ```js
* I.mockRequest('GET', ['/secrets', '/v2/secrets'], 403);
* ```
* @param {string} method request method. Can be `GET`, `POST`, `PUT`, etc or `ANY`.
* @param {string|string[]} oneOrMoreUrls url(s) to mock. Can be exact URL, a pattern, or an array of URLs.
* @param {number|string|object} dataOrStatusCode status code when number provided. A response body otherwise
* @param {string|object} additionalData response body when a status code is set by previous parameter.
*
*/
async mockRequest(method, oneOrMoreUrls, dataOrStatusCode, additionalData = null) {
this._initializeConnector();
await this.connector.checkConnection();
await this.connector.mockRequest(...arguments)
}
}
module.exports = MockRequest;
function assertMockingStarted(connector) {
if (!connector) throw new Error('Mocking should be started. Call startMocking() before');
}