1

I'm intercepting a request with Cypress and I'm trying not to hardcode something. To make it work correctly, I have to add a "1" since I'm trying to intercept something like this:

/api/v1/contacts/cfs:12345/forms/144543

The problem is that there are other requests like /forms/something that are being intercepted if I use forms/**, leading to this:

let urlResponse = '/api/v1/contacts/cfs:' + $code + '/forms/1**'

Is there any regex or something that could let me just intercept the URL that's forms/number and not forms/something or forms/number/something?

Blunt
  • 154
  • 7

2 Answers2

1

Here is some regex for matching just the .../forms/number pattern.

const inputs = [
  '/api/v1/contacts/cfs:12345/forms/144543',
  '/api/v1/contacts/cfs:12345/forms/something',
  '/api/v1/contacts/cfs:12345/forms/144543/something'
];

const regex = new RegExp('/api/v1/contacts/cfs:12345/forms/[0-9]+$');

for (let input of inputs) {
  const isMatch = regex.test(input);
  console.log(isMatch);
}
Rocky Sims
  • 3,523
  • 1
  • 14
  • 19
0

Cypress uses minimatch to validate a url, so you can use:

const part = '12345';
cy.intercept(`/api/v1/contacts/cfs:${part}/forms/1[0-9]*`).as('alias');

And check if it is actually matching what is expected in this way:

expect(Cypress.minimatch('/api/v1/contacts/cfs:12345/forms/144543', `/api/v1/contacts/cfs:${part}/forms/1[0-9]*`)).to.be.true;
expect(Cypress.minimatch('/api/v1/contacts/cfs:12345/forms/something', `/api/v1/contacts/cfs:${part}/forms/1[0-9]*`)).to.be.false;
expect(Cypress.minimatch('/api/v1/contacts/cfs:12345/forms/144543/something', `/api/v1/contacts/cfs:${part}/forms/1[0-9]*`)).to.be.false;
meisdjo
  • 1
  • 1