Skip to content

Commit 3e9574a

Browse files
lpcoxCopilot
andauthored
refactor(test): extract TLS tunnel mock setup into beforeEach in websocket tests (#4330)
Move the duplicated socket/connectReq/tunnel/tlsSocket mock construction and jest.spyOn setup from individual test bodies into a shared beforeEach block in the 'CONNECT tunnel and auth injection' describe scope. Eliminates ~30 lines of copy-pasted mock setup across 4 test cases. Closes #4222 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b56f214 commit 3e9574a

1 file changed

Lines changed: 11 additions & 48 deletions

File tree

containers/api-proxy/server.websocket.test.js

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ describe('proxyWebSocket', () => {
106106

107107
describe('CONNECT tunnel and auth injection', () => {
108108
let wsProxy;
109+
let socket, connectReq, tunnel, tlsSocket;
109110

110111
beforeAll(() => {
111112
// Re-require server with HTTPS_PROXY so proxyWebSocket uses the proxy URL.
@@ -119,12 +120,18 @@ describe('proxyWebSocket', () => {
119120
jest.resetModules();
120121
});
121122

122-
it('returns 502 when the CONNECT response is not 200', () => {
123-
const socket = makeMockSocket();
124-
const connectReq = new EventEmitter();
123+
beforeEach(() => {
124+
socket = makeMockSocket();
125+
connectReq = new EventEmitter();
125126
connectReq.end = jest.fn();
126-
const tunnel = makeMockSocket();
127+
tunnel = makeMockSocket();
128+
tlsSocket = new EventEmitter();
129+
tlsSocket.write = jest.fn();
130+
tlsSocket.destroy = jest.fn();
131+
tlsSocket.pipe = jest.fn();
132+
});
127133

134+
it('returns 502 when the CONNECT response is not 200', () => {
128135
jest.spyOn(http, 'request').mockReturnValue(connectReq);
129136
setImmediate(() => connectReq.emit('connect', { statusCode: 407 }, tunnel));
130137

@@ -139,10 +146,6 @@ describe('proxyWebSocket', () => {
139146
});
140147

141148
it('returns 502 when the CONNECT request emits an error', () => {
142-
const socket = makeMockSocket();
143-
const connectReq = new EventEmitter();
144-
connectReq.end = jest.fn();
145-
146149
jest.spyOn(http, 'request').mockReturnValue(connectReq);
147150
setImmediate(() => connectReq.emit('error', new Error('connection refused')));
148151

@@ -156,15 +159,6 @@ describe('proxyWebSocket', () => {
156159
});
157160

158161
it('returns 502 when TLS handshake fails', () => {
159-
const socket = makeMockSocket();
160-
const connectReq = new EventEmitter();
161-
connectReq.end = jest.fn();
162-
const tunnel = makeMockSocket();
163-
const tlsSocket = new EventEmitter();
164-
tlsSocket.write = jest.fn();
165-
tlsSocket.destroy = jest.fn();
166-
tlsSocket.pipe = jest.fn();
167-
168162
jest.spyOn(http, 'request').mockReturnValue(connectReq);
169163
jest.spyOn(tls, 'connect').mockReturnValue(tlsSocket);
170164

@@ -185,15 +179,6 @@ describe('proxyWebSocket', () => {
185179
});
186180

187181
it('injects Authorization header and fixes Host header in the upgrade request', () => {
188-
const socket = makeMockSocket();
189-
const connectReq = new EventEmitter();
190-
connectReq.end = jest.fn();
191-
const tunnel = makeMockSocket();
192-
const tlsSocket = new EventEmitter();
193-
tlsSocket.write = jest.fn();
194-
tlsSocket.destroy = jest.fn();
195-
tlsSocket.pipe = jest.fn();
196-
197182
jest.spyOn(http, 'request').mockReturnValue(connectReq);
198183
jest.spyOn(tls, 'connect').mockReturnValue(tlsSocket);
199184

@@ -221,15 +206,6 @@ describe('proxyWebSocket', () => {
221206
});
222207

223208
it('strips client-supplied auth headers before forwarding', () => {
224-
const socket = makeMockSocket();
225-
const connectReq = new EventEmitter();
226-
connectReq.end = jest.fn();
227-
const tunnel = makeMockSocket();
228-
const tlsSocket = new EventEmitter();
229-
tlsSocket.write = jest.fn();
230-
tlsSocket.destroy = jest.fn();
231-
tlsSocket.pipe = jest.fn();
232-
233209
jest.spyOn(http, 'request').mockReturnValue(connectReq);
234210
jest.spyOn(tls, 'connect').mockReturnValue(tlsSocket);
235211

@@ -264,10 +240,6 @@ describe('proxyWebSocket', () => {
264240
});
265241

266242
it('forwards the CONNECT request to the configured Squid proxy host/port', () => {
267-
const socket = makeMockSocket();
268-
const connectReq = new EventEmitter();
269-
connectReq.end = jest.fn();
270-
271243
let capturedOptions;
272244
jest.spyOn(http, 'request').mockImplementation((options) => {
273245
capturedOptions = options;
@@ -284,15 +256,6 @@ describe('proxyWebSocket', () => {
284256
});
285257

286258
it('forwards buffered head bytes to the upstream after upgrade', () => {
287-
const socket = makeMockSocket();
288-
const connectReq = new EventEmitter();
289-
connectReq.end = jest.fn();
290-
const tunnel = makeMockSocket();
291-
const tlsSocket = new EventEmitter();
292-
tlsSocket.write = jest.fn();
293-
tlsSocket.destroy = jest.fn();
294-
tlsSocket.pipe = jest.fn();
295-
296259
jest.spyOn(http, 'request').mockReturnValue(connectReq);
297260
jest.spyOn(tls, 'connect').mockReturnValue(tlsSocket);
298261

0 commit comments

Comments
 (0)