Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cy.route2() doesn't work when method is used as the first parameter #8729

Closed
bhgsbatista opened this issue Oct 1, 2020 · 8 comments · Fixed by #8829
Closed

cy.route2() doesn't work when method is used as the first parameter #8729

bhgsbatista opened this issue Oct 1, 2020 · 8 comments · Fixed by #8829

Comments

@bhgsbatista
Copy link

bhgsbatista commented Oct 1, 2020

Current behavior

When passing POST as the first parameter to route2 requests are not properly intercepted.

// Does not work
cy.route2('POST', `${BasePage.api_url}/events/createTemplate`).as('create_template')
cy.wait('@create_template')

// Works
cy.route2(`${BasePage.api_url}/events/createTemplate`).as('create_template')
cy.wait('@create_template')

Desired behavior

Using the method parameter should work.

Test code to reproduce

https://github.com/bhgsbatista/cypress-test-tiny/tree/route2_method_fail

Versions

Cypress v5.3.0
Windows 10

@bhgsbatista bhgsbatista changed the title cy.route2 doesn't work when defining method as the first parameter cy.route2() doesn't work when method is used as the first parameter Oct 1, 2020
@JonahKK
Copy link

JonahKK commented Oct 2, 2020

If you want to set the method, without altering the response, you need to do it in following way:

cy.route2({ method: 'PUT', pathname: '**/api' }).as('api');

See https://docs.cypress.io/api/commands/route2.html#Usage

@bhgsbatista
Copy link
Author

That is not at all reflected in the documentation, that same link you shared has this snippet of code:

cy.route2(url, routeHandler?)
cy.route2(method, url, routeHandler?)
cy.route2(routeMatcher, routeHandler?)

Below that there's an example using the method argument without altering the response:

cy.route2('POST', '/organization', (req) => {
  expect(req.body).to.include('Acme Company')
})

https://docs.cypress.io/api/commands/route2.html#Intercepting-a-request

--

There is definitely an issue here, either with the implementation, or with the documentation.

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Oct 7, 2020

It looks like the route for the one with POST method is reading the POST as the url argument. I would agree that this is a bug (assuming the documentation is correct).

Looking at the code, it looks like it is always expecting a 3rd argument - the routeMatcher - and won't work without that. Although this contradicts the documentation which says the 3rd arg is optional.

https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/net-stubbing/add-command.ts#L249:L249

Screen Shot 2020-10-07 at 1 29 03 PM copy

it('works', () => {
  cy.route2('http://dummy.restapiexample.com/api/v1/create').as('create')

  cy.window().then((win) => {
    win.eval(
      `fetch("http://dummy.restapiexample.com/api/v1/create", {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
      });`
    )
  })

  cy.wait('@create')
})

it('should work', () => {
  cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create').as('create')

  cy.window().then((win) => {
    win.eval(
      `fetch("http://dummy.restapiexample.com/api/v1/create", {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
      });`
    )
  })

  cy.wait('@create') // wait times out
})

Current Workaround

Pass a 3rd arg until this is fixed / clarified. Just passing an empty object will make it work.

cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create', {}).as('create')

@sainthkh
Copy link
Contributor

sainthkh commented Oct 12, 2020

Problem

This is happening because there is an ambiguity in the definition of route2:

In the case above, the OP is using cy.route2(string, string).

But it can be 2 calls:

  • cy.route2(url: string, routeHandler: string)
  • cy.route2(method: string, url: string, routeHandler: undefined)

The OP tries to use the second definition, but Cypress interpreted it as the first.

Solution?

Currently, we're only checking if the first arg is string or not. This ambiguity can be solved by checking if the first arg is an HTTP request method or a WebDAV request method.

What do you think, @flotwig?

@flotwig
Copy link
Contributor

flotwig commented Oct 13, 2020

Currently, we're only checking if the first arg is string or not. This ambiguity can be solved by checking if the first arg is an HTTP request method or a WebDAV request method.

@sainthkh yeah, I think that would be a perfect fix, doing a case-insensitive check against known HTTP methods for the first string parameter. Do you want to pick this up?

@sainthkh
Copy link
Contributor

I do. I'll get it done today. Maybe you'll be reviewing the PR next morning 😄 .

@cypress-bot cypress-bot bot added stage: work in progress There is an open PR for this issue [WIP] and removed stage: ready for work The issue is reproducible and in scope labels Oct 14, 2020
@flotwig flotwig assigned sainthkh and unassigned flotwig Oct 14, 2020
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress There is an open PR for this issue [WIP] labels Oct 19, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 19, 2020

The code for this is done in cypress-io/cypress#8829, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot added stage: pending release There is a closed PR for this issue and removed stage: needs review The PR code is done & tested, needs review labels Oct 19, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 27, 2020

Released in 5.5.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.5.0, please open a new issue.

@cypress-bot cypress-bot bot removed the stage: pending release There is a closed PR for this issue label Oct 27, 2020
@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Oct 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants