Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azcore] JoinPaths will preserve query parameters #15258

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 23 additions & 14 deletions sdk/azcore/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
seankane-msft marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasSuffix(root, "/") {
root += "/"
}
path += paths[i]
return root + "?" + qps
}
return path
return root
}

// NewRequest creates a new Request with the specified input.
Expand Down
9 changes: 7 additions & 2 deletions sdk/azcore/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down