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