Skip to content

Commit a1161c9

Browse files
fix: hanging on basic auth requests in chromium browsers (#27781)
1 parent 70248ab commit a1161c9

File tree

5 files changed

+103
-7
lines changed

5 files changed

+103
-7
lines changed

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _Released 09/12/2023 (PENDING)_
1111

1212
- Edge cases where `cy.intercept()` would not properly intercept and asset response bodies would not properly be captured for test replay have been addressed. Addressed in [#27771](https://github.com/cypress-io/cypress/issues/27771).
1313
- Fixed an issue where `enter`, `keyup`, and `space` events where not triggering `click` events properly in some versions of Firefox. Addressed in [#27715](https://github.com/cypress-io/cypress/pull/27715).
14+
- Fixed a regression in `13.0.0` where tests using Basic Authorization can potentially hang indefinitely on chromium browsers. Addressed in [#27781](https://github.com/cypress-io/cypress/pull/27781)
1415

1516
**Dependency Updates:**
1617

packages/socket/lib/browser.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@ import type { SocketShape } from './types'
44

55
export type { Socket } from 'socket.io-client'
66

7-
const sockets: {[key: string]: CDPBrowserSocket} = {}
7+
declare global {
8+
interface Window {
9+
cypressSockets: {[key: string]: CDPBrowserSocket}
10+
}
11+
}
12+
813
let chromium = false
914

1015
export function client (uri: string, opts?: Partial<ManagerOptions & SocketOptions>): SocketShape {
1116
if (chromium) {
1217
const fullNamespace = `${opts?.path}${uri}`
1318

14-
if (!sockets[fullNamespace]) {
15-
sockets[fullNamespace] = new CDPBrowserSocket(fullNamespace)
19+
// When running in Chromium and with a baseUrl set to something that includes basic auth: (e.g. http://user:pass@localhost:1234), the assets
20+
// will load twice. Thus, we need to add the cypress sockets to the window object rather than just relying on a local variable.
21+
window.cypressSockets ||= {}
22+
if (!window.cypressSockets[fullNamespace]) {
23+
window.cypressSockets[fullNamespace] = new CDPBrowserSocket(fullNamespace)
1624
}
1725

18-
return sockets[fullNamespace]
26+
// Connect the socket regardless of whether or not we have newly created it
27+
window.cypressSockets[fullNamespace].connect()
28+
29+
return window.cypressSockets[fullNamespace]
1930
}
2031

2132
return io(uri, opts)
@@ -27,11 +38,17 @@ export function createWebsocket ({ path, browserFamily }: { path: string, browse
2738

2839
const fullNamespace = `${path}/default`
2940

30-
if (!sockets[fullNamespace]) {
31-
sockets[fullNamespace] = new CDPBrowserSocket(fullNamespace)
41+
// When running in Chromium and with a baseUrl set to something that includes basic auth: (e.g. http://user:pass@localhost:1234), the assets
42+
// will load twice. Thus, we need to add the cypress sockets to the window object rather than just relying on a local variable.
43+
window.cypressSockets ||= {}
44+
if (!window.cypressSockets[fullNamespace]) {
45+
window.cypressSockets[fullNamespace] = new CDPBrowserSocket(fullNamespace)
3246
}
3347

34-
return sockets[fullNamespace]
48+
// Connect the socket regardless of whether or not we have newly created it
49+
window.cypressSockets[fullNamespace].connect()
50+
51+
return window.cypressSockets[fullNamespace]
3552
}
3653

3754
return io({

packages/socket/lib/cdp-browser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export class CDPBrowserSocket extends Emitter implements SocketShape {
4343
if (!cypressSocket.send) {
4444
cypressSocket.send = send
4545
}
46+
}
4647

48+
connect () {
4749
// Set timeout so that the connect event is emitted after the constructor returns and the user has a chance to attach a listener
4850
setTimeout(() => {
4951
super.emit('connect')

system-tests/__snapshots__/base_url_spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,58 @@ exports['e2e baseUrl / http / passes'] = `
107107
108108
109109
`
110+
111+
exports['e2e baseUrl / https basic auth / passes'] = `
112+
113+
====================================================================================================
114+
115+
(Run Starting)
116+
117+
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
118+
│ Cypress: 1.2.3 │
119+
│ Browser: FooBrowser 88 │
120+
│ Specs: 1 found (base_url.cy.js) │
121+
│ Searched: cypress/e2e/base_url.cy.js │
122+
└────────────────────────────────────────────────────────────────────────────────────────────────┘
123+
124+
125+
────────────────────────────────────────────────────────────────────────────────────────────────────
126+
127+
Running: base_url.cy.js (1 of 1)
128+
129+
130+
base url
131+
✓ can visit
132+
133+
134+
1 passing
135+
136+
137+
(Results)
138+
139+
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
140+
│ Tests: 1 │
141+
│ Passing: 1 │
142+
│ Failing: 0 │
143+
│ Pending: 0 │
144+
│ Skipped: 0 │
145+
│ Screenshots: 0 │
146+
│ Video: false │
147+
│ Duration: X seconds │
148+
│ Spec Ran: base_url.cy.js │
149+
└────────────────────────────────────────────────────────────────────────────────────────────────┘
150+
151+
152+
====================================================================================================
153+
154+
(Run Finished)
155+
156+
157+
Spec Tests Passing Failing Pending Skipped
158+
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
159+
│ ✔ base_url.cy.js XX:XX 1 1 - - - │
160+
└────────────────────────────────────────────────────────────────────────────────────────────────┘
161+
✔ All specs passed! XX:XX 1 1 - - -
162+
163+
164+
`

system-tests/test/base_url_spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ describe('e2e baseUrl', () => {
2727
})
2828
})
2929

30+
context('https basic auth', () => {
31+
systemTests.setup({
32+
servers: {
33+
port: 443,
34+
https: true,
35+
onServer,
36+
},
37+
settings: {
38+
e2e: {
39+
baseUrl: 'https://test:test@localhost/app',
40+
},
41+
},
42+
})
43+
44+
systemTests.it('passes', {
45+
spec: 'base_url.cy.js',
46+
browser: 'chrome',
47+
snapshot: true,
48+
})
49+
})
50+
3051
context('http', () => {
3152
systemTests.setup({
3253
servers: {

0 commit comments

Comments
 (0)