Skip to content

Commit

Permalink
when failing to match, express the reasoning
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Jul 30, 2024
1 parent 2ea067f commit 7b8a6a1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ public void RecordMatcherThrowsExceptionsWithDetails()
TestRecordingMismatchException exception = Assert.Throws<TestRecordingMismatchException>(() => 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 <HEAD> record <PUT>" + Environment.NewLine +
"Uri doesn't match:" + Environment.NewLine +
" request <http://localhost/>" + Environment.NewLine +
Expand Down
14 changes: 11 additions & 3 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -127,7 +127,7 @@ public virtual RecordEntry FindMatch(RecordEntry request, IList<RecordEntry> 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)
Expand Down Expand Up @@ -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<RecordEntry> 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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void Record(RecordEntry entry)
}
}

public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnumerable<RecordedTestSanitizer> sanitizers, bool remove = true)
public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, IEnumerable<RecordedTestSanitizer> sanitizers, bool remove = true, string sessionId = null)
{
foreach (RecordedTestSanitizer sanitizer in sanitizers)
{
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down

0 comments on commit 7b8a6a1

Please sign in to comment.