-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a user-agent across all feeds. (#438)
* Add a user-agent across all feeds. Signed-off-by: Caleb Brown <calebbrown@google.com> * Fix linter errors Signed-off-by: Caleb Brown <calebbrown@google.com> * Add user-agent to maven central Signed-off-by: Caleb Brown <calebbrown@google.com> * Fix maven lint and conform it to the style of other feeds. Signed-off-by: Caleb Brown <calebbrown@google.com> * Fix last lint error Signed-off-by: Caleb Brown <calebbrown@google.com> --------- Signed-off-by: Caleb Brown <calebbrown@google.com>
- Loading branch information
1 parent
975d2ec
commit a5f3088
Showing
13 changed files
with
152 additions
and
17 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
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
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
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
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
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,21 @@ | ||
package useragent | ||
|
||
import "net/http" | ||
|
||
type RoundTripper struct { | ||
UserAgent string | ||
Parent http.RoundTripper | ||
} | ||
|
||
func (rt *RoundTripper) RoundTrip(ireq *http.Request) (*http.Response, error) { | ||
req := ireq.Clone(ireq.Context()) | ||
req.Header.Set("User-Agent", rt.UserAgent) | ||
return rt.parent().RoundTrip(req) | ||
} | ||
|
||
func (rt *RoundTripper) parent() http.RoundTripper { | ||
if rt.Parent != nil { | ||
return rt.Parent | ||
} | ||
return http.DefaultTransport | ||
} |
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,75 @@ | ||
package useragent_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/ossf/package-feeds/pkg/useragent" | ||
) | ||
|
||
func TestRoundTripper(t *testing.T) { | ||
t.Parallel() | ||
want := "test user agent string" | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
got := r.Header.Get("user-agent") | ||
if got != want { | ||
t.Errorf("User Agent = %q, want %q", got, want) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer ts.Close() | ||
|
||
c := http.Client{ | ||
Transport: &useragent.RoundTripper{UserAgent: want}, | ||
} | ||
resp, err := c.Get(ts.URL) | ||
if err != nil { | ||
t.Fatalf("Get() = %v; want no error", err) | ||
} | ||
resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
t.Fatalf("Get() status = %v; want 200", resp.StatusCode) | ||
} | ||
} | ||
|
||
type roundTripperFunc func(*http.Request) (*http.Response, error) | ||
|
||
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { | ||
return rt(r) | ||
} | ||
|
||
func TestRoundTripper_Parent(t *testing.T) { | ||
t.Parallel() | ||
want := "test user agent string" | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
got := r.Header.Get("user-agent") | ||
if got != want { | ||
t.Errorf("User Agent = %q, want %q", got, want) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer ts.Close() | ||
|
||
calledParent := false | ||
c := http.Client{ | ||
Transport: &useragent.RoundTripper{ | ||
UserAgent: want, | ||
Parent: roundTripperFunc(func(r *http.Request) (*http.Response, error) { | ||
calledParent = true | ||
return http.DefaultTransport.RoundTrip(r) | ||
}), | ||
}, | ||
} | ||
resp, err := c.Get(ts.URL) | ||
if err != nil { | ||
t.Fatalf("Get() = %v; want no error", err) | ||
} | ||
resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
t.Fatalf("Get() status = %v; want 200", resp.StatusCode) | ||
} | ||
if !calledParent { | ||
t.Errorf("Failed to call Parent RoundTripper") | ||
} | ||
} |