diff --git a/sdk/azcore/CHANGELOG.md b/sdk/azcore/CHANGELOG.md index 741d9fdcf01c..c5711ee628a9 100644 --- a/sdk/azcore/CHANGELOG.md +++ b/sdk/azcore/CHANGELOG.md @@ -3,10 +3,13 @@ ## v0.19.0-beta.1 (Unreleased) ### Features Added +* Adds an `ETag` type for comparing etags and handling etags on requests +* Simplifies the `requestBodyProgess` and `responseBodyProgress` into a single `progress` object ### Breaking Changes ### Bugs Fixed +* `JoinPaths` will preserve query parameters encoded in the `root` url. ### Other Changes diff --git a/sdk/azcore/request.go b/sdk/azcore/request.go index b01b3ded5c40..e4e0cc509280 100644 --- a/sdk/azcore/request.go +++ b/sdk/azcore/request.go @@ -67,23 +67,32 @@ func (ov opValues) get(value interface{}) bool { } // JoinPaths concatenates multiple URL path segments into one path, -// inserting path separation characters as required. -func JoinPaths(paths ...string) string { +// inserting path separation characters as required. JoinPaths will preserve +// query parameters in the root path +func JoinPaths(root string, paths ...string) string { if len(paths) == 0 { - return "" - } - path := paths[0] - for i := 1; i < len(paths); i++ { - if path[len(path)-1] == '/' && paths[i][0] == '/' { - // strip off trailing '/' to avoid doubling up - path = path[:len(path)-1] - } else if path[len(path)-1] != '/' && paths[i][0] != '/' { - // add a trailing '/' - path = path + "/" + return root + } + + qps := "" + if strings.Contains(root, "?") { + splitPath := strings.Split(root, "?") + root, qps = splitPath[0], splitPath[1] + } + + for i := 0; i < len(paths); i++ { + root = strings.TrimRight(root, "/") + paths[i] = strings.TrimLeft(paths[i], "/") + root += "/" + paths[i] + } + + if qps != "" { + if !strings.HasSuffix(root, "/") { + root += "/" } - path += paths[i] + return root + "?" + qps } - return path + return root } // NewRequest creates a new Request with the specified input. diff --git a/sdk/azcore/request_test.go b/sdk/azcore/request_test.go index f4fc75acdcc4..994d0a6132a1 100644 --- a/sdk/azcore/request_test.go +++ b/sdk/azcore/request_test.go @@ -510,13 +510,18 @@ func TestNewRequestFail(t *testing.T) { } func TestJoinPaths(t *testing.T) { - if path := JoinPaths(); path != "" { + if path := JoinPaths(""); path != "" { t.Fatalf("unexpected path %s", path) } - const expected = "http://test.contoso.com/path/one/path/two/path/three/path/four/" + expected := "http://test.contoso.com/path/one/path/two/path/three/path/four/" if path := JoinPaths("http://test.contoso.com/", "/path/one", "path/two", "/path/three/", "path/four/"); path != expected { t.Fatalf("got %s, expected %s", path, expected) } + + expected = "http://test.contoso.com/path/one/path/two/?qp1=abc&qp2=def" + if path := JoinPaths("http://test.contoso.com/?qp1=abc&qp2=def", "/path/one", "path/two"); path != expected { + t.Fatalf("got %s, expected %s", path, expected) + } } func TestRequestValidFail(t *testing.T) {