@@ -199,4 +199,152 @@ describe('iam_token_manager_v1', function() {
199
199
done ( ) ;
200
200
} ) ;
201
201
} ) ;
202
+
203
+ it ( 'should use the default Authorization header - no clientid, no secret' , function ( done ) {
204
+ const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
205
+
206
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
207
+ _callback ( ) ;
208
+ } ) ;
209
+
210
+ instance . getToken ( function ( ) {
211
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
212
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
213
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
214
+ done ( ) ;
215
+ } ) ;
216
+ } ) ;
217
+
218
+ it ( 'should use a non-default Authorization header - client id and secret via ctor' , function ( done ) {
219
+ const instance = new IamTokenManagerV1 ( {
220
+ iamApikey : 'abcd-1234' ,
221
+ iamClientId : 'foo' ,
222
+ iamSecret : 'bar' ,
223
+ } ) ;
224
+
225
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
226
+ _callback ( ) ;
227
+ } ) ;
228
+
229
+ instance . getToken ( function ( ) {
230
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
231
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
232
+ expect ( authHeader ) . not . toBe ( 'Basic Yng6Yng=' ) ;
233
+ done ( ) ;
234
+ } ) ;
235
+ } ) ;
236
+
237
+ it ( 'should use the default Authorization header - clientid only via ctor' , function ( done ) {
238
+ const instance = new IamTokenManagerV1 ( {
239
+ iamApikey : 'abcd-1234' ,
240
+ iamClientId : 'foo' ,
241
+ } ) ;
242
+
243
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
244
+ _callback ( ) ;
245
+ } ) ;
246
+
247
+ instance . getToken ( function ( ) {
248
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
249
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
250
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
251
+ done ( ) ;
252
+ } ) ;
253
+ } ) ;
254
+
255
+ it ( 'should use the default Authorization header, secret only via ctor' , function ( done ) {
256
+ const instance = new IamTokenManagerV1 ( {
257
+ iamApikey : 'abcd-1234' ,
258
+ iamSecret : 'bar' ,
259
+ } ) ;
260
+
261
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
262
+ _callback ( ) ;
263
+ } ) ;
264
+
265
+ instance . getToken ( function ( ) {
266
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
267
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
268
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
269
+ done ( ) ;
270
+ } ) ;
271
+ } ) ;
272
+
273
+ it ( 'should use a non-default Authorization header - client id and secret via setter' , function ( done ) {
274
+ const instance = new IamTokenManagerV1 ( {
275
+ iamApikey : 'abcd-1234' ,
276
+ } ) ;
277
+
278
+ instance . setIamAuthorizationInfo ( 'foo' , 'bar' ) ;
279
+
280
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
281
+ _callback ( ) ;
282
+ } ) ;
283
+
284
+ instance . getToken ( function ( ) {
285
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
286
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
287
+ expect ( authHeader ) . not . toBe ( 'Basic Yng6Yng=' ) ;
288
+ done ( ) ;
289
+ } ) ;
290
+ } ) ;
291
+
292
+ it ( 'should use the default Authorization header - clientid only via setter' , function ( done ) {
293
+ const instance = new IamTokenManagerV1 ( {
294
+ iamApikey : 'abcd-1234' ,
295
+ } ) ;
296
+
297
+ instance . setIamAuthorizationInfo ( 'foo' , null ) ;
298
+
299
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
300
+ _callback ( ) ;
301
+ } ) ;
302
+
303
+ instance . getToken ( function ( ) {
304
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
305
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
306
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
307
+ done ( ) ;
308
+ } ) ;
309
+ } ) ;
310
+
311
+ it ( 'should use the default Authorization header, secret only via ctor' , function ( done ) {
312
+ const instance = new IamTokenManagerV1 ( {
313
+ iamApikey : 'abcd-1234' ,
314
+ iamSecret : 'bar' ,
315
+ } ) ;
316
+
317
+ instance . setIamAuthorizationInfo ( null , 'bar' ) ;
318
+
319
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
320
+ _callback ( ) ;
321
+ } ) ;
322
+
323
+ instance . getToken ( function ( ) {
324
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
325
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
326
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
327
+ done ( ) ;
328
+ } ) ;
329
+ } ) ;
330
+
331
+ it ( 'should use the default Authorization header, nulls passed to setter' , function ( done ) {
332
+ const instance = new IamTokenManagerV1 ( {
333
+ iamApikey : 'abcd-1234' ,
334
+ iamSecret : 'bar' ,
335
+ } ) ;
336
+
337
+ instance . setIamAuthorizationInfo ( null , null ) ;
338
+
339
+ requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
340
+ _callback ( ) ;
341
+ } ) ;
342
+
343
+ instance . getToken ( function ( ) {
344
+ const sendRequestArgs = requestWrapper . sendRequest . mock . calls [ 0 ] [ 0 ] ;
345
+ const authHeader = sendRequestArgs . options . headers . Authorization ;
346
+ expect ( authHeader ) . toBe ( 'Basic Yng6Yng=' ) ;
347
+ done ( ) ;
348
+ } ) ;
349
+ } ) ;
202
350
} ) ;
0 commit comments