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 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,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 Expand Up @@ -314,6 +314,43 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
])
})

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

cy
.intercept(/dump-headers/, { middleware: true }, (req) => {
e.push('mware req handler')
req.on('before:response', (res) => {
e.push('mware before:response')
})

req.on('response', (res) => {
e.push('mware response')
})

req.on('after:response', (res) => {
e.push('mware after:response')
})
})
.intercept('/dump-headers*', (req) => {
e.push('normal req handler')
req.reply(() => {
e.push('normal res handler')
})
})
.then(() => {
return $.get('/dump-headers')
})
.wrap(e).should('have.all.members', [
'mware req handler',
'normal req handler',
'mware before:response',
'normal res handler',
'mware response',
'mware after:response',
])
})

flotwig marked this conversation as resolved.
Show resolved Hide resolved
it('chains request handlers from bottom-up', function (done) {
cy.intercept('/dump-headers*', (req) => {
req.reply((res) => {
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