Skip to content

Commit c3dc7f4

Browse files
authored
fix: correctly wait for static delayMs w/ cy.intercept (#14708)
1 parent f6b747a commit c3dc7f4

File tree

7 files changed

+38
-25
lines changed

7 files changed

+38
-25
lines changed

packages/driver/cypress/integration/commands/net_stubbing_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,29 @@ describe('network stubbing', { retries: 2 }, function () {
10411041
})
10421042
})
10431043

1044+
// @see https://github.com/cypress-io/cypress/issues/14446
1045+
it('should delay the same amount on every response', () => {
1046+
const delayMs = 250
1047+
1048+
const testDelay = () => {
1049+
const start = Date.now()
1050+
1051+
return $.get('/timeout').then((responseText) => {
1052+
expect(Date.now() - start).to.be.closeTo(delayMs, 50)
1053+
expect(responseText).to.eq('foo')
1054+
})
1055+
}
1056+
1057+
cy.intercept('/timeout', {
1058+
statusCode: 200,
1059+
body: 'foo',
1060+
delayMs,
1061+
}).as('get')
1062+
.then(() => testDelay()).wait('@get')
1063+
.then(() => testDelay()).wait('@get')
1064+
.then(() => testDelay()).wait('@get')
1065+
})
1066+
10441067
context('body parsing', function () {
10451068
it('automatically parses JSON request bodies', function () {
10461069
const p = Promise.defer()

packages/driver/src/cy/net-stubbing/events/response-received.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ export const onResponseReceived: HandlerFn<NetEventFrames.HttpResponseReceived>
9797
return sendContinueFrame()
9898
},
9999
delay (delayMs) {
100-
// reduce perceived delay by sending timestamp instead of offset
101-
continueFrame.continueResponseAt = Date.now() + delayMs
100+
continueFrame.delayMs = delayMs
102101

103102
return this
104103
},

packages/driver/src/cy/net-stubbing/static-response-utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function getFixtureOpts (fixture: string): FixtureOpts {
8888
}
8989

9090
export function getBackendStaticResponse (staticResponse: Readonly<StaticResponse>): BackendStaticResponse {
91-
const backendStaticResponse: BackendStaticResponse = _.omit(staticResponse, 'body', 'fixture', 'delayMs')
91+
const backendStaticResponse: BackendStaticResponse = _.omit(staticResponse, 'body', 'fixture')
9292

9393
if (staticResponse.fixture) {
9494
backendStaticResponse.fixture = getFixtureOpts(staticResponse.fixture)
@@ -103,10 +103,6 @@ export function getBackendStaticResponse (staticResponse: Readonly<StaticRespons
103103
}
104104
}
105105

106-
if (staticResponse.delayMs) {
107-
backendStaticResponse.continueResponseAt = Date.now() + staticResponse.delayMs
108-
}
109-
110106
return backendStaticResponse
111107
}
112108

packages/net-stubbing/lib/external-types.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,7 @@ export type RouteHandler = string | StaticResponse | RouteHandlerController | ob
292292
/**
293293
* Describes a response that will be sent back to the browser to fulfill the request.
294294
*/
295-
export type StaticResponse = GenericStaticResponse<string, string | object> & {
296-
/**
297-
* Milliseconds to delay before the response is sent.
298-
*/
299-
delayMs?: number
300-
}
295+
export type StaticResponse = GenericStaticResponse<string, string | object>
301296

302297
export interface GenericStaticResponse<Fixture, Body> {
303298
/**
@@ -328,6 +323,10 @@ export interface GenericStaticResponse<Fixture, Body> {
328323
* Kilobits per second to send 'body'.
329324
*/
330325
throttleKbps?: number
326+
/**
327+
* Milliseconds to delay before the response is sent.
328+
*/
329+
delayMs?: number
331330
}
332331

333332
/**

packages/net-stubbing/lib/internal-types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ export type FixtureOpts = {
1010
filePath: string
1111
}
1212

13-
export type BackendStaticResponse = GenericStaticResponse<FixtureOpts, string> & {
14-
// Millisecond timestamp for when the response should continue
15-
continueResponseAt?: number
16-
}
13+
export type BackendStaticResponse = GenericStaticResponse<FixtureOpts, string>
1714

1815
export const SERIALIZABLE_REQ_PROPS = [
1916
'headers',
@@ -91,7 +88,7 @@ export declare namespace NetEventFrames {
9188
res?: CyHttpMessages.IncomingResponse
9289
staticResponse?: BackendStaticResponse
9390
// Millisecond timestamp for when the response should continue
94-
continueResponseAt?: number
91+
delayMs?: number
9592
throttleKbps?: number
9693
followRedirect?: boolean
9794
}

packages/net-stubbing/lib/server/intercept-response.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,21 @@ export async function onResponseContinue (state: NetStubbingState, frame: NetEve
103103
debug('_onResponseContinue %o', { backendRequest: _.omit(backendRequest, 'res.body'), frame: _.omit(frame, 'res.body') })
104104

105105
const throttleKbps = _.get(frame, 'staticResponse.throttleKbps') || frame.throttleKbps
106-
const continueResponseAt = _.get(frame, 'staticResponse.continueResponseAt') || frame.continueResponseAt
106+
const delayMs = _.get(frame, 'staticResponse.delayMs') || frame.delayMs
107107

108108
if (frame.staticResponse) {
109109
// replacing response with a staticResponse
110110
await setResponseFromFixture(backendRequest.route.getFixture, frame.staticResponse)
111111

112-
const staticResponse = _.chain(frame.staticResponse).clone().assign({ continueResponseAt, throttleKbps }).value()
112+
const staticResponse = _.chain(frame.staticResponse).clone().assign({ delayMs, throttleKbps }).value()
113113

114114
return sendStaticResponse(backendRequest, staticResponse)
115115
}
116116

117117
// merge the changed response attributes with our response and continue
118118
_.assign(res, _.pick(frame.res, SERIALIZABLE_RES_PROPS))
119119

120-
const bodyStream = getBodyStream(res.body, { throttleKbps, continueResponseAt })
120+
const bodyStream = getBodyStream(res.body, { throttleKbps, delayMs })
121121

122122
return backendRequest.continueResponse!(bodyStream)
123123
}

packages/net-stubbing/lib/server/util.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,13 @@ export function sendStaticResponse (backendRequest: Pick<BackendRequest, 'onErro
165165
body,
166166
})
167167

168-
const bodyStream = getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'continueResponseAt'))
168+
const bodyStream = getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'delayMs'))
169169

170170
onResponse!(incomingRes, bodyStream)
171171
}
172172

173-
export function getBodyStream (body: Buffer | string | Readable | undefined, options: { continueResponseAt?: number, throttleKbps?: number }): Readable {
174-
const { continueResponseAt, throttleKbps } = options
175-
const delayMs = continueResponseAt ? _.max([continueResponseAt - Date.now(), 0]) : 0
173+
export function getBodyStream (body: Buffer | string | Readable | undefined, options: { delayMs?: number, throttleKbps?: number }): Readable {
174+
const { delayMs, throttleKbps } = options
176175
const pt = new PassThrough()
177176

178177
const sendBody = () => {

0 commit comments

Comments
 (0)