cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.77k stars 3.16k forks source link

getCookie(s) has incorrect default for path. #26876

Open shipdapp opened 1 year ago

shipdapp commented 1 year ago

Current behavior

When testing a NextJS API route that sets a cookie without explicitly setting a path the getCookie(s) function(s) return a cookie with the path set to "/".

Desired behavior

I'd expected the path property to be null or undefined. The reason being that without an explicit path the browser will assume the path to be the current path (in my case /api/sign-in) not /.

Test code to reproduce

Make a request to any api route that sets a cookie without an explicit path and run the following assertion

cy.get('@response').getCookie('session').should('have.property', 'path', '/');

It will return true as getCookie adds the path=/ but in the browser path will actually be the path of the request.

Cypress Version

12.9.0

Node version

18.13.0

Operating System

MacOS 13.3

Debug Logs

No response

Other

No response

mike-plummer commented 1 year ago

Hey @shipdapp , thanks for using Cypress and letting us know about this. Sorry for the delay 😬

I ran your scenario past the team and they agree this is likely an oversight in our cookie handling logic - I'll go ahead and send this through to get worked. I can't guarantee any timeframe for when the team will be able to prioritize it; as always we would also welcome any community contributions to improve our cookie handling in this scenario. I believe this is the line at fault which would need to be updated to properly account for various browser behaviors and add appropriate tests