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.visit does not set Referer header #16975

Open
beezital opened this issue Jun 17, 2021 · 8 comments
Open

cy.visit does not set Referer header #16975

beezital opened this issue Jun 17, 2021 · 8 comments
Labels
E2E Issue related to end-to-end testing topic: visit Problems with cy.visit command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: bug

Comments

@beezital
Copy link

beezital commented Jun 17, 2021

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

@beezital
Copy link
Author

beezital commented Aug 2, 2021

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/')
    })

@nghedia
Copy link

nghedia commented Aug 2, 2021

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');
        });
      });
  });

@beezital
Copy link
Author

beezital commented Aug 6, 2021

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...

@dcmariotti
Copy link

Facing the same issue using Cypress 8.3.0

@sparkalow
Copy link

Same issue in Cypress 10.0.0

@townxelliot
Copy link

Same issue in Cypress 12.5.1

@dravnjakunite
Copy link

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

@chrisbreiding
Copy link
Contributor

@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.

@chrisbreiding chrisbreiding added topic: visit Problems with cy.visit command E2E Issue related to end-to-end testing Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: bug labels May 16, 2023
@chrisbreiding chrisbreiding removed their assignment May 16, 2023
jeverling added a commit to FEWS-NET/cypress that referenced this issue Mar 9, 2024
Empty referer is leading to CSRF problems in a Django page running with HTTPS.
See cypress-io#16975 and https://code.djangoproject.com/ticket/16870
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E2E Issue related to end-to-end testing topic: visit Problems with cy.visit command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: bug
Projects
None yet
Development

No branches or pull requests

7 participants