Skip to content

Commit

Permalink
adding a policy to do the same thing
Browse files Browse the repository at this point in the history
  • Loading branch information
seankane-msft committed Jul 26, 2021
1 parent 79b6a2b commit 9e00c51
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
38 changes: 36 additions & 2 deletions sdk/internal/recording/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/internal/uuid"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
Expand Down Expand Up @@ -454,11 +455,11 @@ func (t TestProxyTransport) Do(req *http.Request) (*http.Response, error) {
originalURLHost := req.URL.Host
req.Header.Set("x-recording-upstream-base-uri", originalURLHost)
req.URL.Host = "localhost:5001"
fmt.Println(req.URL.String())
req.URL.Scheme = "https"
fmt.Println(req.URL.Host, req.Header.Get("x-recording-upstream-base-uri"), req.URL.Path, req.Method)
req.Header.Set(recordingIdHeader, recordingId)
req.Header.Set(recordingModeHeader, recordMode)
fmt.Println("MAKING THE HTTP CALL")
fmt.Println("MAKING THE HTTP CALL TO: ", req.URL.String())
response, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("ERROR: ", err)
Expand Down Expand Up @@ -507,3 +508,36 @@ func StopRecording(t *testing.T) error {
fmt.Println("Stopped recording")
return nil
}

type RecordingOptions struct {
MaxRetries int32
}

func (o *RecordingOptions) init() {
if o.MaxRetries != 0 {
o.MaxRetries = 0
}
}

type recordingPolicy struct {
options RecordingOptions
}

func NewRecordingPolicy(o *RecordingOptions) azcore.Policy {
if o == nil {
o = &RecordingOptions{}
}
p := &recordingPolicy{options: *o}
p.options.init()
return p
}

func (p *recordingPolicy) Do(req *azcore.Request) (resp *azcore.Response, err error) {
req.Host = "localhost:5001"
req.URL.Scheme = "https"
req.URL.Host = "localhost:5001"

fmt.Println("URL: ", req.URL.String())

return req.Next()
}
15 changes: 7 additions & 8 deletions sdk/tables/aztable/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
"github.com/stretchr/testify/require"
Expand All @@ -16,24 +17,22 @@ func Test_TestProxy(t *testing.T) {
err := recording.StartRecording(t)
require.NoError(err)
defer recording.StopRecording(t)
if err != nil {
fmt.Println(err)
}

cred, err := azidentity.NewDefaultAzureCredential(nil)
require.NoError(err)
testProxyTransport := recording.TestProxyTransport{}
// testProxyTransport := recording.TestProxyTransport{}
recordingPolicy := recording.NewRecordingPolicy(nil)
options := TableClientOptions{
Scopes: []string{AADAuthenticationScope},
HTTPClient: testProxyTransport,
Scopes: []string{AADAuthenticationScope},
PerCallOptions: []azcore.Policy{recordingPolicy},
// HTTPClient: testProxyTransport,
}
client, err := NewTableClient("testproxy", "https://seankaneprim.table.core.windows.net", cred, &options)
fmt.Println("NewTableClient err: ", err)
require.NoError(err)

fmt.Println("CALLING CREATE")
_, err = client.Create(ctx)
fmt.Println("Create err: ", err)
fmt.Println("Create err: ", err.Error())
require.NoError(err)

fmt.Println("CALLING DELETE")
Expand Down

0 comments on commit 9e00c51

Please sign in to comment.