Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename delayMs to delay, deprecate delay #14822

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,8 @@ describe('network stubbing', { retries: 2 }, function () {
it('can delay and throttle a StaticResponse', function (done) {
const payload = 'A'.repeat(10 * 1024)
const throttleKbps = 10
const delayMs = 250
const expectedSeconds = payload.length / (1024 * throttleKbps) + delayMs / 1000
const delay = 250
const expectedSeconds = payload.length / (1024 * throttleKbps) + delay / 1000

cy.intercept('/timeout', (req) => {
this.start = Date.now()
Expand All @@ -1029,7 +1029,7 @@ describe('network stubbing', { retries: 2 }, function () {
statusCode: 200,
body: payload,
throttleKbps,
delayMs,
delay,
})
}).then(() => {
return $.get('/timeout').then((responseText) => {
Expand All @@ -1041,23 +1041,41 @@ describe('network stubbing', { retries: 2 }, function () {
})
})

it('can delay with deprecated delayMs param', function (done) {
const delay = 250

cy.intercept('/timeout', (req) => {
this.start = Date.now()

req.reply({
delay,
})
}).then(() => {
return $.get('/timeout').then((responseText) => {
expect(Date.now() - this.start).to.be.closeTo(250 + 100, 100)

done()
})
})
})

// @see https://github.com/cypress-io/cypress/issues/14446
it('should delay the same amount on every response', () => {
const delayMs = 250
const delay = 250

const testDelay = () => {
const start = Date.now()

return $.get('/timeout').then((responseText) => {
expect(Date.now() - start).to.be.closeTo(delayMs, 50)
expect(Date.now() - start).to.be.closeTo(delay, 50)
expect(responseText).to.eq('foo')
})
}

cy.intercept('/timeout', {
statusCode: 200,
body: 'foo',
delayMs,
delay,
}).as('get')
.then(() => testDelay()).wait('@get')
.then(() => testDelay()).wait('@get')
Expand Down Expand Up @@ -1634,15 +1652,15 @@ describe('network stubbing', { retries: 2 }, function () {
const payload = 'A'.repeat(10 * 1024)
const kbps = 20
let expectedSeconds = payload.length / (1024 * kbps)
const delayMs = 500
const delay = 500

expectedSeconds += delayMs / 1000
expectedSeconds += delay / 1000

cy.intercept('/timeout', (req) => {
req.reply((res) => {
this.start = Date.now()

res.throttle(kbps).delay(delayMs).send({
res.throttle(kbps).delay(delay).send({
statusCode: 200,
body: payload,
})
Expand Down Expand Up @@ -1872,8 +1890,8 @@ describe('network stubbing', { retries: 2 }, function () {
it('can delay and throttle', function (done) {
const payload = 'A'.repeat(10 * 1024)
const throttleKbps = 50
const delayMs = 50
const expectedSeconds = payload.length / (1024 * throttleKbps) + delayMs / 1000
const delay = 50
const expectedSeconds = payload.length / (1024 * throttleKbps) + delay / 1000

cy.intercept('/timeout', (req) => {
req.reply((res) => {
Expand All @@ -1884,7 +1902,7 @@ describe('network stubbing', { retries: 2 }, function () {
statusCode: 200,
body: payload,
throttleKbps,
delayMs,
delay,
})
})
}).then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export const onResponseReceived: HandlerFn<NetEventFrames.HttpResponseReceived>

return sendContinueFrame()
},
delay (delayMs) {
continueFrame.delayMs = delayMs
delay (delay) {
continueFrame.delay = delay

return this
},
Expand Down
19 changes: 16 additions & 3 deletions packages/driver/src/cy/net-stubbing/static-response-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
import $errUtils from '../../cypress/error_utils'

// user-facing StaticResponse only
export const STATIC_RESPONSE_KEYS: (keyof StaticResponse)[] = ['body', 'fixture', 'statusCode', 'headers', 'forceNetworkError', 'throttleKbps', 'delayMs']
export const STATIC_RESPONSE_KEYS: (keyof StaticResponse)[] = ['body', 'fixture', 'statusCode', 'headers', 'forceNetworkError', 'throttleKbps', 'delay', 'delayMs']

export function validateStaticResponse (cmd: string, staticResponse: StaticResponse): void {
const err = (message) => {
$errUtils.throwErrByPath('net_stubbing.invalid_static_response', { args: { cmd, message, staticResponse } })
}

const { body, fixture, statusCode, headers, forceNetworkError, throttleKbps, delayMs } = staticResponse
const { body, fixture, statusCode, headers, forceNetworkError, throttleKbps, delay, delayMs } = staticResponse

if (forceNetworkError && (body || statusCode || headers)) {
err('`forceNetworkError`, if passed, must be the only option in the StaticResponse.')
Expand Down Expand Up @@ -43,9 +43,17 @@ export function validateStaticResponse (cmd: string, staticResponse: StaticRespo
err('`throttleKbps` must be a finite, positive number.')
}

if (delayMs && delay) {
err('`delayMs` and `delay` cannot both be set.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be more like "delayMs has been deprecated. Use delay instead.`"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I tried thinking of a better error, but it's backwards-compatible so the wording is tricky. I think this wording is prob fine - we still support delayMs, so this is the most accurate error. In the future when delayMs is removed it can be updated.

}

if (delayMs && (!_.isFinite(delayMs) || delayMs < 0)) {
err('`delayMs` must be a finite, positive number.')
}

if (delay && (!_.isFinite(delay) || delay < 0)) {
err('`delay` must be a finite, positive number.')
}
}

export function parseStaticResponseShorthand (statusCodeOrBody: number | string | any, bodyOrHeaders: string | { [key: string]: string }, maybeHeaders?: { [key: string]: string }) {
Expand Down Expand Up @@ -88,7 +96,12 @@ function getFixtureOpts (fixture: string): FixtureOpts {
}

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

if (staticResponse.delayMs) {
// support deprecated `delayMs` usage
backendStaticResponse.delay = staticResponse.delayMs
}

if (staticResponse.fixture) {
backendStaticResponse.fixture = getFixtureOpts(staticResponse.fixture)
Expand Down
14 changes: 10 additions & 4 deletions packages/net-stubbing/lib/external-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export namespace CyHttpMessages {
*/
send(): void
/**
* Wait for `delayMs` milliseconds before sending the response to the client.
* Wait for `delay` milliseconds before sending the response to the client.
*/
delay: (delayMs: number) => IncomingHttpResponse
delay: (delay: number) => IncomingHttpResponse
/**
* Serve the response at `throttleKbps` kilobytes per second.
*/
Expand Down Expand Up @@ -292,7 +292,13 @@ export type RouteHandler = string | StaticResponse | RouteHandlerController | ob
/**
* Describes a response that will be sent back to the browser to fulfill the request.
*/
export type StaticResponse = GenericStaticResponse<string, string | object>
export type StaticResponse = GenericStaticResponse<string, string | object> & {
/**
* Milliseconds to delay before the response is sent.
* @deprecated Use `delay` instead of `delayMs`.
*/
delayMs?: number
}

export interface GenericStaticResponse<Fixture, Body> {
/**
Expand Down Expand Up @@ -326,7 +332,7 @@ export interface GenericStaticResponse<Fixture, Body> {
/**
* Milliseconds to delay before the response is sent.
*/
delayMs?: number
delay?: number
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/net-stubbing/lib/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export declare namespace NetEventFrames {
res?: CyHttpMessages.IncomingResponse
staticResponse?: BackendStaticResponse
// Millisecond timestamp for when the response should continue
delayMs?: number
delay?: number
throttleKbps?: number
followRedirect?: boolean
}
Expand Down
6 changes: 3 additions & 3 deletions packages/net-stubbing/lib/server/intercept-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ export async function onResponseContinue (state: NetStubbingState, frame: NetEve
debug('_onResponseContinue %o', { backendRequest: _.omit(backendRequest, 'res.body'), frame: _.omit(frame, 'res.body') })

const throttleKbps = _.get(frame, 'staticResponse.throttleKbps') || frame.throttleKbps
const delayMs = _.get(frame, 'staticResponse.delayMs') || frame.delayMs
const delay = _.get(frame, 'staticResponse.delay') || frame.delay

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

const staticResponse = _.chain(frame.staticResponse).clone().assign({ delayMs, throttleKbps }).value()
const staticResponse = _.chain(frame.staticResponse).clone().assign({ delay, throttleKbps }).value()

return sendStaticResponse(backendRequest, staticResponse)
}

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

const bodyStream = getBodyStream(res.body, { throttleKbps, delayMs })
const bodyStream = getBodyStream(res.body, { throttleKbps, delay })

return backendRequest.continueResponse!(bodyStream)
}
8 changes: 4 additions & 4 deletions packages/net-stubbing/lib/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ export function sendStaticResponse (backendRequest: Pick<BackendRequest, 'onErro
body,
})

const bodyStream = getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'delayMs'))
const bodyStream = getBodyStream(body, _.pick(staticResponse, 'throttleKbps', 'delay'))

onResponse!(incomingRes, bodyStream)
}

export function getBodyStream (body: Buffer | string | Readable | undefined, options: { delayMs?: number, throttleKbps?: number }): Readable {
const { delayMs, throttleKbps } = options
export function getBodyStream (body: Buffer | string | Readable | undefined, options: { delay?: number, throttleKbps?: number }): Readable {
const { delay, throttleKbps } = options
const pt = new PassThrough()

const sendBody = () => {
Expand All @@ -195,7 +195,7 @@ export function getBodyStream (body: Buffer | string | Readable | undefined, opt
return writable.end()
}

delayMs ? setTimeout(sendBody, delayMs) : sendBody()
delay ? setTimeout(sendBody, delay) : sendBody()

return pt
}