0

I have a code where an endpoint(https://localhost:3000/endpoint/A) triggers a method and this method internally calls another endpoint(https://localhost:3000/endpoint/B). So now the headers of second endpoint (https://localhost:3000/endpoint/B) have to be modified as there are some mandatory custom headers.

So i tried using the below code to intercept second endpoint and modify headers but the cypress test is not intercepting this request. Can someone throw some inputs or let me know what is the mistake i am doing here.

  cy.intercept('POST', '/endpoint/B', (req) => {
    req.headers['custom-header-one'] = "ch1"
    req.headers['custom-header-two'] = "ch2"
    cy.log(JSON.stringify(req.headers));
  })

Edit 1 :

The second call I am doing is a third party endpoint and it happens on the node server.

Removed the http from the code as its not working with or without http

Udo.Kier
  • 228
  • 7
Deadpool_er
  • 215
  • 4
  • 20
  • What does "The second call ... happens on the node server" mean? Not clear what's going on here. – Rab.Scarabelli Feb 16 '23 at 06:05
  • thanks @Rab.Scarabelli . I meant the second call is at the server level which i mean its not visible on the clientside (network tab). So our architecture is like the first call which we see is like a proxy on the client side and it triggers some code on the node server side . – Deadpool_er Feb 16 '23 at 06:09
  • Why are you using `cy.intercept()` on it? (Assuming `endpoint/B` is what you refer to in comments as second call) – Rab.Scarabelli Feb 16 '23 at 06:11
  • I do not have any idea how to intercept that second call and so tried using cy.intercept but that clearly did not work for me. So looking if there is any way to achieve this ? – Deadpool_er Feb 16 '23 at 06:17
  • 1
    As you found out, the intercept command only works on calls issued from the browser. If you want to test the server features, most likely it's going to take a unit test. You can mock responses (in this case mocking endpoint/A) if all you want to do is get passed the 2nd call and receive a particular response in the browser. – Rab.Scarabelli Feb 16 '23 at 06:26

1 Answers1

2

This is probably just because you have use https:// against localhost, but it should be http://.

Since the mini-match is pure pattern matching, there would not be any auto-correction for this type of error.

You can also just wildcard it

 cy.intercept('POST', '**/endpoint/B', (req) => {
W.Moura
  • 31
  • 3
  • thanks, i tried with and without http:// and still its same. Just to note the second call that I am calling is a third party rest API and happens on node server. – Deadpool_er Feb 16 '23 at 05:48