Skip to content

Commit

Permalink
change the ordering so we aren't ALWAYS expanding the full json
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Aug 21, 2024
1 parent 5ab2b1c commit 719b270
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/RecordMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,46 +157,50 @@ public virtual int CompareBodies(byte[] requestBody, byte[] recordBody, string c
return 1;
}

if (!string.IsNullOrWhiteSpace(contentType) && contentType.Contains("json"))
{
var jsonDifferences = JsonComparer.CompareJson(requestBody, recordBody);

if (jsonDifferences.Count > 0)
if (!requestBody.SequenceEqual(recordBody))
{
// we just failed sequence equality, before erroring, lets check if we're a json body and check for property equality
if (!string.IsNullOrWhiteSpace(contentType) && contentType.Contains("json"))
{
var jsonDifferences = JsonComparer.CompareJson(requestBody, recordBody);

if (descriptionBuilder != null)
if (jsonDifferences.Count > 0)
{
descriptionBuilder.AppendLine($"There are differences between request and recordentry bodies:");
foreach (var jsonDifference in jsonDifferences)

if (descriptionBuilder != null)
{
descriptionBuilder.AppendLine(jsonDifference);
descriptionBuilder.AppendLine($"There are differences between request and recordentry bodies:");
foreach (var jsonDifference in jsonDifferences)
{
descriptionBuilder.AppendLine(jsonDifference);
}
}
}

return 1;
}
}
else if (!requestBody.SequenceEqual(recordBody))
{
if (descriptionBuilder != null)
{
var minLength = Math.Min(requestBody.Length, recordBody.Length);
int i;
for (i = 0; i < minLength - 1; i++)
return 1;
}
}
else {
if (descriptionBuilder != null)
{
if (requestBody[i] != recordBody[i])
var minLength = Math.Min(requestBody.Length, recordBody.Length);
int i;
for (i = 0; i < minLength - 1; i++)
{
break;
if (requestBody[i] != recordBody[i])
{
break;
}
}
descriptionBuilder.AppendLine($"Request and record bodies do not match at index {i}:");
var before = Math.Max(0, i - 10);
var afterRequest = Math.Min(i + 20, requestBody.Length);
var afterResponse = Math.Min(i + 20, recordBody.Length);
descriptionBuilder.AppendLine($" request: \"{Encoding.UTF8.GetString(requestBody, before, afterRequest - before)}\"");
descriptionBuilder.AppendLine($" record: \"{Encoding.UTF8.GetString(recordBody, before, afterResponse - before)}\"");
}
descriptionBuilder.AppendLine($"Request and record bodies do not match at index {i}:");
var before = Math.Max(0, i - 10);
var afterRequest = Math.Min(i + 20, requestBody.Length);
var afterResponse = Math.Min(i + 20, recordBody.Length);
descriptionBuilder.AppendLine($" request: \"{Encoding.UTF8.GetString(requestBody, before, afterRequest - before)}\"");
descriptionBuilder.AppendLine($" record: \"{Encoding.UTF8.GetString(recordBody, before, afterResponse - before)}\"");
return 1;
}
return 1;
}

return 0;
Expand Down

0 comments on commit 719b270

Please sign in to comment.