@@ -16,8 +16,17 @@ jest.mock("@aws-sdk/node-config-provider", () => ({
16
16
loadConfig : ( ) => mockAppIdLoader ,
17
17
} ) ) ;
18
18
19
+ import { UserAgent } from "@aws-sdk/types" ;
20
+
19
21
import { defaultUserAgent } from "." ;
20
22
23
+ const validateUserAgent = ( userAgent : UserAgent , expected : UserAgent ) => {
24
+ expect ( userAgent . length ) . toBe ( expected . length ) ;
25
+ for ( const pair of expected ) {
26
+ expect ( userAgent ) . toContainEqual ( pair ) ;
27
+ }
28
+ } ;
29
+
21
30
describe ( "defaultUserAgent" , ( ) => {
22
31
beforeEach ( ( ) => {
23
32
jest . resetAllMocks ( ) ;
@@ -27,31 +36,54 @@ describe("defaultUserAgent", () => {
27
36
jest . clearAllMocks ( ) ;
28
37
} ) ;
29
38
39
+ const basicUserAgent : UserAgent = [
40
+ [ "aws-sdk-js" , "0.1.0" ] ,
41
+ [ "api/s3" , "0.1.0" ] ,
42
+ [ "os/darwin" , "19.6.0" ] ,
43
+ [ "lang/js" ] ,
44
+ [ "md/nodejs" , "14.13.1" ] ,
45
+ ] ;
46
+
30
47
it ( "should response basic node default user agent" , async ( ) => {
31
48
const userAgent = await defaultUserAgent ( { serviceId : "s3" , clientVersion : "0.1.0" } ) ( ) ;
32
- expect ( userAgent ) . toContainEqual ( [ "aws-sdk-js" , "0.1.0" ] ) ;
33
- expect ( userAgent ) . toContainEqual ( [ "api/s3" , "0.1.0" ] ) ;
34
- expect ( userAgent ) . toContainEqual ( [ "os/darwin" , "19.6.0" ] ) ;
35
- expect ( userAgent ) . toContainEqual ( [ "lang/js" ] ) ;
49
+ validateUserAgent ( userAgent , basicUserAgent ) ;
36
50
} ) ;
37
51
38
52
it ( "should skip api version if service id is not supplied" , async ( ) => {
39
53
const userAgent = await defaultUserAgent ( { serviceId : undefined , clientVersion : "0.1.0" } ) ( ) ;
40
- expect ( userAgent ) . not . toContainEqual ( [ "api/s3" , "0.1.0" ] ) ;
54
+ validateUserAgent (
55
+ userAgent ,
56
+ basicUserAgent . filter ( ( pair ) => pair [ 0 ] !== "api/s3" )
57
+ ) ;
41
58
} ) ;
42
59
43
60
it ( "should add AWS_EXECUTION_ENV" , async ( ) => {
44
61
//@ts -ignore mock environmental variables
45
62
mockEnv . AWS_EXECUTION_ENV = "lambda" ;
46
63
const userAgent = await defaultUserAgent ( { serviceId : "s3" , clientVersion : "0.1.0" } ) ( ) ;
47
- expect ( userAgent ) . toContainEqual ( [ "exec-env/lambda" ] ) ;
64
+ const expectedUserAgent : UserAgent = [ ...basicUserAgent , [ "exec-env/lambda" ] ] ;
65
+ validateUserAgent ( userAgent , expectedUserAgent ) ;
66
+ //@ts -ignore mock environmental variables
67
+ delete mockEnv . AWS_EXECUTION_ENV ;
48
68
} ) ;
49
69
50
70
it ( "should load app id if available" , async ( ) => {
51
71
mockAppIdLoader . mockClear ( ) ;
52
72
const appId = "appId12345" ;
53
73
mockAppIdLoader . mockResolvedValue ( appId ) ;
54
74
const userAgent = await defaultUserAgent ( { serviceId : "s3" , clientVersion : "0.1.0" } ) ( ) ;
55
- expect ( userAgent ) . toContainEqual ( [ `app/${ appId } ` ] ) ;
75
+ const expectedUserAgent : UserAgent = [ ...basicUserAgent , [ `app/${ appId } ` ] ] ;
76
+ validateUserAgent ( userAgent , expectedUserAgent ) ;
77
+ } ) ;
78
+
79
+ it ( "should memoize app id" , async ( ) => {
80
+ mockAppIdLoader . mockClear ( ) ;
81
+ const appId = "appId12345" ;
82
+ mockAppIdLoader . mockResolvedValue ( appId ) ;
83
+ const userAgentProvider = defaultUserAgent ( { serviceId : "s3" , clientVersion : "0.1.0" } ) ;
84
+ const expectedUserAgent : UserAgent = [ ...basicUserAgent , [ `app/${ appId } ` ] ] ;
85
+ validateUserAgent ( await userAgentProvider ( ) , expectedUserAgent ) ;
86
+ validateUserAgent ( await userAgentProvider ( ) , expectedUserAgent ) ;
87
+ expect ( mockAppIdLoader ) . toBeCalledTimes ( 1 ) ;
56
88
} ) ;
57
89
} ) ;
0 commit comments