0

I want to test API endpoint using cypress and want do mocking for AUTH token because it's coming from another API

cy.intercept({
method:'get',
url:'/first endpoint'
},response).as('mocktoken');

cy.request({
method:'get',
url:'/firstendpoint'}).then(response=>{
 cy.request({
method:'get',
url:'/secondendpoint'}).then(response=>{ // assertion statement});

Thhis is not working what can be done to implement this scenario

First, i want auth token from first which I am, trying to mock then using this token I will get the response from second endpoint

Constance
  • 283
  • 1
  • 8
  • Cypress will not intercept any requests sent via `cy.request`. https://docs.cypress.io/api/commands/intercept#Verifying-the-request-modification -> `cy.intercept() cannot be debugged using cy.request()! Cypress only intercepts requests made by your front-end application.` – agoff Dec 06 '22 at 15:24
  • What are you testing if you mock the request to get a auth token when could you not set the data instead? – jjhelguero Dec 06 '22 at 16:27

1 Answers1

1

Don't use cy.intercpet() in this case, it's for intercepting web page requests not test requests.

This is how you can do it most simply

cy.request('/firstendpoint')
  .then(response1 => {
    cy.request({
      method:'get',
      url:'/secondendpoint',
      body: {
        token: response1.token    // check where the token is in the response
      }
    })
    .then(response2 => { 
      ...
    })
  })

There is no mocking, you are using the real thing!

Constance
  • 283
  • 1
  • 8