-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathauto-stub.ts
200 lines (185 loc) · 6.14 KB
/
auto-stub.ts
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
import { CyHttpMessages } from 'cypress/types/net-stubbing';
import { getTestCaseInfo } from './test-info';
/**
* Whenever a request is started, update `pendingAPICount` accordingly.
*/
function increaseAPICountOnRequestStart() {
cy._data.api.pendingAPICount += 1;
}
/**
* Whenever a request is finished, update `pendingAPICount` accordingly.
*/
function decreaseAPICountOnRequestFinish() {
const { isAPIRecording } = cy._config;
/**
* Sometimes there are some time windows between API requests, e.g. Request1 finishes,
* but Request2 starts after 100ms, in this case, `cy.waitUntilAllAPIFinished()` would
* not work correctly, so when we decrease the counter, we need to have a delay here.
*/
const delayTime = isAPIRecording ? 500 : 50;
if (cy._data.api.pendingAPICount === 1) {
setTimeout(() => {
cy._data.api.pendingAPICount -= 1;
}, delayTime);
} else {
cy._data.api.pendingAPICount -= 1;
}
}
/**
* The interception handler for stubbed requests, which will hit the real server.
*/
function routeHandlerWithRealResponse(
request: CyHttpMessages.IncomingHttpRequest
) {
const { isAPIRecording } = cy._config;
increaseAPICountOnRequestStart();
request.reply((response) => {
if (isAPIRecording) {
recordAPIResponse(request, response);
response.send();
decreaseAPICountOnRequestFinish();
}
});
}
/**
* The interception handler for requests which won't hit the real server, instead it will
* return a mocked response.
*/
function routeHandlerWithMockResponse(
request: CyHttpMessages.IncomingHttpRequest,
mockedResponse: any
) {
increaseAPICountOnRequestStart();
request.reply(mockedResponse);
// request.reply() won't wait the response to complete, so put a manual wait
setTimeout(() => {
decreaseAPICountOnRequestFinish();
}, 100);
}
/**
* Setup cypress interception to make sure API recording, API wait working.
*/
export function setupCypressInterception() {
const { fixtureName } = getTestCaseInfo();
cy._data.api = {
records: [],
pendingAPICount: 0,
};
const { isAPIRecording, isAPISnapshotUsed } = cy._config;
cy.log(`API Recording: ${isAPIRecording ? 'ON' : 'OFF'}`);
if (isAPISnapshotUsed) {
cy.log(`Use API snapshot: ${fixtureName}`);
}
/**
* `startMonitorAPI()` is always required regardless of snapshot or real API:
* * If real API is used, `cy.intercept` is running against regex to monitor all
* API calls inside test, so that `cy.waitUntilAllAPIFinished()` can work as expected.
* * If snapshot is used, `cy.intercept` is running against the APIs defined in the
* snapshot (logic in`loadAPIFixture()`), which means unmatched APIs (not included
* in fixture) won't be captured by that, that's why we still need to fallback `cy.
* intercept` here to run against regex in addition.
*/
startMonitorAPI();
if (isAPISnapshotUsed) {
loadAPIFixture();
}
}
/**
* Write recorded API data to fixture snapshot file.
*/
export function writeRecordedAPIToFixture() {
const { currentTest, fixturePath, currentTestFullTitle } = getTestCaseInfo();
if (currentTest.state === 'passed') {
// if fixture file exists, only update the data related to this test case
cy.task('isFileExisted', fixturePath, { log: false }).then(
(isFixtureExisted) => {
const recordedData = {
timestamp: new Date().toJSON(),
records: cy._data.api.records,
};
if (isFixtureExisted) {
cy.readFile(fixturePath, { log: false }).then(
(snapshotFixture: APISnapshotFixture) => {
snapshotFixture[currentTestFullTitle] = recordedData;
cy.writeFile(fixturePath, snapshotFixture, {
log: false,
});
}
);
} else {
cy.writeFile(
fixturePath,
{
[currentTestFullTitle]: recordedData,
},
{ log: false }
);
}
cy.log('API recorded', cy._data.api.records);
}
);
}
}
function recordAPIResponse(
request: CyHttpMessages.IncomingHttpRequest,
response: CyHttpMessages.IncomingHttpResponse
) {
/**
* save URL without the host info, because API host might be different between
* Record and Replay session
*/
let url = '';
let matchHostIndex: number = -1;
const apiHosts = Cypress.env('apiHosts').split(',');
for (let i = 0; i < apiHosts.length; i += 1) {
const host = apiHosts[i].trim();
if (request.url.includes(host)) {
url = request.url.replace(host, '');
matchHostIndex = i;
break;
}
}
// the host of API is not the one we are interested
if (matchHostIndex === -1) {
return;
}
// save API request/response into an array so we can write these info to fixture
cy._data.api.records.push({
url,
method: request.method,
request: {
body: request.body,
},
response: {
body: response.body,
},
matchHostIndex,
});
}
function startMonitorAPI() {
const stubAPIPatterns = Cypress.env('stubAPIPatterns').split(',');
stubAPIPatterns.forEach((pattern: string) => {
const apiRegex = new RegExp(pattern.trim());
// let Cypress stub all API requests which match the pattern defined in cypress.json
cy.intercept('GET', apiRegex, routeHandlerWithRealResponse);
cy.intercept('POST', apiRegex, routeHandlerWithRealResponse);
cy.intercept('PUT', apiRegex, routeHandlerWithRealResponse);
cy.intercept('DELETE', apiRegex, routeHandlerWithRealResponse);
});
}
function loadAPIFixture() {
const { fixtureName, currentTestFullTitle } = getTestCaseInfo();
const apiHosts = Cypress.env('apiHosts').split(',');
return cy.fixture(fixtureName).then((snapshotFixture: APISnapshotFixture) => {
// in snapshot, we use `currentTestFullTitle` from .spec.ts file as the Key
const snapshot = snapshotFixture[currentTestFullTitle];
snapshot.records.forEach((apiRecord) => {
const fullUrl = `${apiHosts[apiRecord.matchHostIndex].trim()}${
apiRecord.url
}`;
cy.intercept(apiRecord.method as any, fullUrl, (request) => {
routeHandlerWithMockResponse(request, apiRecord.response.body || {});
});
});
});
}