feat(net-stubbing): rename cy.route2 to cy.http (#9182)

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
This commit is contained in:
Zach Bloomquist
2020-11-14 02:52:21 -05:00
committed by GitHub
parent cc0a80ffb8
commit 3d0d1d89de
12 changed files with 206 additions and 165 deletions

View File

@@ -237,7 +237,7 @@
"experimentalNetworkStubbing": {
"type": "boolean",
"default": false,
"description": "Enables `cy.route2`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)"
"description": "Enables `cy.http()`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)"
},
"experimentalFetchPolyfill": {
"type": "boolean",

View File

@@ -2577,7 +2577,7 @@ declare namespace Cypress {
*/
experimentalSourceRewriting: boolean
/**
* Enables `cy.route2`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)
* Enables `cy.http()`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)
* @default false
*/
experimentalNetworkStubbing: boolean

View File

@@ -10,7 +10,7 @@ describe('Release Notes', () => {
cy.fixture('user').then((theUser) => user = theUser)
cy.fixture('release_notes').then((theReleaseNotes) => releaseNotes = theReleaseNotes)
cy.route2('cypress-banner.jpg', { fixture: 'cypress-banner.jpg' })
cy.http('cypress-banner.jpg', { fixture: 'cypress-banner.jpg' })
cy.visitIndex().then((win) => {
ipc = win.App.ipc

View File

@@ -26,7 +26,7 @@ Cypress.Commands.add('visitIndex', (options = {}) => {
// disable livereload within the Cypress-loaded desktop GUI. it doesn't fully
// reload the app because the stubbed out ipc calls don't work after the first
// time, so it ends up a useless white page
cy.route2({ path: /livereload/ }, '')
cy.http({ path: /livereload/ }, '')
cy.visit('/', options)
})

View File

@@ -112,7 +112,7 @@ describe('driver/src/cy/snapshots', () => {
it('does not cause images to be requested multiple times', function () {
let timesRequested = 0
cy.route2('media/cypress.png', () => {
cy.http('media/cypress.png', () => {
timesRequested++
})
.then(() => {

View File

@@ -25,7 +25,7 @@ const throwErr = (arg) => {
module.exports = (Commands, Cypress, cy, state) => {
const isDynamicAliasingPossible = () => {
// dynamic aliasing is possible if cy.route2 is enabled and a route with dynamic interception has been defined
// dynamic aliasing is possible if cy.http is enabled and a route with dynamic interception has been defined
return Cypress.config('experimentalNetworkStubbing') && _.find(state('routes'), (route) => {
return _.isFunction(route.handler)
})
@@ -119,8 +119,8 @@ module.exports = (Commands, Cypress, cy, state) => {
try {
aliasObj = cy.getAlias(str, 'wait', log)
} catch (err) {
// before cy.route2, we could know when an alias did/did not exist, because they
// were declared synchronously. with cy.route2, req.alias can be used to dynamically
// before cy.http, we could know when an alias did/did not exist, because they
// were declared synchronously. with cy.http, req.alias can be used to dynamically
// create aliases, so we cannot know at wait-time if an alias exists or not
if (!isDynamicAliasingPossible()) {
throw err
@@ -163,7 +163,7 @@ module.exports = (Commands, Cypress, cy, state) => {
log.set('referencesAlias', aliases)
}
if (command && !['route', 'route2'].includes(command.get('name'))) {
if (command && !['route', 'route2', 'http'].includes(command.get('name'))) {
$errUtils.throwErrByPath('wait.invalid_alias', {
onFail: options._log,
args: { alias },

View File

@@ -208,7 +208,7 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
hasInterceptor = true
break
case _.isUndefined(handler):
// user is doing something like cy.route2('foo').as('foo') to wait on a URL
// user is doing something like cy.http('foo').as('foo') to wait on a URL
break
case _.isString(handler):
staticResponse = { body: <string>handler }
@@ -222,12 +222,12 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
}
}
validateStaticResponse('cy.route2', <StaticResponse>handler)
validateStaticResponse('cy.http', <StaticResponse>handler)
staticResponse = handler as StaticResponse
break
default:
return $errUtils.throwErrByPath('net_stubbing.route2.invalid_handler', { args: { handler } })
return $errUtils.throwErrByPath('net_stubbing.http.invalid_handler', { args: { handler } })
}
const routeMatcher = annotateMatcherOptionsTypes(matcher)
@@ -263,9 +263,16 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
return emitNetEvent('route:added', frame)
}
function route2 (matcher: RouteMatcher, handler?: RouteHandler | StringMatcher, arg2?: RouteHandler) {
function route2 (...args) {
$errUtils.warnByPath('net_stubbing.route2_renamed')
// @ts-ignore
return http.apply(undefined, args)
}
function http (matcher: RouteMatcher, handler?: RouteHandler | StringMatcher, arg2?: RouteHandler) {
if (!Cypress.config('experimentalNetworkStubbing')) {
return $errUtils.throwErrByPath('net_stubbing.route2.needs_experimental')
return $errUtils.throwErrByPath('net_stubbing.http.needs_experimental')
}
function getMatcherOptions (): RouteMatcherOptions {
@@ -295,7 +302,7 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
const { isValid, message } = validateRouteMatcherOptions(routeMatcherOptions)
if (!isValid) {
$errUtils.throwErrByPath('net_stubbing.route2.invalid_route_matcher', { args: { message, matcher: routeMatcherOptions } })
$errUtils.throwErrByPath('net_stubbing.http.invalid_route_matcher', { args: { message, matcher: routeMatcherOptions } })
}
return addRoute(routeMatcherOptions, handler as RouteHandler)
@@ -303,6 +310,7 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
}
Commands.addAll({
http,
route2,
})
}

View File

@@ -22,7 +22,7 @@ export function waitForRoute (alias: string, state: Cypress.State, specifier: 'r
const candidateRequests = _.filter(state('aliasedRequests'), { alias })
.map(({ request }) => request)
// Now add route-level (cy.route2(...).as()) aliased requests.
// Now add route-level (cy.http(...).as()) aliased requests.
const route: Route = _.find(state('routes'), { alias })
if (route) {

View File

@@ -909,28 +909,29 @@ module.exports = {
},
net_stubbing: {
route2_renamed: `${cmd('route2')} was renamed to ${cmd('http')} and will be removed in a future release. Please update usages of ${cmd('route2')} to use ${cmd('http')} instead.`,
invalid_static_response: ({ cmd, message, staticResponse }) => {
return cyStripIndent(`\
An invalid StaticResponse was supplied to \`${cmd}()\`. ${message}
You passed: ${format(staticResponse)}`, 8)
},
route2: {
http: {
needs_experimental: stripIndent`\
${cmd('route2')} requires experimental network mocking to be enabled.
${cmd('http')} requires experimental network mocking to be enabled.
Set the \`experimentalNetworkStubbing\` config value to \`true\` to access this command.
Read more: https://on.cypress.io/experiments`,
invalid_handler: ({ handler }) => {
return stripIndent`\
${cmd('route2')}'s \`handler\` argument must be a String, StaticResponse, or HttpController function.
${cmd('http')}'s \`handler\` argument must be a String, StaticResponse, or HttpController function.
You passed: ${format(handler)}`
},
invalid_route_matcher: ({ message, matcher }) => {
return stripIndent`\
An invalid RouteMatcher was supplied to ${cmd('route2')}. ${message}
An invalid RouteMatcher was supplied to ${cmd('http')}. ${message}
You passed: ${format(matcher)}`
},
@@ -938,7 +939,7 @@ module.exports = {
request_handling: {
cb_failed: ({ err, req, route }) => {
return cyStripIndent(`\
A request callback passed to ${cmd('route2')} threw an error while intercepting a request:
A request callback passed to ${cmd('http')} threw an error while intercepting a request:
${err.message}
@@ -948,7 +949,7 @@ module.exports = {
},
cb_timeout: ({ timeout, req, route }) => {
return cyStripIndent(`\
A request callback passed to ${cmd('route2')} timed out after returning a Promise that took more than the \`defaultCommandTimeout\` of \`${timeout}ms\` to resolve.
A request callback passed to ${cmd('http')} timed out after returning a Promise that took more than the \`defaultCommandTimeout\` of \`${timeout}ms\` to resolve.
If the request callback is expected to take longer than \`${timeout}ms\`, increase the configured \`defaultCommandTimeout\` value.

View File

@@ -332,35 +332,45 @@ declare global {
namespace Cypress {
interface Chainable<Subject = any> {
/**
* Use `cy.route2()` to stub and intercept HTTP requests and responses.
* Use `cy.http()` to stub and intercept HTTP requests and responses.
*
* Note: this command is only available if you have set the `experimentalNetworkStubbing`
* configuration option to `true`.
*
* @see https://on.cypress.io/route2
* @see https://on.cypress.io/http
* @example
* cy.route2('https://localhost:7777/users', [{id: 1, name: 'Pat'}])
* cy.http('https://localhost:7777/users', [{id: 1, name: 'Pat'}])
* @example
* cy.route2('https://localhost:7777/protected-endpoint', (req) => {
* cy.http('https://localhost:7777/protected-endpoint', (req) => {
* req.headers['authorization'] = 'basic fooabc123'
* })
* @example
* cy.route2('https://localhost:7777/some-response', (req) => {
* cy.http('https://localhost:7777/some-response', (req) => {
* req.reply(res => {
* res.body = 'some new body'
* })
* })
*/
route2(url: RouteMatcher, response?: RouteHandler): Chainable<null>
http(url: RouteMatcher, response?: RouteHandler): Chainable<null>
/**
* Use `cy.route2()` to stub and intercept HTTP requests and responses.
* Use `cy.http()` to stub and intercept HTTP requests and responses.
*
* Note: this command is only available if you have set the `experimentalNetworkStubbing`
* configuration option to `true`.
*
* @see https://on.cypress.io/route2
* @see https://on.cypress.io/http
* @example
* cy.route2('GET', 'http://foo.com/fruits', ['apple', 'banana', 'cherry'])
* cy.http('GET', 'http://foo.com/fruits', ['apple', 'banana', 'cherry'])
*/
http(method: Method, url: RouteMatcher, response?: RouteHandler): Chainable<null>
/**
* Deprecated - use `cy.http()` instead.
* @deprecated
*/
route2(url: RouteMatcher, response?: RouteHandler): Chainable<null>
/**
* Deprecated - use `cy.http()` instead.
* @deprecated
*/
route2(method: Method, url: RouteMatcher, response?: RouteHandler): Chainable<null>
}

View File

@@ -52,7 +52,7 @@ interface StringValues {
*/
const _summaries: StringValues = {
experimentalComponentTesting: 'Framework-specific component testing, uses `componentFolder` to load component specs',
experimentalNetworkStubbing: 'Enables `cy.route2`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)',
experimentalNetworkStubbing: 'Enables `cy.http()`, which can be used to dynamically intercept/stub/await any HTTP request or response (XHRs, fetch, beacons, etc.)',
experimentalSourceRewriting: 'Enables AST-based JS/HTML rewriting. This may fix issues caused by the existing regex-based JS/HTML replacement algorithm.',
experimentalFetchPolyfill: 'Polyfills `window.fetch` to enable Network spying and stubbing',
}