Skip to content

Commit 034d14a

Browse files
authored
Ensure that TestRecordingMismatchException doesn't exhaust default header size limits (#9662)
* limit the size of the Remaining Entries output in the actual header of a TestRecordingMismatch exception. The message content's potential size could break the default limits on header size * update tests to reflect new exception output
1 parent 10801cb commit 034d14a

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/RecordSessionTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ public void RecordMatcherThrowsExceptionsWithDetails()
408408
TestRecordingMismatchException exception = Assert.Throws<TestRecordingMismatchException>(() => matcher.FindMatch(requestEntry, entries));
409409
Assert.Equal(
410410
"Unable to find a record for the request HEAD http://localhost/" + Environment.NewLine +
411-
"Remaining entry: http://remote-host" + Environment.NewLine +
412411
"Method doesn't match, request <HEAD> record <PUT>" + Environment.NewLine +
413412
"Uri doesn't match:" + Environment.NewLine +
414413
" request <http://localhost/>" + Environment.NewLine +
@@ -420,7 +419,9 @@ public void RecordMatcherThrowsExceptionsWithDetails()
420419
"Body differences:" + Environment.NewLine +
421420
"Request and record bodies do not match at index 40:" + Environment.NewLine +
422421
" request: \"e and long.\"" + Environment.NewLine +
423-
" record: \"e and long but it also doesn't\"" + Environment.NewLine,
422+
" record: \"e and long but it also doesn't\"" + Environment.NewLine +
423+
"Remaining Entries:" + Environment.NewLine +
424+
"0: http://remote-host" + Environment.NewLine,
424425
exception.Message);
425426
}
426427

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ public async Task Invoke(HttpContext context)
4444
response.StatusCode = statusCode;
4545
response.ContentType = "application/json;";
4646

47-
var encodedException = Convert.ToBase64String(Encoding.UTF8.GetBytes(e.Message));
48-
4947
if (e is TestRecordingMismatchException)
5048
{
5149
response.Headers.Append("x-request-mismatch", "true");
52-
response.Headers.Append("x-request-mismatch-error", encodedException);
50+
51+
// grab the exception up till Remaining Entries: which can potentially push the header size past the default limit of ~8k
52+
var onlyMisMatchMessage = e.Message.Split("Remaining Entries:")[0];
53+
response.Headers.Append("x-request-mismatch-error", Convert.ToBase64String(Encoding.UTF8.GetBytes(onlyMisMatchMessage)));
5354
}
5455
else
5556
{
5657
response.Headers.Append("x-request-known-exception", "true");
57-
response.Headers.Append("x-request-known-exception-error", encodedException);
58+
response.Headers.Append("x-request-known-exception-error", Convert.ToBase64String(Encoding.UTF8.GetBytes(e.Message)));
5859
}
5960

6061
var bodyObj = new

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry
244244
StringBuilder builder = new StringBuilder();
245245
builder.AppendLine($"Unable to find a record for the request {request.RequestMethod} {request.RequestUri}");
246246

247-
if (entries != null)
248-
{
249-
foreach (var entry in entries)
250-
{
251-
builder.AppendLine($"Remaining entry: {entry.RequestUri}");
252-
}
253-
}
254-
255247
if (bestScoreEntry == null)
256248
{
257249
builder.AppendLine("No records to match.");
@@ -279,6 +271,15 @@ private string GenerateException(RecordEntry request, RecordEntry bestScoreEntry
279271
request.Request.TryGetContentType(out var contentType);
280272
CompareBodies(request.Request.Body, bestScoreEntry.Request.Body, contentType, descriptionBuilder: builder);
281273

274+
if (entries != null && entries.Count >= 1)
275+
{
276+
builder.AppendLine("Remaining Entries:");
277+
for (int i = 0; i < entries.Count; i++)
278+
{
279+
var entry = entries[i];
280+
builder.AppendLine($"{i}: {entry.RequestUri}");
281+
}
282+
}
282283
return builder.ToString();
283284
}
284285

0 commit comments

Comments
 (0)