22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5-
5+ import sinon from 'sinon'
66import assert from 'assert'
77import { version } from 'vscode'
88import { getClientId } from '../../shared/telemetry/util'
99import { FakeMemento } from '../fakeExtensionContext'
1010import { FakeAwsContext } from '../utilities/fakeAwsContext'
1111import { GlobalState } from '../../shared/globalState'
12- import { AWSClientBuilderV3 , getServiceId , recordErrorTelemetry } from '../../shared/awsClientBuilderV3'
12+ import {
13+ AWSClientBuilderV3 ,
14+ emitOnRequest ,
15+ getServiceId ,
16+ logOnRequest ,
17+ overwriteEndpoint ,
18+ recordErrorTelemetry ,
19+ } from '../../shared/awsClientBuilderV3'
1320import { Client } from '@aws-sdk/smithy-client'
14- import { extensionVersion } from '../../shared'
21+ import { DevSettings , extensionVersion } from '../../shared'
1522import { assertTelemetry } from '../testUtil'
1623import { telemetry } from '../../shared/telemetry'
24+ import { HttpRequest , HttpResponse } from '@aws-sdk/protocol-http'
25+ import { assertLogsContain , assertLogsContainAllOf } from '../globalSetup.test'
26+ import { TestSettings } from '../utilities/testSettingsConfiguration'
1727import { CredentialsShim } from '../../auth/deprecated/loginManager'
1828import { Credentials } from '@aws-sdk/types'
1929import { oneDay } from '../../shared/datetime'
@@ -25,39 +35,126 @@ describe('AwsClientBuilderV3', function () {
2535 builder = new AWSClientBuilderV3 ( new FakeAwsContext ( ) )
2636 } )
2737
28- describe ( 'createAndConfigureSdkClient' , function ( ) {
29- it ( 'includes Toolkit user-agent if no options are specified' , async function ( ) {
30- const service = await builder . createAwsService ( Client )
31- const clientId = getClientId ( new GlobalState ( new FakeMemento ( ) ) )
32-
33- assert . ok ( service . config . userAgent )
34- assert . strictEqual (
35- service . config . userAgent ! [ 0 ] [ 0 ] . replace ( '---Insiders' , '' ) ,
36- `AWS-Toolkit-For-VSCode/testPluginVersion Visual-Studio-Code/${ version } ClientId/${ clientId } `
37- )
38- assert . strictEqual ( service . config . userAgent ! [ 0 ] [ 1 ] , extensionVersion )
38+ it ( 'includes Toolkit user-agent if no options are specified' , async function ( ) {
39+ const service = await builder . createAwsService ( Client )
40+ const clientId = getClientId ( new GlobalState ( new FakeMemento ( ) ) )
41+
42+ assert . ok ( service . config . userAgent )
43+ assert . strictEqual (
44+ service . config . userAgent ! [ 0 ] [ 0 ] . replace ( '---Insiders' , '' ) ,
45+ `AWS-Toolkit-For-VSCode/testPluginVersion Visual-Studio-Code/${ version } ClientId/${ clientId } `
46+ )
47+ assert . strictEqual ( service . config . userAgent ! [ 0 ] [ 1 ] , extensionVersion )
48+ } )
49+
50+ it ( 'adds region to client' , async function ( ) {
51+ const service = await builder . createAwsService ( Client , { region : 'us-west-2' } )
52+
53+ assert . ok ( service . config . region )
54+ assert . strictEqual ( service . config . region , 'us-west-2' )
55+ } )
56+
57+ it ( 'adds Client-Id to user agent' , async function ( ) {
58+ const service = await builder . createAwsService ( Client )
59+ const clientId = getClientId ( new GlobalState ( new FakeMemento ( ) ) )
60+ const regex = new RegExp ( `ClientId/${ clientId } ` )
61+ assert . ok ( service . config . userAgent ! [ 0 ] [ 0 ] . match ( regex ) )
62+ } )
63+
64+ it ( 'does not override custom user-agent if specified in options' , async function ( ) {
65+ const service = await builder . createAwsService ( Client , {
66+ userAgent : [ [ 'CUSTOM USER AGENT' ] ] ,
3967 } )
4068
41- it ( 'adds region to client' , async function ( ) {
42- const service = await builder . createAwsService ( Client , { region : 'us-west-2' } )
69+ assert . strictEqual ( service . config . userAgent [ 0 ] [ 0 ] , 'CUSTOM USER AGENT' )
70+ } )
4371
44- assert . ok ( service . config . region )
45- assert . strictEqual ( service . config . region , 'us-west-2' )
72+ describe ( 'middlewareStack' , function ( ) {
73+ let args : { request : { hostname : string ; path : string } ; input : any }
74+ let context : { clientName ?: string ; commandName ?: string }
75+ let response : { response : { statusCode : number } ; output : { message : string } }
76+ let httpRequestStub : sinon . SinonStub
77+ let httpResponseStub : sinon . SinonStub
78+
79+ before ( function ( ) {
80+ httpRequestStub = sinon . stub ( HttpRequest , 'isInstance' )
81+ httpResponseStub = sinon . stub ( HttpResponse , 'isInstance' )
82+ httpRequestStub . callsFake ( ( ) => true )
83+ httpResponseStub . callsFake ( ( ) => true )
4684 } )
4785
48- it ( 'adds Client-Id to user agent' , async function ( ) {
49- const service = await builder . createAwsService ( Client )
50- const clientId = getClientId ( new GlobalState ( new FakeMemento ( ) ) )
51- const regex = new RegExp ( `ClientId/${ clientId } ` )
52- assert . ok ( service . config . userAgent ! [ 0 ] [ 0 ] . match ( regex ) )
86+ beforeEach ( function ( ) {
87+ args = {
88+ request : {
89+ hostname : 'testHost' ,
90+ path : 'testPath' ,
91+ } ,
92+ input : {
93+ testKey : 'testValue' ,
94+ } ,
95+ }
96+ context = {
97+ clientName : 'fooClient' ,
98+ }
99+ response = {
100+ response : {
101+ statusCode : 200 ,
102+ } ,
103+ output : {
104+ message : 'test output' ,
105+ } ,
106+ }
107+ } )
108+ after ( function ( ) {
109+ sinon . restore ( )
110+ } )
111+
112+ it ( 'logs messages on request' , async function ( ) {
113+ await logOnRequest ( ( _ : any ) => _ , args as any )
114+ assertLogsContainAllOf ( [ 'testHost' , 'testPath' ] , false , 'debug' )
115+ } )
116+
117+ it ( 'adds telemetry metadata and logs on error failure' , async function ( ) {
118+ const next = ( _ : any ) => {
119+ throw new Error ( 'test error' )
120+ }
121+ await telemetry . vscode_executeCommand . run ( async ( span ) => {
122+ await assert . rejects ( emitOnRequest ( next , context , args ) )
123+ } )
124+ assertLogsContain ( 'test error' , false , 'error' )
125+ assertTelemetry ( 'vscode_executeCommand' , { requestServiceType : 'foo' } )
53126 } )
54127
55- it ( 'does not override custom user-agent if specified in options' , async function ( ) {
56- const service = await builder . createAwsService ( Client , {
57- userAgent : [ [ 'CUSTOM USER AGENT' ] ] ,
128+ it ( 'does not emit telemetry, but still logs on successes' , async function ( ) {
129+ const next = async ( _ : any ) => {
130+ return response
131+ }
132+ await telemetry . vscode_executeCommand . run ( async ( span ) => {
133+ assert . deepStrictEqual ( await emitOnRequest ( next , context , args ) , response )
58134 } )
135+ assertLogsContainAllOf ( [ 'testHost' , 'testPath' ] , false , 'debug' )
136+ assert . throws ( ( ) => assertTelemetry ( 'vscode_executeCommand' , { requestServiceType : 'foo' } ) )
137+ } )
138+
139+ it ( 'custom endpoints overwrite request url' , async function ( ) {
140+ const settings = new TestSettings ( )
141+ await settings . update ( 'aws.dev.endpoints' , { foo : 'http://example.com:3000/path' } )
142+ const next = async ( args : any ) => args
143+ const newArgs : any = await overwriteEndpoint ( next , context , new DevSettings ( settings ) , args )
144+
145+ assert . strictEqual ( newArgs . request . hostname , 'example.com' )
146+ assert . strictEqual ( newArgs . request . protocol , 'http:' )
147+ assert . strictEqual ( newArgs . request . port , '3000' )
148+ assert . strictEqual ( newArgs . request . pathname , '/path' )
149+ } )
150+
151+ it ( 'custom endpoints are not overwritten if not specified' , async function ( ) {
152+ const settings = new TestSettings ( )
153+ const next = async ( args : any ) => args
154+ const newArgs : any = await overwriteEndpoint ( next , context , new DevSettings ( settings ) , args )
59155
60- assert . strictEqual ( service . config . userAgent [ 0 ] [ 0 ] , 'CUSTOM USER AGENT' )
156+ assert . strictEqual ( newArgs . request . hostname , 'testHost' )
157+ assert . strictEqual ( newArgs . request . path , 'testPath' )
61158 } )
62159 } )
63160
0 commit comments