-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bake UserAgent into http.Clients (#92)
- Loading branch information
1 parent
01211fb
commit fa14e95
Showing
5 changed files
with
47 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
type roundTripperFunc func(*http.Request) (*http.Response, error) | ||
|
||
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } | ||
|
||
func userAgentTransport(next http.RoundTripper, userAgent string) http.RoundTripper { | ||
return roundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
req.Header.Set("User-Agent", userAgent) | ||
return next.RoundTrip(req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestUserAgentTransport(t *testing.T) { | ||
c := make(chan string, 1) | ||
handler := func(_ http.ResponseWriter, r *http.Request) { c <- r.Header.Get("User-Agent") } | ||
server := httptest.NewServer(http.HandlerFunc(handler)) | ||
defer server.Close() | ||
|
||
userAgent := "something" | ||
transport := userAgentTransport(http.DefaultTransport, userAgent) | ||
client := &http.Client{Transport: transport} | ||
if _, err := client.Get(server.URL); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if want, have := userAgent, <-c; want != have { | ||
t.Fatalf("want %q, have %q", want, have) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters