Skip to content

Commit

Permalink
feat(intercept): support regexp matcher with middleware intercepts (#…
Browse files Browse the repository at this point in the history
…16390)

Co-authored-by: Zach Bloomquist <[email protected]>
  • Loading branch information
Andarist and flotwig authored May 10, 2021
1 parent 6bc7e7d commit a534b17
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
46 changes: 44 additions & 2 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
this.testRoute(options, handler, expectedEvent, expectedRoute)
})

it('mergeRouteMatcher works when supplied', function () {
it('mergeRouteMatcher + string url works', function () {
const url = '/foo*'

const handler = (req) => {
Expand Down Expand Up @@ -221,6 +221,48 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
})
.wait('@get')
})

// @see https://github.com/cypress-io/cypress/pull/16390
it('mergeRouteMatcher + regex url works', function () {
const url = /^\/foo.*/

const handler = (req) => {
// @ts-ignore
const routeId = _.findKey(state('routes'), { handler })
const route = state('routes')[routeId!]

// @ts-ignore
expectedEvent.routeId = routeId
expect(this.emit).to.be.calledWith('backend:request', 'net', 'route:added', expectedEvent)

expect(route.handler).to.deep.eq(expectedRoute.handler)
expect(route.options).to.deep.eq(expectedRoute.options)

req.reply('a')
}

const expectedRoute = {
options: { url, middleware: true },
handler,
}

const expectedEvent = {
routeMatcher: {
url: {
type: 'regex',
value: String(url),
},
middleware: true,
},
hasInterceptor: true,
}

cy.intercept(url, { middleware: true }, handler).as('get')
.then(() => {
return $.get('/foo')
})
.wait('@get')
})
})

// https://github.com/cypress-io/cypress/issues/8729
Expand Down Expand Up @@ -277,7 +319,7 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
})

context('overrides', function () {
it('chains middleware as expected', function () {
it('chains middleware with string matcher as expected', function () {
const e: string[] = []

cy
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/net-stubbing/add-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,

function intercept (matcher: RouteMatcher, handler?: RouteHandler | StringMatcher | RouteMatcherOptions, arg2?: RouteHandler) {
function getMatcherOptions (): RouteMatcherOptions {
if (_.isString(matcher) && hasOnlyRouteMatcherKeys(handler)) {
if (isStringMatcher(matcher) && hasOnlyRouteMatcherKeys(handler)) {
// url, mergeRouteMatcher, handler
// @ts-ignore
if (handler.url) {
Expand Down
2 changes: 1 addition & 1 deletion packages/net-stubbing/lib/external-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ declare global {
*
* @param mergeRouteMatcher Additional route matcher options to merge with `url`. Typically used for middleware.
*/
intercept(url: string, mergeRouteMatcher: Omit<RouteMatcherOptions, 'url'>, response: RouteHandler): Chainable<null>
intercept(url: StringMatcher, mergeRouteMatcher: Omit<RouteMatcherOptions, 'url'>, response: RouteHandler): Chainable<null>
/**
* Wait for a specific request to complete.
*
Expand Down

4 comments on commit a534b17

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a534b17 May 10, 2021

Choose a reason for hiding this comment

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

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.3.0/circle-develop-a534b17813c45fb4d72ce6a2da8c996c42479d71/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a534b17 May 10, 2021

Choose a reason for hiding this comment

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

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.3.0/appveyor-develop-a534b17813c45fb4d72ce6a2da8c996c42479d71/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a534b17 May 10, 2021

Choose a reason for hiding this comment

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

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.3.0/appveyor-develop-a534b17813c45fb4d72ce6a2da8c996c42479d71/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a534b17 May 10, 2021

Choose a reason for hiding this comment

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

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.3.0/circle-develop-a534b17813c45fb4d72ce6a2da8c996c42479d71/cypress.tgz

Please sign in to comment.