@@ -518,6 +518,45 @@ func TestBearerTokenPolicy_CAEChallengeHandling(t *testing.T) {
518
518
require .Equal (t , 2 , tkReqs , "policy shouldn't handle a second CAE challenge for the same request" )
519
519
require .Equal (t , 2 , srv .Requests (), "policy shouldn't handle a second CAE challenge for the same request" )
520
520
})
521
+
522
+ t .Run ("errors non-retriable" , func (t * testing.T ) {
523
+ srv , close := mock .NewTLSServer ()
524
+ defer close ()
525
+ srv .AppendResponse (mock .WithStatusCode (http .StatusOK ))
526
+ srv .AppendResponse (
527
+ mock .WithHeader (shared .HeaderWWWAuthenticate , `Bearer error="insufficient_claims", claims="ey=="` ),
528
+ mock .WithStatusCode (http .StatusUnauthorized ),
529
+ )
530
+
531
+ called := false
532
+ expectedErr := errors .New ("something went wrong" )
533
+ cred := mockCredential {
534
+ getTokenImpl : func (context.Context , policy.TokenRequestOptions ) (exported.AccessToken , error ) {
535
+ if called {
536
+ return exported.AccessToken {}, expectedErr
537
+ }
538
+ called = true
539
+ return exported.AccessToken {Token : tokenValue , ExpiresOn : time .Now ().Add (time .Hour ).UTC ()}, nil
540
+ },
541
+ }
542
+ counter := & countingPolicy {}
543
+ btp := NewBearerTokenPolicy (cred , []string {scope }, nil )
544
+ pl := newTestPipeline (& policy.ClientOptions {PerRetryPolicies : []policy.Policy {counter , btp }, Transport : srv })
545
+
546
+ req , err := NewRequest (context .Background (), http .MethodGet , srv .URL ())
547
+ require .NoError (t , err )
548
+ _ , err = pl .Do (req )
549
+ require .NoError (t , err )
550
+
551
+ req , err = NewRequest (context .Background (), http .MethodGet , srv .URL ())
552
+ require .NoError (t , err )
553
+ _ , err = pl .Do (req )
554
+ require .EqualError (t , err , expectedErr .Error ())
555
+ require .ErrorAs (t , err , new (errorinfo.NonRetriable ))
556
+ // this is the crucial assertion; the retry policy would have retried the request
557
+ // if BearerTokenPolicy didn't make the credential's error NonRetriable
558
+ require .Equal (t , 2 , counter .count , "BearerTokenPolicy should make the authentication error NonRetriable" )
559
+ })
521
560
}
522
561
523
562
func TestBearerTokenPolicy_RequiresHTTPS (t * testing.T ) {
0 commit comments