@@ -64,9 +64,13 @@ type mockSigner struct {
64
64
SampleKey string
65
65
SampleValue string
66
66
ReturnError bool
67
+ testHook func (* http.Request )
67
68
}
68
69
69
70
func (m * mockSigner ) SignRequest (req * http.Request ) error {
71
+ if m .testHook != nil {
72
+ m .testHook (req )
73
+ }
70
74
if m .ReturnError {
71
75
return fmt .Errorf ("invalid data" )
72
76
}
@@ -732,6 +736,47 @@ func TestTransportPerformRetries(t *testing.T) {
732
736
}
733
737
})
734
738
739
+ t .Run ("Signer can sign correctly during retry" , func (t * testing.T ) {
740
+ u , _ := url .Parse ("https://foo.com/bar" )
741
+ signer := mockSigner {}
742
+ callsToSigner := 0
743
+ expectedBody := "FOOBAR"
744
+
745
+ signer .testHook = func (req * http.Request ) {
746
+ callsToSigner ++
747
+ body , err := io .ReadAll (req .Body )
748
+ if err != nil {
749
+ panic (err )
750
+ }
751
+ if string (body ) != expectedBody {
752
+ t .Fatalf ("request %d body: expected %q, got %q" , callsToSigner , expectedBody , body )
753
+ }
754
+ }
755
+
756
+ tp , _ := New (
757
+ Config {
758
+ URLs : []* url.URL {u },
759
+ Signer : & signer ,
760
+ Transport : & mockTransp {
761
+ RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
762
+ return & http.Response {Status : "MOCK" , StatusCode : http .StatusBadGateway }, nil
763
+ },
764
+ },
765
+ },
766
+ )
767
+
768
+ req , _ := http .NewRequest (http .MethodPost , "/abc" , strings .NewReader (expectedBody ))
769
+ //nolint:bodyclose // Mock response does not have a body to close
770
+ _ , err := tp .Perform (req )
771
+ if err != nil {
772
+ t .Fatalf ("Unexpected error: %s" , err )
773
+ }
774
+
775
+ if callsToSigner != 4 {
776
+ t .Fatalf ("expected 4 requests, got %d" , callsToSigner )
777
+ }
778
+ })
779
+
735
780
t .Run ("Don't retry request on regular error" , func (t * testing.T ) {
736
781
var i int
737
782
0 commit comments