refactor: migrate apitool from netx to netxlite (#496)
I discovered which transport were used by apitool and made sure he gets the same transports now. While there, I discovered an issue with ooni/oohttp that has been fixed with https://github.com/ooni/oohttp/commit/cba9b1ce5ecf7a20217ad8571ad7a85e490c4789. Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
@@ -1,19 +1,9 @@
|
||||
package httptransport
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// UserAgentTransport is a transport that ensures that we always
|
||||
// set an OONI specific default User-Agent header.
|
||||
type UserAgentTransport struct {
|
||||
RoundTripper
|
||||
}
|
||||
|
||||
// RoundTrip implements RoundTripper.RoundTrip
|
||||
func (txp UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if req.Header.Get("User-Agent") == "" {
|
||||
req.Header.Set("User-Agent", "miniooni/0.1.0-dev")
|
||||
}
|
||||
return txp.RoundTripper.RoundTrip(req)
|
||||
}
|
||||
|
||||
var _ RoundTripper = UserAgentTransport{}
|
||||
type UserAgentTransport = netxlite.UserAgentTransport
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
package httptransport_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
|
||||
)
|
||||
|
||||
func TestUserAgentWithDefault(t *testing.T) {
|
||||
txp := httptransport.UserAgentTransport{
|
||||
RoundTripper: httptransport.FakeTransport{
|
||||
Resp: &http.Response{StatusCode: 200},
|
||||
},
|
||||
}
|
||||
req := &http.Request{URL: &url.URL{
|
||||
Scheme: "https",
|
||||
Host: "www.google.com",
|
||||
Path: "/",
|
||||
}}
|
||||
req.Header = http.Header{}
|
||||
resp, err := txp.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Request.Header.Get("User-Agent") != "miniooni/0.1.0-dev" {
|
||||
t.Fatal("not the User-Agent we expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAgentWithExplicitValue(t *testing.T) {
|
||||
txp := httptransport.UserAgentTransport{
|
||||
RoundTripper: httptransport.FakeTransport{
|
||||
Resp: &http.Response{StatusCode: 200},
|
||||
},
|
||||
}
|
||||
req := &http.Request{URL: &url.URL{
|
||||
Scheme: "https",
|
||||
Host: "www.google.com",
|
||||
Path: "/",
|
||||
}}
|
||||
req.Header = http.Header{"User-Agent": []string{"antani-client/0.1.1"}}
|
||||
resp, err := txp.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Request.Header.Get("User-Agent") != "antani-client/0.1.1" {
|
||||
t.Fatal("not the User-Agent we expected")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user