@@ -8,95 +8,51 @@ package testutil
88
99import (
1010 "fmt"
11- "net/http"
12- "strconv"
1311 "testing"
1412
15- "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1613 "github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
1714)
1815
19- const recordingRandomSeedVariableName = "recordingRandomSeed"
20-
21- var (
22- recordingSeed int64
23- )
24-
25- type recordingPolicy struct {
26- options recording.RecordingOptions
27- t * testing.T
28- }
29-
30- // Host of the test proxy.
31- func (r * recordingPolicy ) Host () string {
32- if r .options .UseHTTPS {
33- return "localhost:5001"
34- }
35- return "localhost:5000"
36- }
37-
38- // Scheme of the test proxy.
39- func (r * recordingPolicy ) Scheme () string {
40- if r .options .UseHTTPS {
41- return "https"
42- }
43- return "http"
44- }
45-
46- // NewRecordingPolicy will create a recording policy which can be used in pipeline.
47- // The policy will change the destination of the request to the proxy server and add required header for the recording test.
48- func NewRecordingPolicy (t * testing.T , o * recording.RecordingOptions ) policy.Policy {
49- if o == nil {
50- o = & recording.RecordingOptions {UseHTTPS : true }
51- }
52- p := & recordingPolicy {options : * o , t : t }
53- return p
54- }
55-
56- // Do with recording mode.
57- // When handling live request, the policy will do nothing.
58- // Otherwise, the policy will replace the URL of the request with the test proxy endpoint.
59- // After request, the policy will change back to the original URL for the request to prevent wrong polling URL for LRO.
60- func (r * recordingPolicy ) Do (req * policy.Request ) (resp * http.Response , err error ) {
61- if recording .GetRecordMode () != "live" && ! recording .IsLiveOnly (r .t ) {
62- oriSchema := req .Raw ().URL .Scheme
63- oriHost := req .Raw ().URL .Host
64- req .Raw ().URL .Scheme = r .Scheme ()
65- req .Raw ().URL .Host = r .Host ()
66- req .Raw ().Host = r .Host ()
16+ // StartProxy starts the test proxy with the path to store test recording file.
17+ // It should be used in the module test preparation stage only once.
18+ // It will return a delegate function to stop test proxy.
19+ func StartProxy (pathToPackage string ) func () {
20+ if recording .GetRecordMode () == recording .PlaybackMode || recording .GetRecordMode () == recording .RecordingMode {
21+ proxy , err := recording .StartTestProxy (pathToPackage , nil )
22+ if err != nil {
23+ panic (fmt .Sprintf ("Failed to start recording proxy: %v" , err ))
24+ }
6725
68- // replace request target to use test proxy
69- req .Raw ().Header .Set (recording .UpstreamURIHeader , fmt .Sprintf ("%v://%v" , oriSchema , oriHost ))
70- req .Raw ().Header .Set (recording .ModeHeader , recording .GetRecordMode ())
71- req .Raw ().Header .Set (recording .IDHeader , recording .GetRecordingId (r .t ))
26+ // sanitizer for any uuid string, e.g., subscriptionID
27+ err = recording .AddGeneralRegexSanitizer ("00000000-0000-0000-0000-000000000000" , `[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}` , proxy .Options )
28+ if err != nil {
29+ panic (fmt .Sprintf ("Failed to add uuid sanitizer: %v" , err ))
30+ }
31+ // consolidate resource group name for recording and playback
32+ err = recording .AddGeneralRegexSanitizer (recording .SanitizedValue , `go-sdk-test-\d+` , proxy .Options )
33+ if err != nil {
34+ panic (fmt .Sprintf ("Failed to add resource group name sanitizer: %v" , err ))
35+ }
36+ // disable location header sanitizer
37+ err = recording .RemoveRegisteredSanitizers ([]string {"AZSDK2003" , "AZSDK2030" }, proxy .Options )
38+ if err != nil {
39+ panic (fmt .Sprintf ("Failed to remove location header sanitizer: %v" , err ))
40+ }
7241
73- resp , err = req . Next ()
74- // for any lro operation, need to change back to the original target to prevent
75- if resp != nil {
76- resp . Request . URL . Scheme = oriSchema
77- resp . Request . URL . Host = oriHost
42+ return func () {
43+ err := recording . StopTestProxy ( proxy )
44+ if err != nil {
45+ panic ( fmt . Sprintf ( "Failed to stop recording proxy: %v" , err ))
46+ }
7847 }
79- return resp , err
80- } else {
81- return req .Next ()
8248 }
49+ return func () {}
8350}
8451
8552// StartRecording starts the recording with the path to store recording file.
8653// It will return a delegate function to stop recording.
8754func StartRecording (t * testing.T , pathToPackage string ) func () {
88- option := & recording.RecordingOptions {UseHTTPS : true }
89- // sanitizer for any uuid string, e.g., subscriptionID
90- err := recording .AddGeneralRegexSanitizer ("00000000-0000-0000-0000-000000000000" , `[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}` , option )
91- if err != nil {
92- t .Fatalf ("Failed to add uuid sanitizer: %v" , err )
93- }
94- // consolidate resource group name for recording and playback
95- err = recording .AddGeneralRegexSanitizer ("go-sdk-test-rg" , `go-sdk-test-\d+` , option )
96- if err != nil {
97- t .Fatalf ("Failed to add resource group name sanitizer: %v" , err )
98- }
99- err = recording .Start (t , pathToPackage , option )
55+ err := recording .Start (t , pathToPackage , nil )
10056 if err != nil {
10157 t .Fatalf ("Failed to start recording: %v" , err )
10258 }
@@ -105,7 +61,7 @@ func StartRecording(t *testing.T, pathToPackage string) func() {
10561
10662// StopRecording stops the recording.
10763func StopRecording (t * testing.T ) {
108- err := recording .Stop (t , & recording. RecordingOptions { Variables : map [ string ] interface {}{ recordingRandomSeedVariableName : strconv . FormatInt ( recordingSeed , 10 )}} )
64+ err := recording .Stop (t , nil )
10965 if err != nil {
11066 t .Fatalf ("Failed to stop recording: %v" , err )
11167 }
0 commit comments