Skip to content

Commit

Permalink
make client config's UserAgent property optional
Browse files Browse the repository at this point in the history
The value is useful for debugging, but it's not being set shouldn't stop a
test or any other process from running.

Also fixed a few calls to Go stdlib deprecated functions.
  • Loading branch information
ewollesen committed Nov 7, 2023
1 parent 5f016bb commit ed7a9a5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 70 deletions.
3 changes: 1 addition & 2 deletions alerts/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ var _ = Describe("Client", func() {
func buildTestClient(s *httptest.Server) *Client {
pCfg := &platform.Config{
Config: &client.Config{
Address: s.URL,
UserAgent: "foo",
Address: s.URL,
},
}
token := mockTokenProvider(testToken)
Expand Down
4 changes: 3 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func (c *Client) createRequest(ctx context.Context, method string, url string, m
return nil, errors.New("url is missing")
}

mutators = append(mutators, request.NewHeaderMutator("User-Agent", c.userAgent))
if c.userAgent != "" {
mutators = append(mutators, request.NewHeaderMutator("User-Agent", c.userAgent))
}

var body io.Reader
if requestBody != nil {
Expand Down
78 changes: 39 additions & 39 deletions client/client_test.go

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import (
)

type Config struct {
Address string // this should be overridden for loaders using envconfig
Address string // this should be overridden for loaders using envconfig
// UserAgent is an optional way for a client to identify itself.
//
// This is usually set to the name of the service that's using the
// client. If left empty, the default Go http.Client value should be used.
//
// This value can be helpful when debugging. But remember that these
// values can be spoofed, so when in doubt, verify the client's source IP.
//
// More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
UserAgent string `envconfig:"TIDEPOOL_USER_AGENT"`
}

Expand All @@ -28,9 +37,6 @@ func (c *Config) Validate() error {
} else if _, err := url.Parse(c.Address); err != nil {
return errors.New("address is invalid")
}
if c.UserAgent == "" {
return errors.New("user agent is missing")
}

return nil
}
Expand Down
5 changes: 0 additions & 5 deletions client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ var _ = Describe("Config", func() {
Expect(cfg.Validate()).To(MatchError("address is invalid"))
})

It("returns an error if the user agent is missing", func() {
cfg.UserAgent = ""
Expect(cfg.Validate()).To(MatchError("user agent is missing"))
})

It("returns success", func() {
Expect(cfg.Validate()).To(Succeed())
Expect(cfg.Address).To(Equal(address))
Expand Down
7 changes: 0 additions & 7 deletions data/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ var _ = Describe("Client", func() {
Expect(clnt).To(BeNil())
})

It("returns an error if config user agent is missing", func() {
config.UserAgent = ""
clnt, err := dataClient.New(config, platform.AuthorizeAsService)
Expect(err).To(MatchError("config is invalid; user agent is missing"))
Expect(clnt).To(BeNil())
})

It("returns success", func() {
clnt, err := dataClient.New(config, platform.AuthorizeAsService)
Expect(err).ToNot(HaveOccurred())
Expand Down
7 changes: 0 additions & 7 deletions metric/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ var _ = Describe("Client", func() {
Expect(clnt).To(BeNil())
})

It("returns an error if config user agent is missing", func() {
config.UserAgent = ""
clnt, err := metricClient.New(config, platform.AuthorizeAsUser, name, versionReporter)
Expect(err).To(MatchError("config is invalid; user agent is missing"))
Expect(clnt).To(BeNil())
})

It("returns success", func() {
clnt, err := metricClient.New(config, platform.AuthorizeAsUser, name, versionReporter)
Expect(err).ToNot(HaveOccurred())
Expand Down
5 changes: 0 additions & 5 deletions platform/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ var _ = Describe("Config", func() {
Expect(cfg.Validate()).To(MatchError("address is invalid"))
})

It("returns an error if the user agent is missing", func() {
cfg.UserAgent = ""
Expect(cfg.Validate()).To(MatchError("user agent is missing"))
})

It("returns success", func() {
Expect(cfg.Validate()).To(Succeed())
Expect(cfg.Config).ToNot(BeNil())
Expand Down

0 comments on commit ed7a9a5

Please sign in to comment.