Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/MatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void MatchesBadlyNormalizedHeader(string file, string targetHeader, strin
public void BodilessMatcherMatchesIdenticalRequest()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/oauth_request.json");

var identicalRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/oauth_request.json").Session.Entries[0];

var expectedIdenticalMatch = sessionForRetrieval.Session.Lookup(identicalRequest, BodilessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
Expand All @@ -51,7 +51,7 @@ public void BodilessMatcherMatchesBodilessRequest()
var bodilessRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/oauth_request.json").Session.Entries[0];
bodilessRequest.Request.Body = new byte[] { };
bodilessRequest.Request.Headers["Content-Length"] = new string[] { "0" };

var expectedBodilessMatch = sessionForRetrieval.Session.Lookup(bodilessRequest, BodilessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
}

Expand All @@ -75,7 +75,8 @@ public void BodilessMatcherThrowsOnDiffUri()
var identicalRequestDiffURI = TestHelpers.LoadRecordSession("Test.RecordEntries/oauth_request.json").Session.Entries[0];
identicalRequestDiffURI.RequestUri = identicalRequestDiffURI.RequestUri + "2";

Assert.Throws<TestRecordingMismatchException>(() => {
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(identicalRequestDiffURI, BodilessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}
Expand All @@ -88,7 +89,8 @@ public void BodilessMatcherThrowsOnDiffHeaders()
var identicalBodyDiffHeaders = TestHelpers.LoadRecordSession("Test.RecordEntries/oauth_request.json").Session.Entries[0];
identicalBodyDiffHeaders.Request.Headers.Remove(identicalBodyDiffHeaders.Request.Headers.Keys.First());

Assert.Throws<TestRecordingMismatchException>(() => {
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(identicalBodyDiffHeaders, BodilessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}
Expand Down Expand Up @@ -119,7 +121,7 @@ public void HeaderlessMatcherMatchesIdenticalHeadersRequest()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/if_none_match_present.json");
var identicalHeaders = TestHelpers.LoadRecordSession("Test.RecordEntries/if_none_match_present.json").Session.Entries[0];

var expectedDiffBodyMatch = sessionForRetrieval.Session.Lookup(identicalHeaders, HeaderlessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
}

Expand All @@ -130,7 +132,8 @@ public void HeaderlessMatcherThrowsOnDiffBody()
var diffBodyRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/if_none_match_present.json").Session.Entries[0];
diffBodyRequest.Request.Body = Encoding.UTF8.GetBytes("A Different Request Body");

Assert.Throws<TestRecordingMismatchException>(() => {
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(diffBodyRequest, HeaderlessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}
Expand All @@ -142,7 +145,8 @@ public void HeaderlessMatcherThrowsOnDiffUri()
var differenUriRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/if_none_match_present.json").Session.Entries[0];
differenUriRequest.RequestUri = "https://shouldntmatch.com";

Assert.Throws<TestRecordingMismatchException>(() => {
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(differenUriRequest, HeaderlessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}
Expand Down Expand Up @@ -202,7 +206,8 @@ public void CustomMatcherSpecifyIgnoredThrowsOnRequestNonPresence()

var matcher = new CustomDefaultMatcher(ignoredHeaders: "Accept-Encoding");

var assertion = Assert.Throws<TestRecordingMismatchException>(() => {
var assertion = Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(differentHeadersRequest, matcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});

Expand All @@ -218,7 +223,8 @@ public void CustomMatcherSpecifyIgnoredThrowsOnRecordNonPresence()

var matcher = new CustomDefaultMatcher(ignoredHeaders: "Accept-Encoding");

var assertion = Assert.Throws<TestRecordingMismatchException>(() => {
var assertion = Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(sameOriginalHeadersRequest, matcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});

Expand All @@ -245,7 +251,8 @@ public void CustomMatcherThrowsOnUnmatched()
differentRequest.Request.Headers["Accept-Encoding"] = new string[] { "a-test-header-that-shouldn't-match" };
differentRequest.RequestUri = "https://shouldntmatch.com";

Assert.Throws<TestRecordingMismatchException>(() => {
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(differentRequest, HeaderlessMatcher, sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}
Expand All @@ -260,7 +267,7 @@ public async Task CustomMatcherMatchesDifferentUriOrder()
var body = "{\"x-recording-file\":\"" + targetFile + "\"}";
playbackContext.Request.Body = TestHelpers.GenerateStreamRequestBody(body);
playbackContext.Request.ContentLength = body.Length;

var controller = new Playback(testRecordingHandler, new NullLoggerFactory())
{
ControllerContext = new ControllerContext()
Expand Down Expand Up @@ -343,6 +350,42 @@ public async Task EncodedUriAmpersandWorksCrossplat()
Assert.Equal("Ref A: 980665086A12483993E2782EDFC9F29A Ref B: STBEDGE0106 Ref C: 2023-07-19T22:52:17Z", playbackContext.Response.Headers["X-MSEdge-Ref"].ToString());
Assert.Equal(200, playbackContext.Response.StatusCode);
}

[Fact]
public void MultiPartMatcherMatchesDifferentBoundary()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request.json");
var differentBoundaryRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request_diff_boundary.json").Session.Entries[0];
var expectedDiffBodyMatch = sessionForRetrieval.Session.Lookup(differentBoundaryRequest, new RecordMatcher(), sanitizers: new List<RecordedTestSanitizer>(), remove: false);
}

[Fact]
public void MultiPartMultiLayerMatcherMatchesDifferentBoundary()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request_two_layers.json");
var differentBoundaryRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request_two_layers_diff_boundary.json").Session.Entries[0];
var expectedDiffBodyMatch = sessionForRetrieval.Session.Lookup(differentBoundaryRequest, new RecordMatcher(), sanitizers: new List<RecordedTestSanitizer>(), remove: false);
}

[Fact]
public void MultiPartMatcherThrowsOnDiffBody()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request.json");
var diffBodyRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request_diff_body.json").Session.Entries[0];
diffBodyRequest.Request.Body = Encoding.UTF8.GetBytes("A Different Request Body");
Assert.Throws<TestRecordingMismatchException>(() =>
{
sessionForRetrieval.Session.Lookup(diffBodyRequest, new RecordMatcher(), sanitizers: new List<RecordedTestSanitizer>(), remove: false);
});
}

[Fact]
public void MultiPartMatcherMatchesIdenticalBoundary()
{
var sessionForRetrieval = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request.json");
var identicalRequest = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_request.json").Session.Entries[0];
var expectedIdenticalMatch = sessionForRetrieval.Session.Lookup(identicalRequest, new RecordMatcher(), sanitizers: new List<RecordedTestSanitizer>(), remove: false);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,11 @@ public async void GenStringSanitizerQuietExitForAllHttpComponents()
Assert.Equal(0, matcher.CompareHeaderDictionaries(targetUntouchedEntry.Request.Headers, targetEntry.Request.Headers, new HashSet<string>(), new HashSet<string>()));
Assert.Equal(0, matcher.CompareHeaderDictionaries(targetUntouchedEntry.Response.Headers, targetEntry.Response.Headers, new HashSet<string>(), new HashSet<string>()));

targetUntouchedEntry.Request.TryGetContentType(out var contentType);
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Request.Body, targetEntry.Request.Body, contentType));
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Response.Body, targetEntry.Response.Body, contentType));
targetUntouchedEntry.Request.TryGetContentType(out var requestContentType);
targetEntry.Request.TryGetContentType(out var recordContentType);
ContentTypeUtilities.TryGetTextEncoding(requestContentType, out var encoding);
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Request.Body, targetEntry.Request.Body, requestContentType, recordContentType, encoding));
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Response.Body, targetEntry.Response.Body, requestContentType, recordContentType, encoding));
Assert.Equal(targetUntouchedEntry.RequestUri, targetEntry.RequestUri);
}

Expand Down Expand Up @@ -771,9 +773,11 @@ public async void BodyStringSanitizerQuietlyExits(string targetValue, string rep
await session.Session.Sanitize(sanitizer);

var resultBodyValue = Encoding.UTF8.GetString(targetEntry.Request.Body);
targetUntouchedEntry.Request.TryGetContentType(out var contentType);
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Request.Body, targetEntry.Request.Body, contentType));
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Response.Body, targetEntry.Response.Body, contentType));
targetUntouchedEntry.Request.TryGetContentType(out var requestContentType);
targetEntry.Request.TryGetContentType(out var recordContentType);
ContentTypeUtilities.TryGetTextEncoding(requestContentType, out var encoding);
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Request.Body, targetEntry.Request.Body, requestContentType, recordContentType, encoding));
Assert.Equal(0, matcher.CompareBodies(targetUntouchedEntry.Response.Body, targetEntry.Response.Body, requestContentType, recordContentType, encoding));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Accept": "*/*",
"Authorization": "Sanitized",
"Content-Length": "3505",
"Content-Type": "multipart/mixed; boundary=REDACTED",
"Content-Type": "multipart/mixed; boundary=batch_41ee0728-dac8-4887-a5f6-fcf5a69252c9",
"DataServiceVersion": "3.0",
"Date": "Sat, 26 Apr 2025 01:09:57 GMT",
"User-Agent": "azsdk-java-azure-data-tables/12.6.0-beta.1 (17.0.15; Windows 11; 10.0)",
Expand Down Expand Up @@ -66,7 +66,7 @@
],
"StatusCode": 202,
"ResponseHeaders": {
"Content-Type": "multipart/mixed; boundary=REDACTED",
"Content-Type": "multipart/mixed; boundary=batchresponse_32c36dfe-6fad-46c7-9a9a-5472c406b4a8",
"Date": "Sat, 26 Apr 2025 01:09:58 GMT",
"Transfer-Encoding": "chunked"
},
Expand Down
Loading