Skip to content

Commit 0fc3944

Browse files
authoredJul 13, 2021
fix: cy.intercept delay works correctly with 204 No Content (#17126)
1 parent 56a30e0 commit 0fc3944

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed
 

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

+24
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,30 @@ describe('network stubbing', { retries: 2 }, function () {
16821682
.then(() => testDelay()).wait('@get')
16831683
})
16841684

1685+
// https://github.com/cypress-io/cypress/issues/15188
1686+
it('delay works correctly with 204 No Content', (done) => {
1687+
cy.on('fail', (err) => {
1688+
expect(err.message).to.include('No response ever occurred')
1689+
1690+
done()
1691+
})
1692+
1693+
cy.intercept('POST', '/post-only', {
1694+
statusCode: 204, // delay is not respected
1695+
delay: 5000,
1696+
}).as('create')
1697+
1698+
cy.window().then((win) => {
1699+
win.eval(
1700+
`fetch("/post-only", {
1701+
method: 'POST', // *GET, POST, PUT, DELETE, etc.
1702+
});`,
1703+
)
1704+
})
1705+
1706+
cy.wait('@create', { timeout: 500 })
1707+
})
1708+
16851709
// @see https://github.com/cypress-io/cypress/issues/15901
16861710
it('can intercept utf-8 request bodies without crashing', function () {
16871711
cy.intercept('POST', 'http://localhost:5000/api/sample')

‎packages/net-stubbing/lib/server/driver-events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function sendStaticResponse (state: NetStubbingState, getFixture: GetFixtu
7373

7474
await setResponseFromFixture(getFixture, options.staticResponse)
7575

76-
_sendStaticResponse(request, options.staticResponse)
76+
await _sendStaticResponse(request, options.staticResponse)
7777
}
7878

7979
export function _restoreMatcherOptionsTypes (options: AnnotatedRouteMatcherOptions) {

‎packages/net-stubbing/lib/server/intercepted-request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export class InterceptedRequest {
194194
}
195195

196196
if (immediateStaticResponse) {
197-
sendStaticResponse(this, immediateStaticResponse)
197+
await sendStaticResponse(this, immediateStaticResponse)
198198

199199
return data
200200
}

‎packages/net-stubbing/lib/server/middleware/response.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const InterceptResponse: ResponseMiddleware = async function () {
7979

8080
mergeChanges(request.res as any, modifiedRes)
8181

82-
const bodyStream = getBodyStream(modifiedRes.body, _.pick(modifiedRes, ['throttleKbps', 'delay']) as any)
82+
const bodyStream = await getBodyStream(modifiedRes.body, _.pick(modifiedRes, ['throttleKbps', 'delay']) as any)
8383

8484
return request.continueResponse!(bodyStream)
8585
}

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function setResponseFromFixture (getFixtureFn: GetFixtureFn, static
153153
* @param backendRequest BackendRequest object.
154154
* @param staticResponse BackendStaticResponse object.
155155
*/
156-
export function sendStaticResponse (backendRequest: Pick<InterceptedRequest, 'res' | 'onError' | 'onResponse'>, staticResponse: BackendStaticResponse) {
156+
export async function sendStaticResponse (backendRequest: Pick<InterceptedRequest, 'res' | 'onError' | 'onResponse'>, staticResponse: BackendStaticResponse) {
157157
const { onError, onResponse } = backendRequest
158158

159159
if (staticResponse.forceNetworkError) {
@@ -173,12 +173,12 @@ export function sendStaticResponse (backendRequest: Pick<InterceptedRequest, 're
173173
body,
174174
})
175175

176-
const bodyStream = getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'delay'))
176+
const bodyStream = await getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'delay'))
177177

178178
onResponse!(incomingRes, bodyStream)
179179
}
180180

181-
export function getBodyStream (body: Buffer | string | Readable | undefined, options: { delay?: number, throttleKbps?: number }): Readable {
181+
export async function getBodyStream (body: Buffer | string | Readable | undefined, options: { delay?: number, throttleKbps?: number }): Promise<Readable> {
182182
const { delay, throttleKbps } = options
183183
const pt = new PassThrough()
184184

@@ -203,11 +203,19 @@ export function getBodyStream (body: Buffer | string | Readable | undefined, opt
203203
return writable.end()
204204
}
205205

206-
delay ? setTimeout(sendBody, delay) : sendBody()
206+
delay ? await wait(sendBody, delay) : sendBody()
207207

208208
return pt
209209
}
210210

211+
function wait (fn, ms) {
212+
return new Promise((resolve) => {
213+
setTimeout(() => {
214+
resolve(fn())
215+
}, ms)
216+
})
217+
}
218+
211219
export function mergeDeletedHeaders (before: CyHttpMessages.BaseMessage, after: CyHttpMessages.BaseMessage) {
212220
for (const k in before.headers) {
213221
// a header was deleted from `after` but was present in `before`, delete it in `before` too

4 commit comments

Comments
 (4)

cypress-bot[bot] commented on Jul 13, 2021

@cypress-bot[bot]
Contributor

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/circle-develop-0fc39441fb7cf722de68b8d69abd921d3ea4d843/cypress.tgz

cypress-bot[bot] commented on Jul 13, 2021

@cypress-bot[bot]
Contributor

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/appveyor-develop-0fc39441fb7cf722de68b8d69abd921d3ea4d843/cypress.tgz

cypress-bot[bot] commented on Jul 13, 2021

@cypress-bot[bot]
Contributor

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/appveyor-develop-0fc39441fb7cf722de68b8d69abd921d3ea4d843/cypress.tgz

cypress-bot[bot] commented on Jul 13, 2021

@cypress-bot[bot]
Contributor

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.8.0/circle-develop-0fc39441fb7cf722de68b8d69abd921d3ea4d843/cypress.tgz
Please sign in to comment.