Skip to content

Commit 51625eb

Browse files
roger-zhanggPotatoWKY
authored andcommitted
test(lambda): remote debugging user-agent test (aws#8380)
## Problem We removed the test in :https://github.com/aws/aws-toolkit-vscode/pull/8318/files due to localproxy not mocked correctly. ## Solution Fixed the issue and now adding back these tests. This test starts a mock server and captures the actual user-agent being sent to make sure we are sending correct UA. --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent fb833ed commit 51625eb

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

packages/core/src/test/lambda/remoteDebugging/ldkClient.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,111 @@ import {
2424
TunnelStatus,
2525
} from '@aws-sdk/client-iotsecuretunneling'
2626
import { AwsStub, mockClient } from 'aws-sdk-client-mock'
27+
import * as http from 'http'
28+
import { AWSClientBuilderV3 } from '../../../shared/awsClientBuilderV3'
29+
import { FakeAwsContext } from '../../utilities/fakeAwsContext'
30+
import { Any } from '../../../shared/utilities/typeConstructors'
31+
32+
describe('Remote Debugging User-Agent test', () => {
33+
let sandbox: sinon.SinonSandbox
34+
let ldkClient: LdkClient
35+
let mockServer: http.Server
36+
let capturedHeaders: http.IncomingHttpHeaders | undefined
37+
let sdkBuilderTmp: Any
38+
let mockLocalProxy: any
39+
40+
before(async () => {
41+
sdkBuilderTmp = globals.sdkClientBuilderV3
42+
43+
mockServer = http.createServer((req, res) => {
44+
capturedHeaders = req.headers
45+
res.writeHead(200, { 'Content-Type': 'application/json' })
46+
res.end()
47+
})
48+
49+
// Start the mock server
50+
await new Promise<void>((resolve) => {
51+
mockServer.listen(0, '127.0.0.1', () => {
52+
resolve()
53+
})
54+
})
55+
56+
const port = (mockServer.address() as any).port
57+
globals.sdkClientBuilderV3 = new AWSClientBuilderV3(
58+
new FakeAwsContext({
59+
contextCredentials: {
60+
endpointUrl: `http://127.0.0.1:${port}`,
61+
credentials: undefined as any,
62+
credentialsId: '',
63+
},
64+
})
65+
)
66+
})
67+
68+
beforeEach(() => {
69+
sandbox = sinon.createSandbox()
70+
sandbox.stub(telemetryUtil, 'getClientId').returns('test-client-id')
71+
capturedHeaders = undefined
72+
// Mock LocalProxy
73+
mockLocalProxy = {
74+
start: sandbox.stub(),
75+
stop: sandbox.stub(),
76+
}
77+
sandbox.stub(LocalProxy.prototype, 'start').callsFake(mockLocalProxy.start)
78+
sandbox.stub(LocalProxy.prototype, 'stop').callsFake(mockLocalProxy.stop)
79+
ldkClient = LdkClient.instance
80+
;(ldkClient as any).localProxy = mockLocalProxy
81+
ldkClient.dispose()
82+
})
83+
84+
afterEach(() => {
85+
sandbox.restore()
86+
})
87+
88+
after(async () => {
89+
globals.sdkClientBuilderV3 = sdkBuilderTmp
90+
// Close the server
91+
mockServer.close()
92+
})
93+
94+
for (const scenario of ['Lambda', 'IoT']) {
95+
it(`should send ${scenario} request with correct User-Agent header to mock server`, async () => {
96+
try {
97+
switch (scenario) {
98+
case 'Lambda':
99+
await ldkClient.getFunctionDetail('arn:aws:lambda:us-east-1:123456789012:function:testFunction')
100+
break
101+
case 'IoT':
102+
await ldkClient.createOrReuseTunnel('us-east-1')
103+
break
104+
}
105+
} catch (e) {
106+
// Ignore errors from the mock response, we just want to capture headers
107+
}
108+
109+
// Verify the User-Agent header was sent correctly
110+
assert(capturedHeaders, 'Should have captured request headers')
111+
const userAgent = capturedHeaders!['user-agent'] || capturedHeaders!['User-Agent']
112+
assert(userAgent, 'Should have User-Agent header')
113+
114+
// The User-Agent should contain our custom user agent pairs
115+
assert(
116+
userAgent.includes('LAMBDA-DEBUG/1.0.0'),
117+
`User-Agent should include LAMBDA-DEBUG/1.0.0, got: ${userAgent}`
118+
)
119+
// Check for presence of other user agent components without checking specific values
120+
assert(
121+
userAgent.includes('AWS-Toolkit-For-VSCode/'),
122+
`User-Agent should include AWS-Toolkit-For-VSCode/, got: ${userAgent}`
123+
)
124+
assert(
125+
userAgent.includes('Visual-Studio-Code'),
126+
`User-Agent should include Visual-Studio-Code, got: ${userAgent}`
127+
)
128+
assert(userAgent.includes('ClientId/'), `User-Agent should include ClientId/, got: ${userAgent}`)
129+
})
130+
}
131+
})
27132

28133
describe('LdkClient', () => {
29134
let sandbox: sinon.SinonSandbox

0 commit comments

Comments
 (0)