/
request-hooks-test.js
238 lines (194 loc) · 8.98 KB
/
request-hooks-test.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
const { RequestFilterRule } = require('testcafe-hammerhead');
const testRunTracker = require('../../lib/api/test-run-tracker');
const exportableLib = require('../../lib/api/exportable-lib');
const { RequestMock, RequestLogger, RequestHook } = exportableLib;
const { nanoid } = require('nanoid');
const { expect } = require('chai');
describe('RequestLogger', () => {
const createProxyRequestEventMock = (testRunId, requestId) => {
return {
isAjax: false,
_requestInfo: {
requestId: requestId,
userAgent: 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
url: 'http://example.com/',
method: 'get',
headers: {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'max-age=0',
},
body: Buffer.from('testParamerValue'),
sessionId: testRunId,
},
};
};
const createProxyResponseEventMock = (testRunId, requestId) => {
return {
requestId: requestId,
body: Buffer.from('<html><body><h1>Content</h1></body></html>'),
statusCode: 304,
sessionId: testRunId,
headers: {
'cache-control': 'max-age=604800',
'date': 'Wed, 17 Jan 2018 10:08:08 GMT',
'etag': '359670651',
},
};
};
const requestId = nanoid(9);
const testRunId = nanoid(9);
const requestEventMock = createProxyRequestEventMock(requestId, testRunId);
const responseEventMock = createProxyResponseEventMock(requestId, testRunId);
describe('Check collected data', () => {
const getLoggerRequests = logOptions => {
const logger = new RequestLogger('http://example.com', logOptions);
logger.onRequest(requestEventMock);
logger.onResponse(responseEventMock);
return logger.requests;
};
it('Default', () => {
const requests = getLoggerRequests();
expect(requests.length).eql(1);
expect(requests[0].request.timestamp).to.be.a('number');
expect(requests[0].request.headers).to.be.undefined;
expect(requests[0].request.body).to.be.undefined;
expect(requests[0].response.timestamp).to.be.a('number');
expect(requests[0].response.headers).to.be.undefined;
expect(requests[0].response.body).to.be.undefined;
});
it('Headers and body', () => {
const requests = getLoggerRequests({
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
logResponseBody: true,
});
expect(requests.length).eql(1);
expect(requests[0].request.headers).eql(requestEventMock._requestInfo.headers);
expect(requests[0].request.body).eql(requestEventMock._requestInfo.body);
expect(requests[0].request.body).instanceOf(Buffer);
expect(requests[0].response.headers).eql(responseEventMock.headers);
expect(requests[0].response.body).eql(responseEventMock.body);
expect(requests[0].response.body).instanceof(Buffer);
});
it('Stringify body', () => {
const requests = getLoggerRequests({
logRequestBody: true,
stringifyRequestBody: true,
logResponseBody: true,
stringifyResponseBody: true,
});
expect(requests.length).eql(1);
expect(requests[0].request.body).eql(requestEventMock._requestInfo.body.toString());
expect(requests[0].response.body).eql(responseEventMock.body.toString());
});
});
it('.clear method', () => {
const logger = new RequestLogger('http://example.com');
const requestId1 = nanoid(9);
const requestId2 = nanoid(9);
const testRunId1 = nanoid(9);
const testRunId2 = nanoid(9);
const requestEventMock1 = createProxyRequestEventMock(testRunId1, requestId1);
const responseEventMock1 = createProxyResponseEventMock(testRunId1, requestId1);
const requestEventMock2 = createProxyRequestEventMock(testRunId2, requestId2);
const responseEventMock2 = createProxyResponseEventMock(testRunId2, requestId2);
logger.onRequest(requestEventMock1);
logger.onResponse(responseEventMock1);
logger.onRequest(requestEventMock2);
logger.onResponse(responseEventMock2);
const storedResolveContextTestRunFn = testRunTracker.resolveContextTestRun;
testRunTracker.resolveContextTestRun = () => {
return {
id: testRunId1,
};
};
// With test context
logger.clear();
expect(logger.requests.length).eql(0);
const internalRequestIds = Object.keys(logger._internalRequests);
const firstInternalRequest = logger._internalRequests[internalRequestIds[0]];
expect(internalRequestIds.length).eql(1);
expect(firstInternalRequest.testRunId).eql(testRunId2);
expect(firstInternalRequest.id).eql(requestId2);
testRunTracker.resolveContextTestRun = () => void 0;
// Without test context
logger.clear();
expect(logger.requests.length).eql(0);
testRunTracker.resolveContextTestRun = storedResolveContextTestRunFn;
});
it('.requests property without test context', () => {
const logger = new RequestLogger();
expect(logger.requests.length).eql(0);
logger.onRequest(requestEventMock);
logger.onResponse(responseEventMock);
expect(logger.requests.length).eql(1);
});
it('.contains and .count methods should affect only completed requests', () => {
const logger = new RequestLogger();
const requestEventMock2 = createProxyRequestEventMock(nanoid(9), nanoid(9));
logger.onRequest(requestEventMock);
logger.onResponse(responseEventMock);
logger.onRequest(requestEventMock2);
return Promise.all([
logger.count(r => r.request),
logger.count(r => r.response.statusCode === 304),
logger.contains(r => r.request.id === requestEventMock2._requestInfo.requestId),
])
.then(data => {
expect(data[0]).eql(1);
expect(data[1]).eql(1);
expect(data[2]).eql(false);
});
});
it('Should not raise an error if during a long running request the ".clear" method will be called (GH-2688)', () => {
const logger = new RequestLogger();
logger.onRequest(requestEventMock);
logger.clear();
logger.onResponse(responseEventMock);
expect(logger.requests.length).eql(0);
});
it('Should not fail when logger logs and stringifies a response body and it equals null (GH-2718)', () => {
const logger = new RequestLogger('http://example.com/', { logResponseBody: true, stringifyResponseBody: true });
const clonedResponseEventMock = Object.assign({}, responseEventMock, { body: null });
logger.onRequest(requestEventMock);
logger.onResponse(clonedResponseEventMock);
expect(logger.requests[0].response.body).eql(null);
});
});
describe('RequestMock', () => {
describe('Throwing errors on construction', () => {
it('Without configure', () => {
expect(() => {
RequestMock();
}).to.not.throw;
});
it('With configure', () => {
expect(() => {
RequestMock()
.onRequestTo('http://example.com')
.respond('<html></html>');
}).to.not.throw;
});
});
it('Should handle only specified requests (GH-2336)', () => {
const mock = RequestMock()
.onRequestTo('http://example.com')
.respond();
expect(mock._requestFilterRules.length).eql(1);
expect(mock._requestFilterRules[0].options.url).eql('http://example.com');
});
});
describe('RequestHook', () => {
it('Should handle any requests by default', () => {
const hook = new RequestHook();
expect(hook._requestFilterRules.length).eql(1);
expect(hook._requestFilterRules[0].options).eql(RequestFilterRule.ANY.options);
});
it('Should have an identifier', () => {
const hook = new RequestHook();
expect(hook.id).not.undefined;
});
});