fix(httpx): correctly combine paths (#706)

See https://github.com/ooni/probe/issues/2010

Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
Yeganathan S 2022-05-09 19:32:49 +00:00 committed by GitHub
commit 3d81845614
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 5 deletions

View file

@ -93,6 +93,62 @@ func newAPIClient() *apiClient {
}
}
func TestJoinURLPath(t *testing.T) {
t.Run("empty baseURL path and slash-prefixed resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com"
req, err := ac.newRequest(context.Background(), "GET", "/foo", nil, nil)
if req.URL.String() != "https://example.com/foo" {
t.Fatal("unexpected result", err)
}
})
t.Run("root baseURL path and slash-prefixed resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com/"
req, err := ac.newRequest(context.Background(), "GET", "/foo", nil, nil)
if req.URL.String() != "https://example.com/foo" {
t.Fatal("unexpected result", err)
}
})
t.Run("empty baseURL path and empty resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com"
req, err := ac.newRequest(context.Background(), "GET", "", nil, nil)
if req.URL.String() != "https://example.com/" {
t.Fatal("unexpected result", err)
}
})
t.Run("non-slash-terminated baseURL path and slash-prefixed resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo"
req, err := ac.newRequest(context.Background(), "GET", "/bar", nil, nil)
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
}
})
t.Run("slash-terminated baseURL path and slash-prefixed resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo/"
req, err := ac.newRequest(context.Background(), "GET", "/bar", nil, nil)
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
}
})
t.Run("slash-terminated baseURL path and non-slash-prefixed resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo/"
req, err := ac.newRequest(context.Background(), "GET", "bar", nil, nil)
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
}
})
}
// fakeRequest is a fake request we serialize.
type fakeRequest struct {
Name string