diff --git a/sdk/core/Azure.Core/tests/TestFramework/DiagnosticScopeValidatingInterceptor.cs b/sdk/core/Azure.Core/tests/TestFramework/DiagnosticScopeValidatingInterceptor.cs index 7ac04f6abbea..ad7e28ba461c 100755 --- a/sdk/core/Azure.Core/tests/TestFramework/DiagnosticScopeValidatingInterceptor.cs +++ b/sdk/core/Azure.Core/tests/TestFramework/DiagnosticScopeValidatingInterceptor.cs @@ -103,4 +103,4 @@ public void Intercept(IInvocation invocation) } } } -} \ No newline at end of file +} diff --git a/sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs b/sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs index a4bf2d1bbaf2..b2721c2e4e95 100644 --- a/sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs +++ b/sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs @@ -17,6 +17,8 @@ public class RecordEntry public string RequestUri { get; set; } + public bool IsTrack1Recording { get; set; } + public RequestMethod RequestMethod { get; set; } public int StatusCode { get; set; } @@ -35,6 +37,11 @@ public static RecordEntry Deserialize(JsonElement element) record.RequestUri = property.GetString(); } + if (element.TryGetProperty("EncodedRequestUri", out property)) + { + record.IsTrack1Recording = true; + } + if (element.TryGetProperty("RequestHeaders", out property)) { DeserializeHeaders(record.Request.Headers, property); diff --git a/sdk/core/Azure.Core/tests/TestFramework/RecordMatcher.cs b/sdk/core/Azure.Core/tests/TestFramework/RecordMatcher.cs index 577423b71d0a..a439b0af2ede 100644 --- a/sdk/core/Azure.Core/tests/TestFramework/RecordMatcher.cs +++ b/sdk/core/Azure.Core/tests/TestFramework/RecordMatcher.cs @@ -74,7 +74,19 @@ public virtual RecordEntry FindMatch(Request request, IList entries { int score = 0; - if (!AreUrisSame(entry.RequestUri, uri)) + var recordRequestUri = entry.RequestUri; + if (entry.IsTrack1Recording) + { + //there's no domain name for request uri in track 1 record, so add it from reqeust uri + int len = 8; //length of "https://" + int domainEndingIndex = uri.IndexOf('/', len); + if (domainEndingIndex > 0) + { + recordRequestUri = uri.Substring(0, domainEndingIndex) + recordRequestUri; + } + } + + if (!AreUrisSame(recordRequestUri, uri)) { score++; } @@ -84,7 +96,11 @@ public virtual RecordEntry FindMatch(Request request, IList entries score++; } - score += CompareHeaderDictionaries(headers, entry.Request.Headers, ExcludeHeaders); + //we only check Uri + RequestMethod for track1 record + if (entry.IsTrack1Recording) + { + score += CompareHeaderDictionaries(headers, entry.Request.Headers, ExcludeHeaders); + } if (score == 0) { diff --git a/sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs b/sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs index 37ffcc14a049..9fb702467167 100644 --- a/sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs +++ b/sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs @@ -14,6 +14,9 @@ public class RecordSession public SortedDictionary Variables { get; } = new SortedDictionary(StringComparer.OrdinalIgnoreCase); + //Used only for deserializing track 1 session record files + public Dictionary> Names { get; set; } = new Dictionary>(); + public void Serialize(Utf8JsonWriter jsonWriter) { jsonWriter.WriteStartObject(); @@ -52,6 +55,19 @@ public static RecordSession Deserialize(JsonElement element) session.Variables[item.Name] = item.Value.GetString(); } } + + if (element.TryGetProperty(nameof(Names), out property)) + { + foreach (JsonProperty item in property.EnumerateObject()) + { + var queue = new Queue(); + foreach (JsonElement subItem in item.Value.EnumerateArray()) + { + queue.Enqueue(subItem.GetString()); + } + session.Names[item.Name] = queue; + } + } return session; } diff --git a/sdk/core/Azure.Core/tests/TestFramework/TestRecording.cs b/sdk/core/Azure.Core/tests/TestFramework/TestRecording.cs index 19ac051a2706..218aa8808f01 100644 --- a/sdk/core/Azure.Core/tests/TestFramework/TestRecording.cs +++ b/sdk/core/Azure.Core/tests/TestFramework/TestRecording.cs @@ -3,6 +3,8 @@ using System; using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text.Json; using System.Threading; @@ -90,7 +92,15 @@ public TestRandom Random _random = new TestRandom(Mode, seed); break; case RecordedTestMode.Playback: - _random = new TestRandom(Mode, int.Parse(_session.Variables[RandomSeedVariableKey])); + if (IsTrack1SessionRecord()) + { + //random is not really used for track 1 playback, so randomly pick one as seed + _random = new TestRandom(Mode, (int)DateTime.UtcNow.Ticks); + } + else + { + _random = new TestRandom(Mode, int.Parse(_session.Variables[RandomSeedVariableKey])); + } break; default: throw new ArgumentOutOfRangeException(); @@ -201,6 +211,23 @@ public string GenerateId() return Random.Next().ToString(); } + public string GenerateAssetName(string prefix, [CallerMemberName]string callerMethodName = "testframework_failed") + { + if (Mode == RecordedTestMode.Playback && IsTrack1SessionRecord()) + { + return _session.Names[callerMethodName].Dequeue(); + } + else + { + return prefix + Random.Next(9999); + } + } + + public bool IsTrack1SessionRecord() + { + return _session.Entries.FirstOrDefault()?.IsTrack1Recording ?? false; + } + public string GetVariable(string variableName, string defaultValue) { switch (Mode)