cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.

Home Page:https://cypress.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cy.visit does not set Referer header

beezital opened this issue · comments

Current behavior

cy.visit sets the headers > Referer to the url instead of using the value provided in the options

image

Desired behavior

image

Test code to reproduce

    it.only(`simulates jump from google search`, () => {
        // https://stackoverflow.com/questions/55492585/how-do-i-set-http-referer
        cy.visit({
            url: 'https://www.cypress.io',
            method: 'GET',
            headers: {
                'Referer': 'https://google.com/'
            }
        })
    })

Versions

7.5.0

Still not working with version 8.1.0 :-(

    it.only(`simulates jump from google search`, () => {
        // https://stackoverflow.com/questions/55492585/how-do-i-set-http-referer
        cy.visit({
            url: 'https://www.cypress.io',
            method: 'GET',
            headers: {
                'Referer': 'https://google.com/'
            }
        })
        cy.expect('Referer: https://www.cypress.io/__/').to.equal('Referer: https://google.com/')
    })

hi @beezital

we ran into this issue recently and came up with a workaround. hope this helps while we wait on a fix

  it('visits google and clicks on link to browse/dresses', () => {
    const linkText = 'Testing SEO Attribution';

    // unable to set referrer header on a cy.visit()
    // see https://github.com/cypress-io/cypress/issues/4295
    // so instead load up google, insert an anchor to our server
    // and click on that.
    cy.window()
      .then((win) => {
        win.open('https://www.google.com/', '_self');
      })
      .then((win) => {
        const createLink = win.document.createElement('a');
        const baseUrl = Cypress.config().baseUrl;
        createLink.setAttribute('href', baseUrl);
        createLink.innerHTML = linkText;
        win.document.body.appendChild(createLink);
      });
    cy.get('a')
      .contains(linkText)
      .click()
      .then(() => {
        cy.getAppComponent();

        cy.getCookie(cookieNames.attribution).then((cookie) => {
          cy.checkAttributionCookie(cookie, 'seo');
        });
      });
  });

Dear @nghedia
Thank you very much for your workaround.
I could successfully apply it to my tests.
Kudos to you and your team for coming up with this workaround and for sharing it!

Still expecting a solution using the cy.visit() approach...

Facing the same issue using Cypress 8.3.0

Same issue in Cypress 10.0.0

Same issue in Cypress 12.5.1

hi @beezital
Here is something how we managed to get it working using cypress 12.11.0

it('should visit with referrer', () => {
    const onBeforeLoad = (contentWindow) => {
      Object.defineProperty(contentWindow.document, 'referrer', {
        value: 'http://test.com/',
        enumerable: true,
        configurable: true,
      });
    };

    cy.visit('/', { onBeforeLoad });
    . . .
})

This is because of a fix with hardcoded value
https://github.com/cypress-io/cypress/blob/236b1816667720e1f0295699a148b84c25d7c04f/packages/driver/src/cy/commands/navigation.ts#LL830C4-L830C4

@dravnjakunite, thanks for posting a workaround! That's very useful.

I replicated this issue and will forward this ticket to the appropriate team. They will evaluate the priority of this ticket and consider their capacity to pick it up. Please note that this does not guarantee that this issue will be resolved.

The solutions in this thread did not work for us. We were specifically having an issue with Google's Address Validation API. This is the solution that ended up working for us.

  cy.intercept('POST', 'https://addressvalidation.googleapis.com/**', (req) => {
    req.headers['Referer'] = window.location.href;
  });

This only fails with Chromium browsers.
It'd be really cool to not have to need a workaround.