Skip to content

Commit c832235

Browse files
authored
Merge branch 'feature-multidomain' into mo-isolate-multi-domain-tests
2 parents 7990696 + d7ce865 commit c832235

File tree

18 files changed

+371
-219
lines changed

18 files changed

+371
-219
lines changed

packages/driver/cypress/integration/commands/navigation_spec.js

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,10 +1415,10 @@ describe('src/cy/commands/navigation', () => {
14151415
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
14161416
${experimentalMessage}
14171417
\`cy.visit('http://localhost:3500/fixtures/generic.html')\`
1418-
\`<other commands targeting http://localhost:3500 go here>\`\n
1418+
\`<commands targeting http://localhost:3500 go here>\`\n
14191419
\`cy.origin('http://localhost:3501', () => {\`
14201420
\` cy.visit('http://localhost:3501/fixtures/generic.html')\`
1421-
\` <other commands targeting http://localhost:3501 go here>\`
1421+
\` <commands targeting http://localhost:3501 go here>\`
14221422
\`})\`\n
14231423
The new URL is considered a different origin because the following parts of the URL are different:\n
14241424
> port\n
@@ -1448,18 +1448,18 @@ describe('src/cy/commands/navigation', () => {
14481448
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
14491449
${experimentalMessage}
14501450
\`cy.visit('http://localhost:3500/fixtures/generic.html')\`
1451-
\`<other commands targeting http://localhost:3500 go here>\`\n
1452-
\`cy.origin('https://localhost:3500', () => {\`
1453-
\` cy.visit('https://localhost:3500/fixtures/generic.html')\`
1454-
\` <other commands targeting https://localhost:3500 go here>\`
1451+
\`<commands targeting http://localhost:3500 go here>\`\n
1452+
\`cy.origin('https://localhost:3502', () => {\`
1453+
\` cy.visit('https://localhost:3502/fixtures/generic.html')\`
1454+
\` <commands targeting https://localhost:3502 go here>\`
14551455
\`})\`\n
14561456
The new URL is considered a different origin because the following parts of the URL are different:\n
1457-
> protocol\n
1457+
> protocol, port\n
14581458
You may only \`cy.visit()\` same-origin URLs within a single test.\n
14591459
The previous URL you visited was:\n
14601460
> 'http://localhost:3500'\n
14611461
You're attempting to visit this URL:\n
1462-
> 'https://localhost:3500'`)
1462+
> 'https://localhost:3502'`)
14631463

14641464
expect(err.docsUrl).to.eq('https://on.cypress.io/cannot-visit-different-origin-domain')
14651465
assertLogLength(this.logs, 2)
@@ -1469,7 +1469,7 @@ describe('src/cy/commands/navigation', () => {
14691469
})
14701470

14711471
cy.visit('http://localhost:3500/fixtures/generic.html')
1472-
cy.visit('https://localhost:3500/fixtures/generic.html')
1472+
cy.visit('https://localhost:3502/fixtures/generic.html')
14731473
})
14741474

14751475
it('throws when attempting to visit a 2nd domain on different superdomain', function (done) {
@@ -1481,18 +1481,18 @@ describe('src/cy/commands/navigation', () => {
14811481
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
14821482
${experimentalMessage}
14831483
\`cy.visit('http://localhost:3500/fixtures/generic.html')\`
1484-
\`<other commands targeting http://localhost:3500 go here>\`\n
1485-
\`cy.origin('http://google.com:3500', () => {\`
1486-
\` cy.visit('http://google.com:3500/fixtures/generic.html')\`
1487-
\` <other commands targeting http://google.com:3500 go here>\`
1484+
\`<commands targeting http://localhost:3500 go here>\`\n
1485+
\`cy.origin('http://foobar.com:3500', () => {\`
1486+
\` cy.visit('http://www.foobar.com:3500/fixtures/generic.html')\`
1487+
\` <commands targeting http://www.foobar.com:3500 go here>\`
14881488
\`})\`\n
14891489
The new URL is considered a different origin because the following parts of the URL are different:\n
14901490
> superdomain\n
14911491
You may only \`cy.visit()\` same-origin URLs within a single test.\n
14921492
The previous URL you visited was:\n
14931493
> 'http://localhost:3500'\n
14941494
You're attempting to visit this URL:\n
1495-
> 'http://google.com:3500'`)
1495+
> 'http://www.foobar.com:3500'`)
14961496

14971497
expect(err.docsUrl).to.eq('https://on.cypress.io/cannot-visit-different-origin-domain')
14981498
assertLogLength(this.logs, 2)
@@ -1502,7 +1502,7 @@ describe('src/cy/commands/navigation', () => {
15021502
})
15031503

15041504
cy.visit('http://localhost:3500/fixtures/generic.html')
1505-
cy.visit('http://google.com:3500/fixtures/generic.html')
1505+
cy.visit('http://www.foobar.com:3500/fixtures/generic.html')
15061506
})
15071507

15081508
it('throws attempting to visit 2 unique ip addresses', function (done) {
@@ -1514,18 +1514,18 @@ describe('src/cy/commands/navigation', () => {
15141514
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
15151515
${experimentalMessage}
15161516
\`cy.visit('http://127.0.0.1:3500/fixtures/generic.html')\`
1517-
\`<other commands targeting http://127.0.0.1:3500 go here>\`\n
1518-
\`cy.origin('http://126.0.0.1:3500', () => {\`
1519-
\` cy.visit('http://126.0.0.1:3500/fixtures/generic.html')\`
1520-
\` <other commands targeting http://126.0.0.1:3500 go here>\`
1517+
\`<commands targeting http://127.0.0.1:3500 go here>\`\n
1518+
\`cy.origin('http://0.0.0.0:3500', () => {\`
1519+
\` cy.visit('http://0.0.0.0:3500/fixtures/generic.html')\`
1520+
\` <commands targeting http://0.0.0.0:3500 go here>\`
15211521
\`})\`\n
15221522
The new URL is considered a different origin because the following parts of the URL are different:\n
15231523
> superdomain\n
15241524
You may only \`cy.visit()\` same-origin URLs within a single test.\n
15251525
The previous URL you visited was:\n
15261526
> 'http://127.0.0.1:3500'\n
15271527
You're attempting to visit this URL:\n
1528-
> 'http://126.0.0.1:3500'`)
1528+
> 'http://0.0.0.0:3500'`)
15291529

15301530
expect(err.docsUrl).to.eq('https://on.cypress.io/cannot-visit-different-origin-domain')
15311531
assertLogLength(this.logs, 2)
@@ -1536,22 +1536,7 @@ describe('src/cy/commands/navigation', () => {
15361536

15371537
cy
15381538
.visit('http://127.0.0.1:3500/fixtures/generic.html')
1539-
.visit('http://126.0.0.1:3500/fixtures/generic.html')
1540-
})
1541-
1542-
it('does not call resolve:url when throws attempting to visit a 2nd domain', (done) => {
1543-
const backend = cy.spy(Cypress, 'backend')
1544-
1545-
cy.on('fail', (err) => {
1546-
expect(backend).to.be.calledWithMatch('resolve:url', 'http://localhost:3500/fixtures/generic.html')
1547-
expect(backend).not.to.be.calledWithMatch('resolve:url', 'http://google.com:3500/fixtures/generic.html')
1548-
1549-
done()
1550-
})
1551-
1552-
cy
1553-
.visit('http://localhost:3500/fixtures/generic.html')
1554-
.visit('http://google.com:3500/fixtures/generic.html')
1539+
.visit('http://0.0.0.0:3500/fixtures/generic.html')
15551540
})
15561541

15571542
it('displays loading_network_failed when _resolveUrl throws', function (done) {

packages/driver/cypress/integration/e2e/multi-domain/commands/multi_domain_navigation.spec.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ context('cy.origin navigation', () => {
178178
You likely forgot to use \`cy.origin()\`:\n
179179
\`cy.origin('http://foobar.com:3500', () => {\`
180180
\` cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')\`
181-
\` <other commands targeting http://www.foobar.com:3500 go here>\`
181+
\` <commands targeting http://www.foobar.com:3500 go here>\`
182182
\`})\`\n
183183
\`cy.origin('http://idp.com:3500', () => {\`
184184
\` cy.visit('http://www.idp.com:3500/fixtures/dom.html')\`
185-
\` <other commands targeting http://www.idp.com:3500 go here>\`
185+
\` <commands targeting http://www.idp.com:3500 go here>\`
186186
\`})\`\n
187187
The new URL is considered a different origin because the following parts of the URL are different:\n
188188
> superdomain\n
@@ -211,10 +211,10 @@ context('cy.origin navigation', () => {
211211
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
212212
In order to visit a different origin, you can enable the \`experimentalSessionAndOrigin\` flag and use \`cy.origin()\`:\n
213213
\`cy.visit('http://localhost:3500/fixtures/multi-domain.html')\`
214-
\`<other commands targeting http://localhost:3500 go here>\`\n
214+
\`<commands targeting http://localhost:3500 go here>\`\n
215215
\`cy.origin('http://foobar.com:3500', () => {\`
216216
\` cy.visit('http://www.foobar.com:3500/fixtures/dom.html')\`
217-
\` <other commands targeting http://www.foobar.com:3500 go here>\`
217+
\` <commands targeting http://www.foobar.com:3500 go here>\`
218218
\`})\`\n
219219
The new URL is considered a different origin because the following parts of the URL are different:\n
220220
> superdomain\n
@@ -312,7 +312,7 @@ context('cy.origin navigation', () => {
312312
})
313313
})
314314

315-
it('supports visit redirects', () => {
315+
it('supports redirecting from primary to secondary in cy.origin', () => {
316316
cy.visit('/fixtures/multi-domain.html')
317317

318318
cy.origin('http://www.foobar.com:3500', () => {
@@ -321,6 +321,79 @@ context('cy.origin navigation', () => {
321321
})
322322
})
323323

324+
it('supports redirecting from secondary to primary outside of cy.origin', () => {
325+
cy.visit('/fixtures/multi-domain.html')
326+
cy.visit('http://www.foobar.com:3500/redirect?href=http://localhost:3500/fixtures/generic.html')
327+
})
328+
329+
it('errors when trying to redirect from secondary to primary in cy.origin', (done) => {
330+
cy.on('fail', (e) => {
331+
expect(e.message).to.equal(stripIndent`
332+
\`cy.visit()\` failed because you are attempting to visit a URL from a previous origin inside of \`cy.origin()\`.\n
333+
Instead of placing the \`cy.visit()\` inside of \`cy.origin()\`, the \`cy.visit()\` should be placed outside of the \`cy.origin()\` block.\n
334+
\`<commands targeting http://localhost:3500 go here>\`\n
335+
\`cy.origin('http://foobar.com:3500', () => {\`
336+
\` <commands targeting http://foobar.com:3500 go here>\`
337+
\`})\`\n
338+
\`cy.visit('http://www.foobar.com:3500/redirect?href=http://localhost:3500/fixtures/generic.html')\``)
339+
340+
done()
341+
})
342+
343+
cy.visit('http://localhost:3500/fixtures/multi-domain.html')
344+
345+
cy.origin('http://www.foobar.com:3500', () => {
346+
cy.visit('/redirect?href=http://localhost:3500/fixtures/generic.html')
347+
})
348+
})
349+
350+
it('errors when trying to visit primary in cy.origin', (done) => {
351+
cy.on('fail', (e) => {
352+
expect(e.message).to.equal(stripIndent`
353+
\`cy.visit()\` failed because you are attempting to visit a URL from a previous origin inside of \`cy.origin()\`.\n
354+
Instead of placing the \`cy.visit()\` inside of \`cy.origin()\`, the \`cy.visit()\` should be placed outside of the \`cy.origin()\` block.\n
355+
\`<commands targeting http://localhost:3500 go here>\`\n
356+
\`cy.origin('http://foobar.com:3500', () => {\`
357+
\` <commands targeting http://foobar.com:3500 go here>\`
358+
\`})\`\n
359+
\`cy.visit('http://localhost:3500/fixtures/generic.html')\``)
360+
361+
done()
362+
})
363+
364+
cy.visit('http://localhost:3500/fixtures/multi-domain.html')
365+
366+
cy.origin('http://www.foobar.com:3500', () => {
367+
cy.visit('http://localhost:3500/fixtures/generic.html')
368+
})
369+
})
370+
371+
it('errors when trying to redirect from primary to secondary outside of cy.origin', (done) => {
372+
cy.on('fail', (e) => {
373+
expect(e.message).to.equal(stripIndent`\
374+
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
375+
You likely forgot to use \`cy.origin()\`:\n
376+
\`cy.visit('http://localhost:3500/fixtures/multi-domain.html')\`
377+
\`<commands targeting http://localhost:3500 go here>\`\n
378+
\`cy.origin('http://foobar.com:3500', () => {\`
379+
\` cy.visit('http://localhost:3500/redirect?href=http://www.foobar.com:3500/fixtures/generic.html')\`
380+
\` <commands targeting http://www.foobar.com:3500 go here>\`
381+
\`})\`\n
382+
The new URL is considered a different origin because the following parts of the URL are different:\n
383+
> superdomain\n
384+
You may only \`cy.visit()\` same-origin URLs within a single test.\n
385+
The previous URL you visited was:\n
386+
> 'http://localhost:3500'\n
387+
You're attempting to visit this URL:\n
388+
> 'http://www.foobar.com:3500'`)
389+
390+
done()
391+
})
392+
393+
cy.visit('/fixtures/multi-domain.html')
394+
cy.visit('http://localhost:3500/redirect?href=http://www.foobar.com:3500/fixtures/generic.html')
395+
})
396+
324397
it('supports auth options and adding auth to subsequent requests', () => {
325398
cy.origin('http://foobar.com:3500', () => {
326399
cy.visit('http://www.foobar.com:3500/basic_auth', {

packages/driver/src/cy/commands/navigation.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const timedOutWaitingForPageLoad = (ms, log) => {
9090
}
9191
}
9292

93-
const cannotVisitDifferentOrigin = ({ remote, existing, previousUrlVisited, log, isCrossOriginSpecBridge = false }) => {
93+
const cannotVisitDifferentOrigin = ({ remote, existing, originalUrl, previousUrlVisited, log, isCrossOriginSpecBridge = false }) => {
9494
const differences: string[] = []
9595

9696
if (remote.protocol !== existing.protocol) {
@@ -111,6 +111,7 @@ const cannotVisitDifferentOrigin = ({ remote, existing, previousUrlVisited, log,
111111
differences: differences.join(', '),
112112
previousUrl: previousUrlVisited,
113113
attemptedUrl: remote,
114+
originalUrl,
114115
isCrossOriginSpecBridge,
115116
experimentalSessionAndOrigin: Cypress.config('experimentalSessionAndOrigin'),
116117
},
@@ -122,6 +123,22 @@ const cannotVisitDifferentOrigin = ({ remote, existing, previousUrlVisited, log,
122123
$errUtils.throwErrByPath('visit.cannot_visit_different_origin', errOpts)
123124
}
124125

126+
const cannotVisitPreviousOrigin = ({ remote, originalUrl, previousUrlVisited, log }) => {
127+
const errOpts = {
128+
onFail: log,
129+
args: {
130+
attemptedUrl: remote,
131+
previousUrl: previousUrlVisited,
132+
originalUrl,
133+
},
134+
errProps: {
135+
isCrossOrigin: true,
136+
},
137+
}
138+
139+
$errUtils.throwErrByPath('origin.cannot_visit_previous_origin', errOpts)
140+
}
141+
125142
const specifyFileByRelativePath = (url, log) => {
126143
$errUtils.throwErrByPath('visit.specify_file_by_relative_path', {
127144
onFail: log,
@@ -494,6 +511,7 @@ const normalizeOptions = (options) => {
494511
.extend({
495512
timeout: options.responseTimeout,
496513
isCrossOrigin: Cypress.isCrossOriginSpecBridge,
514+
hasAlreadyVisitedUrl: options.hasAlreadyVisitedUrl,
497515
})
498516
.value()
499517
}
@@ -833,6 +851,8 @@ export default (Commands, Cypress, cy, state, config) => {
833851
onLoad () {},
834852
})
835853

854+
options.hasAlreadyVisitedUrl = !!previousUrlVisited
855+
836856
if (!_.isUndefined(options.qs) && !_.isObject(options.qs)) {
837857
$errUtils.throwErrByPath('visit.invalid_qs', { args: { qs: String(options.qs) } })
838858
}
@@ -1026,17 +1046,6 @@ export default (Commands, Cypress, cy, state, config) => {
10261046
const existingHash = remote.hash || ''
10271047
const existingAuth = remote.auth || ''
10281048

1029-
if (previousUrlVisited && (remote.originPolicy !== existing.originPolicy)) {
1030-
// if we've already visited a new superDomain
1031-
// then die else we'd be in a terrible endless loop
1032-
// we also need to disable retries to prevent the endless loop
1033-
$utils.getTestFromRunnable(state('runnable'))._retries = 0
1034-
1035-
const params = { remote, existing, previousUrlVisited, log: options._log }
1036-
1037-
return cannotVisitDifferentOrigin(params)
1038-
}
1039-
10401049
// in a cross origin spec bridge, the window may not have been set yet if nothing has been loaded in the secondary origin,
10411050
// it's also possible for a new test to start and for a cross-origin failure to occur if the win is set but
10421051
// the AUT hasn't yet navigated to the secondary origin
@@ -1082,7 +1091,7 @@ export default (Commands, Cypress, cy, state, config) => {
10821091

10831092
return requestUrl(url, options)
10841093
.then((resp: any = {}) => {
1085-
let { url, originalUrl, cookies, redirects, filePath } = resp
1094+
let { url, originalUrl, cookies, redirects, filePath, isPrimaryOrigin } = resp
10861095

10871096
// reapply the existing hash
10881097
url += existingHash
@@ -1114,7 +1123,6 @@ export default (Commands, Cypress, cy, state, config) => {
11141123

11151124
// if the origin currently matches
11161125
// then go ahead and change the iframe's src
1117-
// and we're good to go
11181126
if (remote.originPolicy === existing.originPolicy) {
11191127
previousUrlVisited = remote
11201128

@@ -1126,22 +1134,25 @@ export default (Commands, Cypress, cy, state, config) => {
11261134
})
11271135
}
11281136

1129-
// if we are in a cross origin spec bridge and the origin policies weren't the same,
1130-
// we need to throw an error since the user tried to visit a new
1131-
// origin which isn't allowed within a cy.origin block
1132-
if (Cypress.isCrossOriginSpecBridge && win) {
1133-
const existingAutOrigin = $Location.create(win.location.href)
1134-
const params = { remote, existing, previousUrlVisited: existingAutOrigin, log: options._log, isCrossOriginSpecBridge: true }
1137+
// if we've already cy.visit'ed in the test and we are visiting a new origin,
1138+
// throw an error, else we'd be in a endless loop,
1139+
// we also need to disable retries to prevent the endless loop
1140+
if (previousUrlVisited) {
1141+
$utils.getTestFromRunnable(state('runnable'))._retries = 0
1142+
1143+
const params = { remote, existing, originalUrl, previousUrlVisited, log: options._log }
11351144

11361145
return cannotVisitDifferentOrigin(params)
11371146
}
11381147

1139-
// if we've already visited a new origin
1140-
// then die else we'd be in a terrible endless loop
1141-
if (previousUrlVisited) {
1142-
const params = { remote, existing, previousUrlVisited, log: options._log }
1148+
// if we are in a cross origin spec bridge and the origin policies weren't the same,
1149+
// we need to throw an error since the user tried to visit a new
1150+
// origin which isn't allowed within a cy.origin block
1151+
if (Cypress.isCrossOriginSpecBridge) {
1152+
const existingAutOrigin = win ? $Location.create(win.location.href) : $Location.create(Cypress.state('currentActiveOriginPolicy'))
1153+
const params = { remote, existing, originalUrl, previousUrlVisited: existingAutOrigin, log: options._log, isCrossOriginSpecBridge: true, isPrimaryOrigin }
11431154

1144-
return cannotVisitDifferentOrigin(params)
1155+
return isPrimaryOrigin ? cannotVisitPreviousOrigin(params) : cannotVisitDifferentOrigin(params)
11451156
}
11461157

11471158
// tell our backend we're changing origins

0 commit comments

Comments
 (0)