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(intercept): support regexp matcher with middleware intercepts #16390

Merged
merged 2 commits into from
May 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -283,7 +283,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 @@ -498,7 +498,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