From 7b8a6a1ce5de8012452620aa1969dc549ed0b190 Mon Sep 17 00:00:00 2001 From: Scott Beddall Date: Tue, 30 Jul 2024 15:58:13 -0700 Subject: [PATCH] when failing to match, express the reasoning --- .../RecordSessionTests.cs | 1 + .../Common/RecordMatcher.cs | 14 +++++++++++--- .../Common/RecordSession.cs | 3 ++- .../Azure.Sdk.Tools.TestProxy/RecordingHandler.cs | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs index 4e5a8ee765b..c3562c89370 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs @@ -402,6 +402,7 @@ public void RecordMatcherThrowsExceptionsWithDetails() TestRecordingMismatchException exception = Assert.Throws(() => matcher.FindMatch(requestEntry, entries)); Assert.Equal( "Unable to find a record for the request HEAD http://localhost/" + Environment.NewLine + + "Remaining entry: http://remote-host" + Environment.NewLine + "Method doesn't match, request record " + Environment.NewLine + "Uri doesn't match:" + Environment.NewLine + " request " + Environment.NewLine + diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs index b320e79bf73..aa8bc59dbe0 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using Azure.Core; @@ -127,7 +127,7 @@ public virtual RecordEntry FindMatch(RecordEntry request, IList ent } } - throw new TestRecordingMismatchException(GenerateException(request, bestScoreEntry)); + throw new TestRecordingMismatchException(GenerateException(request, bestScoreEntry, entries)); } public virtual int CompareBodies(byte[] requestBody, byte[] recordBody, StringBuilder descriptionBuilder = null) @@ -213,11 +213,19 @@ private string NormalizeUri(string uriToNormalize) return req.ToUri().ToString(); } - private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry) + private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry, IList entries = null) { StringBuilder builder = new StringBuilder(); builder.AppendLine($"Unable to find a record for the request {request.RequestMethod} {request.RequestUri}"); + if (entries != null) + { + foreach (var entry in entries) + { + builder.AppendLine($"Remaining entry: {entry.RequestUri}"); + } + } + if (bestScoreEntry == null) { builder.AppendLine("No records to match."); diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordSession.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordSession.cs index 8cd39ca831b..e72d274767d 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordSession.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordSession.cs @@ -82,7 +82,7 @@ public void Record(RecordEntry entry) } } - public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnumerable sanitizers, bool remove = true) + public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnumerable sanitizers, bool remove = true, string sessionId = null) { foreach (RecordedTestSanitizer sanitizer in sanitizers) { @@ -97,6 +97,7 @@ public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnum if (remove) { Entries.Remove(entry); + DebugLogger.LogDebug($"We successfully matched and popped request URI {entry.RequestUri} for recordingId {sessionId}"); } return entry; diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs index c0d892ef6f1..03872e9d24d 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs @@ -471,7 +471,7 @@ public async Task HandlePlaybackRequest(string recordingId, HttpRequest incoming var entry = (await CreateEntryAsync(incomingRequest).ConfigureAwait(false)).Item1; // Session may be removed later, but only after response has been fully written - var match = session.Session.Lookup(entry, session.CustomMatcher ?? Matcher, sanitizers, remove: false); + var match = session.Session.Lookup(entry, session.CustomMatcher ?? Matcher, sanitizers, remove: false, sessionId: recordingId); foreach (ResponseTransform transform in Transforms.Concat(session.AdditionalTransforms)) {