cypress icon indicating copy to clipboard operation
cypress copied to clipboard

baseUrl slash breaks URL when trying to append query string

Open Noyabronok opened this issue 2 years ago • 3 comments

Current behavior

baseUrl: https://..../xyz.html
cy.visit('?x=y')

Cypress tries to access http://.../xyz.html/?x=y

The extra slash after xyz.html breaks the request

Desired behavior

Do not add a slash at the end of the baseUrl if it's not needed, as in the case of query parameters

Test code to reproduce

NA

Cypress Version

12.3

Node version

14

Operating System

macOS 12.6.2

Debug Logs

No response

Other

Here's a temporary workaround for the problem that I came up with. Basically, explicitly providing the URL to the visit function, instead of relying on baseUrl, produces the desired behavior.

Cypress.Commands.overwrite(
  'visit',
  (originalFn, url: string, options: Partial<Cypress.VisitOptions>) => {
    options = options ?? {};
    // this is a workaround for a cypress bug that adds a / to the end of baseUrl which breaks the URL if it ends on .html
    // if you don't like this fix, there's another hack, which is to add some dummy query string to the baseUrl
    if (url?.startsWith('?')) {
      // terminal logging enabled by ELECTRON_ENABLE_LOGGING=1
      console.log('Setting baseUrl explicitly to remove unwanted /');
      options.qs = {
        ...options?.qs,
        ...Object.fromEntries(new URLSearchParams(url)),
      };
      url = Cypress.config().baseUrl;
    }

    return originalFn(url, options);
  }
);

Noyabronok avatar Jan 24 '23 17:01 Noyabronok

Hi @Noyabronok. Thank you for opening an issue. I was able to reproduce this behavior and am going to route this to a team. The reproduction link is here.

AtofStryker avatar Jan 27 '23 20:01 AtofStryker

I just hit this bug as well. Are there any updates?

I'm on Cypress 12.7.0

kgartland-rstudio avatar Jul 06 '23 21:07 kgartland-rstudio

Hi @kgartland-rstudio. No updates as of yet. If this work does get picked up, we will be sure to update the issue.

AtofStryker avatar Jul 07 '23 13:07 AtofStryker