Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set custom user-agent on all requests #84

Merged
merged 1 commit into from
Nov 28, 2022

Conversation

rtrox
Copy link
Contributor

@rtrox rtrox commented Nov 20, 2022

This PR sets a custom user-agent on all requests set by the speedtest library: showwin/speedtest-go <version>. This UserAgent can be overridden with a new Option, WithUserAgent. This WithUserAgent option is just a convenience function -- it wraps the transport on the client's Doer to add a user-agent to all requests. As such, you can use it with a custom Doer if you want (as long as you pass the custom doer first), or you can just implement your own User-Agent on the doer you pass in.

I thought this might be a little nicer way to handle user-agents, but this PR is definitely a little opinionated, glad to adjust or close, just lmk :-)

New Unit Tests are added to test the user-agent behavior, and all tests pass:

 ~/speedtest-go ❯❯❯ go test ./... -v
?       github.com/showwin/speedtest-go [no test files]
=== RUN   TestDownloadTestContext
--- PASS: TestDownloadTestContext (0.60s)
=== RUN   TestDownloadTestContextSavingMode
--- PASS: TestDownloadTestContextSavingMode (0.60s)
=== RUN   TestUploadTestContext
--- PASS: TestUploadTestContext (0.60s)
=== RUN   TestUploadTestContextSavingMode
--- PASS: TestUploadTestContextSavingMode (0.60s)
=== RUN   TestFetchServerList
--- PASS: TestFetchServerList (0.17s)
=== RUN   TestDistance
--- PASS: TestDistance (0.00s)
=== RUN   TestFindServer
--- PASS: TestFindServer (0.00s)
=== RUN   TestNew
=== RUN   TestNew/DefaultDoer
=== RUN   TestNew/CustomDoer
--- PASS: TestNew (0.00s)
    --- PASS: TestNew/DefaultDoer (0.00s)
    --- PASS: TestNew/CustomDoer (0.00s)
=== RUN   TestUserAgent
=== RUN   TestUserAgent/DefaultUserAgent
=== RUN   TestUserAgent/CustomUserAgent
=== RUN   TestUserAgent/CustomUserAgentAndDoer
--- PASS: TestUserAgent (0.00s)
    --- PASS: TestUserAgent/DefaultUserAgent (0.00s)
    --- PASS: TestUserAgent/CustomUserAgent (0.00s)
    --- PASS: TestUserAgent/CustomUserAgentAndDoer (0.00s)
=== RUN   TestFetchUserInfo
--- PASS: TestFetchUserInfo (0.05s)
PASS
ok      github.com/showwin/speedtest-go/speedtest       2.628s

Note: to access the speedtest-go version, I had to move it to speedtest.go, so if you like this behavior, new releases will need to bump the version there instead.

@showwin
Copy link
Owner

showwin commented Nov 28, 2022

@rtrox Thank you for making it easy to change 😄

@mikaelpeltier Hi✋ The way to add HTTP Header is changed by this PR, please take care about this change 🙏

@showwin showwin merged commit 6b57ad2 into showwin:master Nov 28, 2022
@showwin showwin mentioned this pull request Nov 28, 2022
@rtrox
Copy link
Contributor Author

rtrox commented Dec 3, 2022

@mikaelpeltier - the default user agent is changed, but with this change, there's still a user agent set on all requests, so I don't think it should regress the issue you reported. If it does, lemme know and I'm glad to debug!

// New creates a new speedtest client.
func New(opts ...Option) *Speedtest {
s := &Speedtest{
doer: http.DefaultClient,
}
WithUserAgent(defaultUserAgent)(s)

for _, opt := range opts {
opt(s)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rtrox. It seems that WithDoer and WithUserAgent can not be used at the same time, because WithDoer will always be executed after WithUserAgent in New(opts ...Option).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants