@@ -4,21 +4,21 @@ import {
4
4
ENV_CMDS_RELATIVE_URI ,
5
5
fromContainerMetadata
6
6
} from "./fromContainerMetadata" ;
7
- import { httpGet } from "./remoteProvider/httpGet " ;
7
+ import { httpRequest } from "./remoteProvider/httpRequest " ;
8
8
import {
9
9
fromImdsCredentials ,
10
10
ImdsCredentials
11
11
} from "./remoteProvider/ImdsCredentials" ;
12
12
13
- const mockHttpGet = < any > httpGet ;
14
- jest . mock ( "./remoteProvider/httpGet " , ( ) => ( { httpGet : jest . fn ( ) } ) ) ;
13
+ const mockHttpRequest = < any > httpRequest ;
14
+ jest . mock ( "./remoteProvider/httpRequest " , ( ) => ( { httpRequest : jest . fn ( ) } ) ) ;
15
15
16
16
const relativeUri = process . env [ ENV_CMDS_RELATIVE_URI ] ;
17
17
const fullUri = process . env [ ENV_CMDS_FULL_URI ] ;
18
18
const authToken = process . env [ ENV_CMDS_AUTH_TOKEN ] ;
19
19
20
20
beforeEach ( ( ) => {
21
- mockHttpGet . mockReset ( ) ;
21
+ mockHttpRequest . mockReset ( ) ;
22
22
delete process . env [ ENV_CMDS_RELATIVE_URI ] ;
23
23
delete process . env [ ENV_CMDS_FULL_URI ] ;
24
24
delete process . env [ ENV_CMDS_AUTH_TOKEN ] ;
@@ -53,12 +53,12 @@ describe("fromContainerMetadata", () => {
53
53
const token = "Basic abcd" ;
54
54
process . env [ ENV_CMDS_FULL_URI ] = "http://localhost:8080/path" ;
55
55
process . env [ ENV_CMDS_AUTH_TOKEN ] = token ;
56
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
56
+ mockHttpRequest . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
57
57
58
58
await fromContainerMetadata ( ) ( ) ;
59
59
60
- expect ( mockHttpGet . mock . calls . length ) . toBe ( 1 ) ;
61
- const [ options = { } ] = mockHttpGet . mock . calls [ 0 ] ;
60
+ expect ( mockHttpRequest . mock . calls . length ) . toBe ( 1 ) ;
61
+ const [ options = { } ] = mockHttpRequest . mock . calls [ 0 ] ;
62
62
expect ( options . headers ) . toMatchObject ( {
63
63
Authorization : token
64
64
} ) ;
@@ -70,7 +70,7 @@ describe("fromContainerMetadata", () => {
70
70
} ) ;
71
71
72
72
it ( "should resolve credentials by fetching them from the container metadata service" , async ( ) => {
73
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
73
+ mockHttpRequest . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
74
74
75
75
expect ( await fromContainerMetadata ( ) ( ) ) . toEqual (
76
76
fromImdsCredentials ( creds )
@@ -80,38 +80,42 @@ describe("fromContainerMetadata", () => {
80
80
it ( "should retry the fetching operation up to maxRetries times" , async ( ) => {
81
81
const maxRetries = 5 ;
82
82
for ( let i = 0 ; i < maxRetries - 1 ; i ++ ) {
83
- mockHttpGet . mockReturnValueOnce ( Promise . reject ( "No!" ) ) ;
83
+ mockHttpRequest . mockReturnValueOnce ( Promise . reject ( "No!" ) ) ;
84
84
}
85
- mockHttpGet . mockReturnValueOnce ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
85
+ mockHttpRequest . mockReturnValueOnce (
86
+ Promise . resolve ( JSON . stringify ( creds ) )
87
+ ) ;
86
88
87
89
expect ( await fromContainerMetadata ( { maxRetries } ) ( ) ) . toEqual (
88
90
fromImdsCredentials ( creds )
89
91
) ;
90
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( maxRetries ) ;
92
+ expect ( mockHttpRequest . mock . calls . length ) . toEqual ( maxRetries ) ;
91
93
} ) ;
92
94
93
95
it ( "should retry responses that receive invalid response values" , async ( ) => {
94
96
for ( let key of Object . keys ( creds ) ) {
95
97
const invalidCreds : any = { ...creds } ;
96
98
delete invalidCreds [ key ] ;
97
- mockHttpGet . mockReturnValueOnce (
99
+ mockHttpRequest . mockReturnValueOnce (
98
100
Promise . resolve ( JSON . stringify ( invalidCreds ) )
99
101
) ;
100
102
}
101
- mockHttpGet . mockReturnValueOnce ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
103
+ mockHttpRequest . mockReturnValueOnce (
104
+ Promise . resolve ( JSON . stringify ( creds ) )
105
+ ) ;
102
106
103
107
await fromContainerMetadata ( { maxRetries : 100 } ) ( ) ;
104
- expect ( mockHttpGet . mock . calls . length ) . toEqual (
108
+ expect ( mockHttpRequest . mock . calls . length ) . toEqual (
105
109
Object . keys ( creds ) . length + 1
106
110
) ;
107
111
} ) ;
108
112
109
- it ( "should pass relevant configuration to httpGet " , async ( ) => {
113
+ it ( "should pass relevant configuration to httpRequest " , async ( ) => {
110
114
const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
111
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
115
+ mockHttpRequest . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
112
116
await fromContainerMetadata ( { timeout } ) ( ) ;
113
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
114
- expect ( mockHttpGet . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
117
+ expect ( mockHttpRequest . mock . calls . length ) . toEqual ( 1 ) ;
118
+ expect ( mockHttpRequest . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
115
119
hostname : "169.254.170.2" ,
116
120
path : process . env [ ENV_CMDS_RELATIVE_URI ] ,
117
121
timeout
@@ -120,20 +124,20 @@ describe("fromContainerMetadata", () => {
120
124
} ) ;
121
125
122
126
describe ( ENV_CMDS_FULL_URI , ( ) => {
123
- it ( "should pass relevant configuration to httpGet " , async ( ) => {
127
+ it ( "should pass relevant configuration to httpRequest " , async ( ) => {
124
128
process . env [ ENV_CMDS_FULL_URI ] = "http://localhost:8080/path" ;
125
129
126
130
const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
127
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
131
+ mockHttpRequest . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
128
132
await fromContainerMetadata ( { timeout } ) ( ) ;
129
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
133
+ expect ( mockHttpRequest . mock . calls . length ) . toEqual ( 1 ) ;
130
134
const {
131
135
protocol,
132
136
hostname,
133
137
path,
134
138
port,
135
139
timeout : actualTimeout
136
- } = mockHttpGet . mock . calls [ 0 ] [ 0 ] ;
140
+ } = mockHttpRequest . mock . calls [ 0 ] [ 0 ] ;
137
141
expect ( protocol ) . toBe ( "http:" ) ;
138
142
expect ( hostname ) . toBe ( "localhost" ) ;
139
143
expect ( path ) . toBe ( "/path" ) ;
@@ -146,10 +150,10 @@ describe("fromContainerMetadata", () => {
146
150
process . env [ ENV_CMDS_FULL_URI ] = "http://localhost:8080/path" ;
147
151
148
152
const timeout = Math . ceil ( Math . random ( ) * 1000 ) ;
149
- mockHttpGet . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
153
+ mockHttpRequest . mockReturnValue ( Promise . resolve ( JSON . stringify ( creds ) ) ) ;
150
154
await fromContainerMetadata ( { timeout } ) ( ) ;
151
- expect ( mockHttpGet . mock . calls . length ) . toEqual ( 1 ) ;
152
- expect ( mockHttpGet . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
155
+ expect ( mockHttpRequest . mock . calls . length ) . toEqual ( 1 ) ;
156
+ expect ( mockHttpRequest . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
153
157
hostname : "169.254.170.2" ,
154
158
path : "foo" ,
155
159
timeout
0 commit comments