-1

I am trying to intercept an a request. This request has a specific endpoint and I would like to change this endpoint.

I have now solved it with the following code:

cy.intercept('**/i18n/nl.json', { fixture: 'LanguageForItemEditorENG.json' }).as('Language')

As you can see I have used a fixture, but I don't think that this is the best practice. My testcode would be more robust if I can route the request to

'**/i18n/en.json'

and not use a fixture.

So, at this moment the application is making a request to **/nl.json but I want to simulate this so the application gets the response of the **/en.json. How can I make this work?

E. E. Ozkan
  • 149
  • 10
  • You can update then [`.continue`](https://docs.cypress.io/api/commands/intercept#Providing-a-stub-response-with-req-reply) a request, but I don't know if it'll allow changing the URL. – jonrsharpe Oct 25 '22 at 10:07

1 Answers1

1

You can intercept the request and modify certain properties before continuing.

cy.intercept('**/i18n/nl.json', (req) => {
  req.url = req.url.replace('/i18n/nl.json', '/i18n/en.json');
  req.continue();
}).as('Language')

Aside: I don't think that stubbing the response is necessarily a bad idea. If you just need some data, then stubbing that data is fine. The only time that I would see that I absolutely need a network response would be a true, complete end-to-end test (of which you should have very few in your suite), or if I were specifically testing that endpoint.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • Thankyou for you fast help. Beside that, I definetely agree with your point of view about stubbing. In this case this is for an E2E test. – E. E. Ozkan Oct 26 '22 at 08:54