Skip to content

Commit

Permalink
feat(intercept): support regexp matcher with middleware intercepts
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed May 7, 2021
1 parent c340bef commit 57c6623
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
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',
])
})

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

0 comments on commit 57c6623

Please sign in to comment.