asdasd
asdasd

Reputation: 7250

simplest usecase of cy.request is not working

I have following code:

    cy.server();
    cy.route({
        method: 'GET',
        url: 'http://localhost:4200/api/login',
        response:{sadasda:2312312123}})
    cy.visit('http://localhost:4200/');
    cy.request('GET', '/api/login');

But cy.request throws 404 error. Pls see the screenshot below. What am i doing wrong?


enter image description here

Upvotes: 0

Views: 924

Answers (1)

Zach Bloomquist
Zach Bloomquist

Reputation: 5891

From the docs for cy.request:

cy.request() cannot be used to debug cy.server() and cy.route()

cy.request() sends requests to actual endpoints, bypassing those defined using cy.route()

The intention of cy.request() is to be used for checking endpoints on an actual, running server without having to start the front end application.

cy.route() is intended to be used to test your application, not to test cy.request(). Try making an XmlHttpRequest from your application that matches the cy.route() like so and it will work:

const xhr = new XmlHttpRequest()
xhr.open('GET', 'http://localhost:4200/api/login', false)
xhr.send() // now your `cy.route` will trigger

Upvotes: 1

Related Questions