From c98fe0a5fcffcc1994e58db23c5077e02cb2997c Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Sun, 22 Sep 2024 17:39:03 +0200 Subject: [PATCH] fix: match max redirects with total requests --- config/confighttp/confighttp.go | 6 +----- config/confighttp/confighttp_test.go | 7 ++++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index 5956bd954a2..7f8c7739976 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -256,14 +256,10 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett func makeCheckRedirect(max *int) func(*http.Request, []*http.Request) error { if max == nil { return nil - } else if *max == 0 { - return func(_ *http.Request, _ []*http.Request) error { - return http.ErrUseLastResponse - } } return func(_ *http.Request, via []*http.Request) error { - if *max == len(via) { + if *max == (len(via) - 1) { return http.ErrUseLastResponse } return nil diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index 3dd2a6f7af3..26f1c4058cf 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -360,10 +360,15 @@ func TestMaxRedirects(t *testing.T) { settings: ClientConfig{MaxRedirects: toIntPtr(0)}, expectedRequests: 1, }, + { + name: "One redirect", + settings: ClientConfig{MaxRedirects: toIntPtr(1)}, + expectedRequests: 2, + }, { name: "Defined max redirects", settings: ClientConfig{MaxRedirects: toIntPtr(5)}, - expectedRequests: 5, + expectedRequests: 6, }, }