Skip to content

Commit 0c75000

Browse files
madpahclaude
andcommitted
test: refactor proxy tests to remove duplicated code
Extract shared mock response to avoid SonarQube duplication warning. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 566ecf8 commit 0c75000

1 file changed

Lines changed: 13 additions & 35 deletions

File tree

src/Services/GuideRequestService.spec.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,34 +129,30 @@ describe('GuideRequestService', () => {
129129
});
130130

131131
describe('proxy support', () => {
132-
it('should pass ProxyAgent as dispatcher when http_proxy is set', async () => {
133-
process.env.http_proxy = 'http://proxy.example.com:8080';
134-
135-
const expectedOutput = [
132+
const mockSuccessResponse = {
133+
ok: true,
134+
status: 200,
135+
statusText: 'OK',
136+
json: vi.fn().mockResolvedValue([
136137
{
137138
coordinates: 'pkg:npm/test@1.0.0',
138139
reference: 'https://guide.sonatype.com/blah',
139140
vulnerabilities: [],
140141
},
141-
];
142+
]),
143+
};
142144

143-
mockFetch.mockResolvedValueOnce({
144-
ok: true,
145-
status: 200,
146-
statusText: 'OK',
147-
json: vi.fn().mockResolvedValue(expectedOutput),
148-
});
145+
it('should pass ProxyAgent as dispatcher when http_proxy is set', async () => {
146+
process.env.http_proxy = 'http://proxy.example.com:8080';
147+
mockFetch.mockResolvedValueOnce(mockSuccessResponse);
149148

150149
const svc = new GuideRequestService('user', 'token', CACHE_PATH, SERVER);
151150
const coords = [new Coordinates('test', '1.0.0')];
152151
await svc.callGuideOrGetFromCache(coords, 'npm');
153152

154153
// Verify fetch was called with a dispatcher (ProxyAgent)
155154
expect(mockFetch).toHaveBeenCalled();
156-
const fetchCall = mockFetch.mock.calls[0];
157-
const fetchOptions = fetchCall[1] as RequestInit & { dispatcher?: ProxyAgent };
158-
159-
// This assertion demonstrates the bug - dispatcher should be a ProxyAgent but is undefined
155+
const fetchOptions = mockFetch.mock.calls[0][1] as RequestInit & { dispatcher?: ProxyAgent };
160156
expect(fetchOptions.dispatcher).toBeDefined();
161157
expect(fetchOptions.dispatcher).toBeInstanceOf(ProxyAgent);
162158
});
@@ -165,32 +161,14 @@ describe('GuideRequestService', () => {
165161
// Ensure no proxy env vars are set before creating service
166162
delete process.env.http_proxy;
167163
delete process.env.https_proxy;
168-
169-
const expectedOutput = [
170-
{
171-
coordinates: 'pkg:npm/test@1.0.0',
172-
reference: 'https://guide.sonatype.com/blah',
173-
vulnerabilities: [],
174-
},
175-
];
176-
177-
mockFetch.mockResolvedValueOnce({
178-
ok: true,
179-
status: 200,
180-
statusText: 'OK',
181-
json: vi.fn().mockResolvedValue(expectedOutput),
182-
});
164+
mockFetch.mockResolvedValueOnce(mockSuccessResponse);
183165

184166
const svc = new GuideRequestService('user', 'token', CACHE_PATH, SERVER);
185167
const coords = [new Coordinates('test', '1.0.0')];
186168
await svc.callGuideOrGetFromCache(coords, 'npm');
187169

188170
expect(mockFetch).toHaveBeenCalled();
189-
const fetchCall = mockFetch.mock.calls[0];
190-
const fetchOptions = fetchCall[1] as RequestInit & { dispatcher?: unknown };
191-
192-
// When no proxy is configured, the custom fetchApi should not be provided,
193-
// so dispatcher should not be in the options
171+
const fetchOptions = mockFetch.mock.calls[0][1] as RequestInit & { dispatcher?: unknown };
194172
expect(fetchOptions.dispatcher).toBeUndefined();
195173
});
196174
});

0 commit comments

Comments
 (0)