feat(oonirun): add support for OONIRun v2 links (#844)

This diff adds support for OONIRun v2 links.

Part of https://github.com/ooni/probe/issues/2184.
This commit is contained in:
Simone Basso
2022-07-08 16:53:59 +02:00
committed by GitHub
parent ebb78c2848
commit 9a0153a349
11 changed files with 644 additions and 22 deletions
+11 -13
View File
@@ -148,21 +148,19 @@ func (c *apiClient) newRequestWithJSONBody(
return request, nil
}
// joinURLPath appends the path of resource URL to the baseURL taking
// care of multiple forward slashes gracefully.
func (c *apiClient) joinURLPath(origPath string, newPath string) string {
// If the BaseURL path doesn't end with a slash, added one
if !strings.HasSuffix(origPath, "/") {
origPath += "/"
// joinURLPath appends resourcePath to the urlPath.
func (c *apiClient) joinURLPath(urlPath, resourcePath string) string {
if resourcePath == "" {
if urlPath == "" {
return "/"
}
return urlPath
}
// If the resourceURL path has a leading slash, it is removed
if strings.HasPrefix(newPath, "/") {
newPath = newPath[1:]
if !strings.HasSuffix(urlPath, "/") {
urlPath += "/"
}
return origPath + newPath
resourcePath = strings.TrimPrefix(resourcePath, "/")
return urlPath + resourcePath
}
// newRequest creates a new request.
+36 -6
View File
@@ -94,12 +94,27 @@ func newAPIClient() *apiClient {
}
func TestJoinURLPath(t *testing.T) {
t.Run("the whole path is inside basePath and there's no resource path", func(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com/robots.txt"
req, err := ac.newRequest(context.Background(), "GET", "", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "https://example.com/robots.txt" {
t.Fatal("unexpected result", req.URL.String())
}
})
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 err != nil {
t.Fatal(err)
}
if req.URL.String() != "https://example.com/foo" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
@@ -107,8 +122,11 @@ func TestJoinURLPath(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com/"
req, err := ac.newRequest(context.Background(), "GET", "/foo", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "https://example.com/foo" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
@@ -116,8 +134,11 @@ func TestJoinURLPath(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "https://example.com"
req, err := ac.newRequest(context.Background(), "GET", "", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "https://example.com/" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
@@ -125,8 +146,11 @@ func TestJoinURLPath(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo"
req, err := ac.newRequest(context.Background(), "GET", "/bar", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
@@ -134,8 +158,11 @@ func TestJoinURLPath(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo/"
req, err := ac.newRequest(context.Background(), "GET", "/bar", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
@@ -143,8 +170,11 @@ func TestJoinURLPath(t *testing.T) {
ac := newAPIClient()
ac.BaseURL = "http://example.com/foo/"
req, err := ac.newRequest(context.Background(), "GET", "bar", nil, nil)
if err != nil {
t.Fatal(err)
}
if req.URL.String() != "http://example.com/foo/bar" {
t.Fatal("unexpected result", err)
t.Fatal("unexpected result", req.URL.String())
}
})
}