-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added recursive query parameter and fixed some linting errors (#2368)
- Loading branch information
1 parent
9500654
commit a5fe78e
Showing
11 changed files
with
198 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright © Microsoft <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// CtxRecursiveKey is used as a context key to apply the recursive query parameter. | ||
type CtxRecursiveKey struct{} | ||
|
||
// WithRecursive applies the recursive parameter to the request. | ||
func WithRecursive(parent context.Context, recursive bool) context.Context { | ||
return context.WithValue(parent, CtxRecursiveKey{}, recursive) | ||
} | ||
|
||
type recursivePolicy struct { | ||
} | ||
|
||
// NewRecursivePolicy creates a policy that applies the recursive parameter to the request. | ||
func NewRecursivePolicy() policy.Policy { | ||
return &recursivePolicy{} | ||
} | ||
|
||
func (p *recursivePolicy) Do(req *policy.Request) (*http.Response, error) { | ||
if recursive := req.Raw().Context().Value(CtxRecursiveKey{}); recursive != nil { | ||
if req.Raw().URL.Query().Has("recursive") { | ||
query := req.Raw().URL.Query() | ||
query.Set("recursive", strconv.FormatBool(recursive.(bool))) | ||
req.Raw().URL.RawQuery = query.Encode() | ||
} | ||
} | ||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
type testRecursive struct{ | ||
recursive string | ||
} | ||
|
||
func (t testRecursive) Do(req *policy.Request) (*http.Response, error) { | ||
if req.Raw().URL.Query().Has("recursive") { | ||
if req.Raw().URL.Query().Get("recursive") == t.recursive { | ||
return &http.Response{}, nil | ||
} | ||
} | ||
return &http.Response{}, fmt.Errorf("recursive query parameter not found or does not match expected value. expected: %s, actual: %s", t.recursive, req.Raw().URL.Query().Get("recursive")) | ||
} | ||
|
||
func TestRecursivePolicyExpectTrue(t *testing.T) { | ||
a := assert.New(t) | ||
ctx := WithRecursive(context.Background(), true) | ||
policies := []policy.Policy{NewRecursivePolicy(), testRecursive{"true"}} | ||
p := runtime.NewPipeline("testmodule", "v0.1.0", runtime.PipelineOptions{}, &policy.ClientOptions{Transport: nil, PerCallPolicies: policies}) | ||
|
||
endpoints := []string{"https://xxxx.dfs.core.windows.net/container/path?recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=true&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=true&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=false&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=false",} | ||
|
||
for _, e := range endpoints { | ||
req, err := runtime.NewRequest(ctx, "HEAD", e) | ||
a.Nil(err) | ||
_, err = p.Do(req) | ||
a.Nil(err) | ||
} | ||
} | ||
|
||
func TestRecursivePolicyExpectFalse(t *testing.T) { | ||
a := assert.New(t) | ||
ctx := WithRecursive(context.Background(), false) | ||
policies := []policy.Policy{NewRecursivePolicy(), testRecursive{"false"}} | ||
p := runtime.NewPipeline("testmodule", "v0.1.0", runtime.PipelineOptions{}, &policy.ClientOptions{Transport: nil, PerCallPolicies: policies}) | ||
|
||
endpoints := []string{"https://xxxx.dfs.core.windows.net/container/path?recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=true&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=true&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=false&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=false",} | ||
|
||
for _, e := range endpoints { | ||
req, err := runtime.NewRequest(ctx, "HEAD", e) | ||
a.Nil(err) | ||
_, err = p.Do(req) | ||
a.Nil(err) | ||
} | ||
} | ||
|
||
type testEndpoint struct{ | ||
endpoint string | ||
} | ||
|
||
func (t testEndpoint) Do(req *policy.Request) (*http.Response, error) { | ||
if req.Raw().URL.String() == t.endpoint { | ||
return &http.Response{}, nil | ||
} | ||
return &http.Response{}, fmt.Errorf("recursive query parameter not found or does not match expected value. expected: %s, actual: %s", t.endpoint, req.Raw().URL.String()) | ||
} | ||
|
||
func TestRecursivePolicyExpectNoChange(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
endpoints := []string{"https://xxxx.dfs.core.windows.net/container/path?recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=true&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=true&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=true", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false", | ||
"https://xxxx.dfs.core.windows.net/container/path?recursive=false&sig=xxxxxx&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&recursive=false&snapshot=xxxxx&timeout=xxxx", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx&recursive=false", | ||
"https://xxxx.dfs.core.windows.net/container/path", | ||
"https://xxxx.dfs.core.windows.net/container/path?sig=xxxxxx&snapshot=xxxxx&timeout=xxxx",} | ||
|
||
for _, e := range endpoints { | ||
policies := []policy.Policy{NewRecursivePolicy(), testEndpoint{e}} | ||
p := runtime.NewPipeline("testmodule", "v0.1.0", runtime.PipelineOptions{}, &policy.ClientOptions{Transport: nil, PerCallPolicies: policies}) | ||
req, err := runtime.NewRequest(context.Background(), "HEAD", e) | ||
a.Nil(err) | ||
_, err = p.Do(req) | ||
a.Nil(err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.