Skip to content

Commit af0e7c1

Browse files
committed
test: replace proxy integration tests with addProxyAgent unit tests
The proxy tests claimed to "verify agent is set correctly" but only asserted on the response body — the agent was never actually checked. Replace with direct unit tests for addProxyAgent() that verify: - HttpsProxyAgent is created with the correct proxy URL - Agent is set for both HTTP and HTTPS proxy URLs - Agent is set when proxy comes from environment variable - No agent is set when no proxy is configured Also fix MSW v2 handler format in requestImages integration tests (use HttpResponse.json() instead of the v1 res(ctx.json()) API). Remove requestStack proxy integration tests that were broken due to fake file paths — the proxy logic is now properly covered by the addProxyAgent unit tests. Implements TC-3922 Assisted-by: Claude Code
1 parent 98f6a5b commit af0e7c1

1 file changed

Lines changed: 45 additions & 109 deletions

File tree

test/analysis.test.js

Lines changed: 45 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { expect } from 'chai'
22
import esmock from 'esmock'
3+
import { HttpsProxyAgent } from 'https-proxy-agent'
34
import { afterEach } from 'mocha'
4-
import { http } from 'msw'
5+
import { http, HttpResponse } from 'msw'
56
import { setupServer } from 'msw/node'
67
import { stub } from 'sinon'
78

89
import analysis from '../src/analysis.js'
10+
import { addProxyAgent } from '../src/tools.js'
911

1012
// utility function creating a dummy server, intercepting a handler,
1113
// running a test, and shutting the server down
@@ -205,7 +207,41 @@ suite('testing the analysis module for sending api requests', () => {
205207
))
206208
})
207209

208-
suite('verify proxy configuration for requestImages', () => {
210+
suite('addProxyAgent', () => {
211+
afterEach(() => {
212+
delete process.env['TRUSTIFY_DA_PROXY_URL']
213+
})
214+
215+
test('should set HttpsProxyAgent when proxy URL is provided via options', () => {
216+
const options = { method: 'POST' }
217+
const result = addProxyAgent(options, { 'TRUSTIFY_DA_PROXY_URL': 'http://proxy.example.com:8080' })
218+
expect(result.agent).to.be.instanceOf(HttpsProxyAgent)
219+
expect(result.agent.proxy.href).to.equal('http://proxy.example.com:8080/')
220+
})
221+
222+
test('should set HttpsProxyAgent for HTTPS proxy URL', () => {
223+
const options = { method: 'POST' }
224+
const result = addProxyAgent(options, { 'TRUSTIFY_DA_PROXY_URL': 'https://proxy.example.com:8443' })
225+
expect(result.agent).to.be.instanceOf(HttpsProxyAgent)
226+
expect(result.agent.proxy.href).to.equal('https://proxy.example.com:8443/')
227+
})
228+
229+
test('should set HttpsProxyAgent when proxy URL is provided via environment variable', () => {
230+
process.env['TRUSTIFY_DA_PROXY_URL'] = 'http://proxy.example.com:8080'
231+
const options = { method: 'POST' }
232+
const result = addProxyAgent(options, {})
233+
expect(result.agent).to.be.instanceOf(HttpsProxyAgent)
234+
expect(result.agent.proxy.href).to.equal('http://proxy.example.com:8080/')
235+
})
236+
237+
test('should not set agent when no proxy is configured', () => {
238+
const options = { method: 'POST' }
239+
const result = addProxyAgent(options, {})
240+
expect(result.agent).to.be.undefined
241+
})
242+
})
243+
244+
suite('requestImages with proxy configuration', () => {
209245
let mockAnalysis
210246

211247
setup(async () => {
@@ -222,125 +258,25 @@ suite('testing the analysis module for sending api requests', () => {
222258
})
223259
})
224260

225-
afterEach(() => {
226-
delete process.env['TRUSTIFY_DA_PROXY_URL']
227-
})
228-
229-
test('when HTTP proxy is configured, verify agent is set correctly', interceptAndRun(
230-
http.post(`${backendUrl}/api/v5/batch-analysis`, (req, res, ctx) => {
231-
return res(ctx.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } }))
261+
test('should succeed when proxy is configured', interceptAndRun(
262+
http.post(`${backendUrl}/api/v5/batch-analysis`, () => {
263+
return HttpResponse.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
232264
}),
233265
async () => {
234-
const httpProxyUrl = 'http://proxy.example.com:8080'
235-
const options = {
236-
'TRUSTIFY_DA_PROXY_URL': httpProxyUrl
237-
}
266+
const options = { 'TRUSTIFY_DA_PROXY_URL': 'http://proxy.example.com:8080' }
238267
let res = await mockAnalysis.requestImages(['fake-image:latest'], backendUrl, false, options)
239268
expect(res).to.deep.equal({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
240269
}
241270
))
242271

243-
test('when HTTPS proxy is configured, verify agent is set correctly', interceptAndRun(
244-
http.post(`${backendUrl}/api/v5/batch-analysis`, (req, res, ctx) => {
245-
return res(ctx.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } }))
272+
test('should succeed when no proxy is configured', interceptAndRun(
273+
http.post(`${backendUrl}/api/v5/batch-analysis`, () => {
274+
return HttpResponse.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
246275
}),
247276
async () => {
248-
const httpsProxyUrl = 'https://proxy.example.com:8080'
249-
const options = {
250-
'TRUSTIFY_DA_PROXY_URL': httpsProxyUrl
251-
}
252-
let res = await mockAnalysis.requestImages(['fake-image:latest'], backendUrl, false, options)
253-
expect(res).to.deep.equal({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
254-
}
255-
))
256-
257-
test('when proxy is configured via environment variable, verify agent is set correctly', interceptAndRun(
258-
http.post(`${backendUrl}/api/v5/batch-analysis`, (req, res, ctx) => {
259-
return res(ctx.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } }))
260-
}),
261-
async () => {
262-
process.env['TRUSTIFY_DA_PROXY_URL'] = 'http://proxy.example.com:8080'
263277
let res = await mockAnalysis.requestImages(['fake-image:latest'], backendUrl)
264278
expect(res).to.deep.equal({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
265279
}
266280
))
267-
268-
test('when no proxy is configured, verify no agent is set', interceptAndRun(
269-
http.post(`${backendUrl}/api/v5/batch-analysis`, (req, res, ctx) => {
270-
return res(ctx.json({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } }))
271-
}),
272-
async () => {
273-
let res = await mockAnalysis.requestImages(['fake-image:latest'], backendUrl)
274-
expect(res).to.deep.equal({ 'pkg:oci/fake-image@sha256:abc123': { ok: 'ok' } })
275-
}
276-
))
277-
})
278-
279-
suite('verify proxy configuration', () => {
280-
let fakeManifest = 'fake-file.typ'
281-
let stackProviderStub = stub()
282-
stackProviderStub.withArgs(fakeManifest).returns(fakeProvided)
283-
let fakeProvider = {
284-
provideComponent: () => { },
285-
provideStack: stackProviderStub,
286-
isSupported: () => { }
287-
};
288-
289-
afterEach(() => {
290-
delete process.env['TRUSTIFY_DA_PROXY_URL']
291-
})
292-
293-
test('when HTTP proxy is configured, verify agent is set correctly', interceptAndRun(
294-
http.post(`${backendUrl}/api/v5/analysis`, (req, res, ctx) => {
295-
// The request should go through the proxy
296-
return res(ctx.json({ ok: 'ok' }))
297-
}),
298-
async () => {
299-
const httpProxyUrl = 'http://proxy.example.com:8080'
300-
const options = {
301-
'TRUSTIFY_DA_PROXY_URL': httpProxyUrl
302-
}
303-
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl, false, options)
304-
expect(res).to.deep.equal({ ok: 'ok' })
305-
}
306-
))
307-
308-
test('when HTTPS proxy is configured, verify agent is set correctly', interceptAndRun(
309-
http.post(`${backendUrl}/api/v5/analysis`, (req, res, ctx) => {
310-
// The request should go through the proxy
311-
return res(ctx.json({ ok: 'ok' }))
312-
}),
313-
async () => {
314-
const httpsProxyUrl = 'https://proxy.example.com:8080'
315-
const options = {
316-
'TRUSTIFY_DA_PROXY_URL': httpsProxyUrl
317-
}
318-
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl, false, options)
319-
expect(res).to.deep.equal({ ok: 'ok' })
320-
}
321-
))
322-
323-
test('when proxy is configured via environment variable, verify agent is set correctly', interceptAndRun(
324-
http.post(`${backendUrl}/api/v5/analysis`, (req, res, ctx) => {
325-
// The request should go through the proxy
326-
return res(ctx.json({ ok: 'ok' }))
327-
}),
328-
async () => {
329-
process.env['TRUSTIFY_DA_PROXY_URL'] = 'http://proxy.example.com:8080'
330-
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
331-
expect(res).to.deep.equal({ ok: 'ok' })
332-
}
333-
))
334-
335-
test('when no proxy is configured, verify no agent is set', interceptAndRun(
336-
http.post(`${backendUrl}/api/v5/analysis`, (req, res, ctx) => {
337-
// The request should go directly without proxy
338-
return res(ctx.json({ ok: 'ok' }))
339-
}),
340-
async () => {
341-
let res = await analysis.requestStack(fakeProvider, fakeManifest, backendUrl)
342-
expect(res).to.deep.equal({ ok: 'ok' })
343-
}
344-
))
345281
})
346282
})

0 commit comments

Comments
 (0)