Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How to stub an incoming http request (response) with cypress

How can I stub a response of a HTTP request?

Let me explain it with my code I have now:

Cypress.Commands.add("FakeLoginWithMsal", (userId) => {
   
    cy.intercept('**/oauth2/v2.0/token', (req) => {
        
        req.reply({
            token_type: "Bearer",
            expires_in: 3795,
            access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
            })

        req.continue((res) => {

        })
    })

With this code I am trying to stub the response for the following request:

enter image description here

But it still gives the following error, where I can understand the stub did not work:

We attempted to make an http request to this URL but the request failed without a response.

I've tried already different intercept methods of cypress but I couldn't get worked.


I even can't intercept the /token endpoint with the following:

   cy.intercept({
    method: 'POST',
    url: 'https://login.microsoftonline.com/2ba13024-a5f6-4b30-afa8-d673b5166d23/oauth2/v2.0/token',
}).as('apiCheck')

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm not sure without seeing the whole test, but are you are issuing the POST to microsoftonline from within the test using cy.request()?

If so, you can't use cy.intercept() to catch it, only requests from the app will be caught.

But you can append a .then() to the cy.request() to wait for the response.

cy.request({
  method: 'POST',
  url: 'https://login.microsoftonline.com/.../oauth2/v2.0/token',
})
.then(response => {
  // handle response
  
})

Also in this code req.reply() and req.continue() you are both stubbing (with reply) and continuing to the server (with continue), which are opposite actions. You would only want to do one or the other.

cy.intercept('**/oauth2/v2.0/token', (req) => {
        
  req.reply({
    token_type: "Bearer",
    expires_in: 3795,
    access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJS"
  })

  req.continue((res) => {

  })
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda