Skip to content

Commit

Permalink
feat: remove pointer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rogercoll committed Dec 4, 2024
1 parent 76807a3 commit e6b62bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
13 changes: 5 additions & 8 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type ClientConfig struct {
Cookies *CookiesConfig `mapstructure:"cookies"`

// Maximum number of redirections to follow, if not defined, the Client uses its default policy, which is to stop after 10 consecutive requests.
MaxRedirects *int `mapstructure:"max_redirects"`
MaxRedirects int `mapstructure:"max_redirects"`
}

// CookiesConfig defines the configuration of the HTTP client regarding cookies served by the server.
Expand All @@ -132,6 +132,7 @@ func NewDefaultClientConfig() ClientConfig {
MaxIdleConnsPerHost: &defaultTransport.MaxIdleConnsPerHost,
MaxConnsPerHost: &defaultTransport.MaxConnsPerHost,
IdleConnTimeout: &defaultTransport.IdleConnTimeout,
MaxRedirects: 10,
}
}

Expand Down Expand Up @@ -253,14 +254,10 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
}

// makeCheckRedirect checks if max redirects are exceeded
func makeCheckRedirect(max *int) func(*http.Request, []*http.Request) error {
if max == nil {
return nil
}

func makeCheckRedirect(max int) func(*http.Request, []*http.Request) error {
return func(_ *http.Request, via []*http.Request) error {
if len(via) > *max {
return http.ErrUseLastResponse
if len(via) > max {
return fmt.Errorf("stopped after %d redirects", max)
}
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,36 +337,36 @@ func TestHTTPClientSettingsError(t *testing.T) {
}

func TestMaxRedirects(t *testing.T) {
toIntPtr := func(i int) *int {
return &i
}
tests := []struct {
name string
settings ClientConfig
expectedRequests int
expectedErrStr string
}{
{
name: "No redirects config",
settings: ClientConfig{},
expectedRequests: 10,
name: "No redirects config (Default Go max redirects)",
settings: NewDefaultClientConfig(),
expectedRequests: 11,
// default client returns an error, custom implementations should return a ErrUseLastResponse which the internal http package will skip it to let the users select the previous response without closing the body.
expectedErrStr: "stopped after 10 redirects",
},
{
name: "Zero redirects",
settings: ClientConfig{MaxRedirects: toIntPtr(0)},
settings: ClientConfig{MaxRedirects: 0},
expectedRequests: 1,
expectedErrStr: "stopped after 0 redirects",
},
{
name: "One redirect",
settings: ClientConfig{MaxRedirects: toIntPtr(1)},
settings: ClientConfig{MaxRedirects: 1},
expectedRequests: 2,
expectedErrStr: "stopped after 1 redirects",
},
{
name: "Defined max redirects",
settings: ClientConfig{MaxRedirects: toIntPtr(5)},
settings: ClientConfig{MaxRedirects: 5},
expectedRequests: 6,
expectedErrStr: "stopped after 5 redirects",
},
}

Expand Down

0 comments on commit e6b62bb

Please sign in to comment.