Skip to content

Commit

Permalink
fix: do not set document.referrer while using cy.visit (#18658)
Browse files Browse the repository at this point in the history
Co-authored-by: David Munechika <david@cypress.io>
  • Loading branch information
2 people authored and flotwig committed Nov 8, 2021
1 parent bbb825e commit 974663d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/driver/cypress/integration/issues/4295_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://github.com/cypress-io/cypress/issues/4295
it('rewrites document.referrer on the AUT to be empty string on visit', () => {
cy.visit('http://localhost:3500/fixtures/generic.html')

cy.window().its('document').its('referrer').should('equal', '')
})

it('rewrites document.referrer on the AUT to be empty string on visit before user calls onBeforeLoad', () => {
cy.visit('http://localhost:3500/fixtures/generic.html', { onBeforeLoad: (contentWindow) => {
expect(contentWindow.document.referrer).to.equal('')
} })
})

it('does not rewrite document.referrer if navigation was triggered by click on a link', () => {
cy.visit('http://localhost:3500/fixtures/generic.html')

cy.get('#dimensions').click()

cy.window().its('document').its('referrer').should('equal', 'http://localhost:3500/fixtures/generic.html')
})
7 changes: 7 additions & 0 deletions packages/driver/src/cy/commands/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,13 @@ export default (Commands, Cypress, cy, state, config) => {

const onBeforeLoad = (contentWindow) => {
try {
// when using the visit the document referrer should be set to an empty string
Object.defineProperty(contentWindow.document, 'referrer', {
value: '',
enumerable: true,
configurable: true,
})

options.onBeforeLoad?.call(runnable.ctx, contentWindow)
} catch (err) {
err.isCallbackError = true
Expand Down

0 comments on commit 974663d

Please sign in to comment.