-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathprerequests.spec.ts
278 lines (212 loc) · 11 KB
/
prerequests.spec.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
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
import { CorrelationInformation, PreRequests } from '@packages/proxy/lib/http/util/prerequests'
import { BrowserPreRequest, CypressIncomingRequest } from '@packages/proxy'
import { expect } from 'chai'
import sinon from 'sinon'
import { performance } from 'perf_hooks'
import { ProtocolManagerShape } from '@packages/types'
describe('http/util/prerequests', () => {
let preRequests: PreRequests
let protocolManager: ProtocolManagerShape
function expectPendingCounts (pendingRequests: number, pendingPreRequests: number, pendingWithoutPreRequests = 0) {
expect(preRequests.pendingRequests.length).to.eq(pendingRequests, 'wrong number of pending requests')
expect(preRequests.pendingPreRequests.length).to.eq(pendingPreRequests, 'wrong number of pending prerequests')
expect(preRequests.pendingUrlsWithoutPreRequests.length).to.eq(pendingWithoutPreRequests, 'wrong number of pending without prerequests')
}
beforeEach(() => {
preRequests = new PreRequests(10)
protocolManager = {
responseStreamTimedOut: sinon.stub(),
} as any
preRequests.setProtocolManager(protocolManager)
})
afterEach(() => {
clearInterval(preRequests.sweepInterval)
})
it('synchronously matches a pre-request that existed at the time of the request', () => {
// should match in order
preRequests.addPending({
requestId: '1234',
url: 'foo',
method: 'WRONGMETHOD',
headers: {},
resourceType: 'xhr',
originalResourceType: undefined,
cdpRequestWillBeSentTimestamp: 1,
cdpRequestWillBeSentReceivedTimestamp: 2,
})
const secondPreRequest: BrowserPreRequest = {
requestId: '1234',
url: 'foo',
method: 'GET',
headers: {},
resourceType: 'xhr',
originalResourceType: undefined,
cdpRequestWillBeSentTimestamp: 1,
cdpRequestWillBeSentReceivedTimestamp: performance.now() + performance.timeOrigin + 10000,
}
preRequests.addPending(secondPreRequest)
preRequests.addPending({
requestId: '1234',
url: 'foo',
method: 'GET',
headers: {},
resourceType: 'xhr',
originalResourceType: undefined,
cdpRequestWillBeSentTimestamp: 1,
cdpRequestWillBeSentReceivedTimestamp: 2,
})
expectPendingCounts(0, 3)
const cb = sinon.stub()
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
const { args } = cb.getCall(0)
const browserPreRequest = args[0].browserPreRequest
const noPreRequestExpected = args[0].noPreRequestExpected
expect(browserPreRequest.requestId).to.eq(secondPreRequest.requestId)
expect(browserPreRequest.url).to.eq(secondPreRequest.url)
expect(browserPreRequest.method).to.eq(secondPreRequest.method)
expect(browserPreRequest.headers).to.deep.eq(secondPreRequest.headers)
expect(browserPreRequest.resourceType).to.eq(secondPreRequest.resourceType)
expect(browserPreRequest.originalResourceType).to.eq(secondPreRequest.originalResourceType)
expect(browserPreRequest.cdpRequestWillBeSentTimestamp).to.eq(secondPreRequest.cdpRequestWillBeSentTimestamp)
expect(browserPreRequest.cdpRequestWillBeSentReceivedTimestamp).to.eq(secondPreRequest.cdpRequestWillBeSentReceivedTimestamp)
expect(browserPreRequest.proxyRequestReceivedTimestamp).to.be.a('number')
expect(browserPreRequest.cdpLagDuration).to.eq(secondPreRequest.cdpRequestWillBeSentReceivedTimestamp - secondPreRequest.cdpRequestWillBeSentTimestamp)
expect(browserPreRequest.proxyRequestCorrelationDuration).to.eq(secondPreRequest.cdpRequestWillBeSentReceivedTimestamp - browserPreRequest.proxyRequestReceivedTimestamp)
expect(noPreRequestExpected).to.be.false
expectPendingCounts(0, 2)
})
it('synchronously matches a request without a pre-request that existed at the time of the request', () => {
// should match in order
preRequests.addPendingUrlWithoutPreRequest('foo')
preRequests.addPendingUrlWithoutPreRequest('foo')
preRequests.addPendingUrlWithoutPreRequest('foo')
expectPendingCounts(0, 0, 3)
const cb = sinon.stub()
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
const { args } = cb.getCall(0)
const arg = args[0]
expect(arg.preRequest).to.be.undefined
expect(arg.noPreRequestExpected).to.be.true
expectPendingCounts(0, 0, 2)
})
it('synchronously matches a pre-request added after the request', (done) => {
const cb = ({ browserPreRequest, noPreRequestExpected }: CorrelationInformation) => {
expect(browserPreRequest).to.include({ requestId: '1234', url: 'foo', method: 'GET' })
expect(noPreRequestExpected).to.be.false
expectPendingCounts(0, 0)
done()
}
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
preRequests.addPending({ requestId: '1234', url: 'foo', method: 'GET' } as BrowserPreRequest)
})
it('synchronously matches a request without a pre-request added after the request', (done) => {
const cb = ({ browserPreRequest, noPreRequestExpected }: CorrelationInformation) => {
expect(browserPreRequest).to.be.undefined
expect(noPreRequestExpected).to.be.true
expectPendingCounts(0, 0)
done()
}
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
preRequests.addPendingUrlWithoutPreRequest('foo')
})
it('invokes a request callback after a timeout if no pre-request occurs', async () => {
let cb
const cbPromise = new Promise<void>((resolve) => {
cb = ({ browserPreRequest, noPreRequestExpected }: CorrelationInformation) => {
expect(browserPreRequest).to.be.undefined
expect(noPreRequestExpected).to.be.false
// we should have keep the pending request to eventually be correlated later, but don't block the body in the meantime
expectPendingCounts(1, 0)
resolve()
}
})
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
await cbPromise
const browserPreRequest: BrowserPreRequest = {
requestId: '1234',
url: 'foo',
method: 'GET',
headers: {},
resourceType: 'xhr',
originalResourceType: undefined,
cdpRequestWillBeSentTimestamp: 1,
cdpRequestWillBeSentReceivedTimestamp: performance.now() + performance.timeOrigin + 10000,
}
preRequests.addPending(browserPreRequest)
expectPendingCounts(0, 0)
const arg = (protocolManager.responseStreamTimedOut as any).getCall(0).args[0]
expect(arg.requestId).to.eq(browserPreRequest.requestId)
expect(arg.timings.cdpRequestWillBeSentTimestamp).to.eq(browserPreRequest.cdpRequestWillBeSentTimestamp)
expect(arg.timings.cdpRequestWillBeSentReceivedTimestamp).to.eq(browserPreRequest.cdpRequestWillBeSentReceivedTimestamp)
expect(arg.timings.proxyRequestReceivedTimestamp).to.be.a('number')
expect(arg.timings.cdpLagDuration).to.eq(browserPreRequest.cdpRequestWillBeSentReceivedTimestamp - browserPreRequest.cdpRequestWillBeSentTimestamp)
expect(arg.timings.proxyRequestCorrelationDuration).to.eq(browserPreRequest.cdpRequestWillBeSentReceivedTimestamp - arg.timings.proxyRequestReceivedTimestamp)
})
// https://github.com/cypress-io/cypress/issues/17853
it('eventually discards pre-requests that don\'t match requests', (done) => {
preRequests = new PreRequests(10, 200)
preRequests.addPending({ requestId: '1234', url: 'foo', method: 'GET', cdpRequestWillBeSentReceivedTimestamp: performance.now() + performance.timeOrigin } as BrowserPreRequest)
preRequests.addPendingUrlWithoutPreRequest('bar')
// preRequests garbage collects pre-requests that never matched up with an incoming request after around
// 2 * requestTimeout. We verify that it's gone (and therefore not leaking memory) by sending in a request
// and assuring that the pre-request wasn't there to be matched anymore.
setTimeout(() => {
const cb = ({ browserPreRequest, noPreRequestExpected }: CorrelationInformation) => {
expect(browserPreRequest).to.be.undefined
expect(noPreRequestExpected).to.be.false
expectPendingCounts(1, 0, 0)
done()
}
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
}, 1200)
})
it('removes a pre-request with a matching requestId', () => {
preRequests.addPending({ requestId: '1234', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1235', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1236', url: 'foo', method: 'GET' } as BrowserPreRequest)
expectPendingCounts(0, 3)
preRequests.removePendingPreRequest('1235')
expectPendingCounts(0, 2)
})
it('removes a pre-request with a matching requestId with retries', () => {
preRequests.addPending({ requestId: '1234', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1235', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1235-retry-1', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1235-retry-2', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1235-retry-3', url: 'foo', method: 'GET' } as BrowserPreRequest)
preRequests.addPending({ requestId: '1236', url: 'foo', method: 'GET' } as BrowserPreRequest)
expectPendingCounts(0, 6)
preRequests.removePendingPreRequest('1235')
expectPendingCounts(0, 2)
})
it('immediately handles a request from a service worker loading', () => {
const cbServiceWorker = sinon.stub()
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: { 'sec-fetch-dest': 'serviceworker' } } as any, () => {}, cbServiceWorker)
expect(cbServiceWorker).to.be.calledOnce
expect(cbServiceWorker).to.be.calledWith()
})
it('removes a pending request', () => {
const cb = sinon.stub()
const firstPreRequest = preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
const secondPreRequest = preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, cb)
expectPendingCounts(2, 0)
preRequests.removePendingRequest(firstPreRequest!)
expectPendingCounts(1, 0)
preRequests.removePendingRequest(firstPreRequest!)
expectPendingCounts(1, 0)
preRequests.removePendingRequest(secondPreRequest!)
expectPendingCounts(0, 0)
})
it('resets the queues', () => {
let callbackCalled = false
preRequests.addPending({ requestId: '1234', url: 'bar', method: 'GET' } as BrowserPreRequest)
preRequests.get({ proxiedUrl: 'foo', method: 'GET', headers: {} } as CypressIncomingRequest, () => {}, () => {
callbackCalled = true
})
preRequests.addPendingUrlWithoutPreRequest('baz')
expectPendingCounts(1, 1, 1)
preRequests.reset()
expectPendingCounts(0, 0, 0)
expect(callbackCalled).to.be.true
})
})