From 262e4e59579e887167880a5c81f39f75b7e9876a Mon Sep 17 00:00:00 2001
From: Scott Beddall <45376673+scbedd@users.noreply.github.com>
Date: Fri, 22 Oct 2021 11:21:58 -0700
Subject: [PATCH] Continuation Sanitizer Bugfix (#2136)
* Continuation sanitizer bugfix
* Adding tests for continuationSanitizer
---
.../Azure.Sdk.Tools.TestProxy.Tests.csproj | 3 +
.../SanitizerTests.cs | 57 +++++++++++--
.../requests_with_continuation.json | 79 +++++++++++++++++++
.../Sanitizers/ContinuationSanitizer.cs | 20 ++---
4 files changed, 144 insertions(+), 15 deletions(-)
create mode 100644 tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/requests_with_continuation.json
diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Azure.Sdk.Tools.TestProxy.Tests.csproj b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Azure.Sdk.Tools.TestProxy.Tests.csproj
index d80fbc7852b..69bdf8ff99d 100644
--- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Azure.Sdk.Tools.TestProxy.Tests.csproj
+++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Azure.Sdk.Tools.TestProxy.Tests.csproj
@@ -27,6 +27,9 @@
PreserveNewest
+
+ PreserveNewest
+
PreserveNewest
diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/SanitizerTests.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/SanitizerTests.cs
index 41d7e2c4335..a164c895419 100644
--- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/SanitizerTests.cs
+++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/SanitizerTests.cs
@@ -330,21 +330,66 @@ public void BodyKeySanitizerQuietlyExits()
[Fact]
- public void ContinuationSanitizerMultipleStepsNoKey()
+ public void ContinuationSanitizerSingleReplace()
{
-
+ var session = TestHelpers.LoadRecordSession("Test.RecordEntries/requests_with_continuation.json");
+ var continueSanitizer = new ContinuationSanitizer("correlationId", "guid", resetAfterFirst: "false");
+ var targetKey = "correlationId";
+ var originalRequestGuid = session.Session.Entries[0].Response.Headers[targetKey].First();
+
+ session.Session.Sanitize(continueSanitizer);
+
+ var firstRequest = session.Session.Entries[0].Response.Headers[targetKey].First();
+ var firstResponse = session.Session.Entries[1].Request.Headers[targetKey].First();
+ var secondRequest = session.Session.Entries[2].Response.Headers[targetKey].First();
+ var secondResponse = session.Session.Entries[3].Request.Headers[targetKey].First();
+
+ Assert.NotEqual(originalRequestGuid, firstRequest);
+ Assert.Equal(firstRequest, firstResponse);
+ Assert.NotEqual(firstRequest, secondRequest);
+ Assert.Equal(firstResponse, secondResponse);
}
[Fact]
- public void ContinuationSanitizerSimpleRequestResponse()
+ public void ContinuationSanitizerMultipleReplace()
{
-
+ var session = TestHelpers.LoadRecordSession("Test.RecordEntries/requests_with_continuation.json");
+ var continueSanitizer = new ContinuationSanitizer("correlationId", "guid", resetAfterFirst: "true");
+ var targetKey = "correlationId";
+ var originalSendGuid = session.Session.Entries[0].Response.Headers[targetKey].First();
+
+ session.Session.Sanitize(continueSanitizer);
+
+ var firstRequest = session.Session.Entries[0].Response.Headers[targetKey].First();
+ var firstResponse = session.Session.Entries[1].Request.Headers[targetKey].First();
+ var secondRequest = session.Session.Entries[2].Response.Headers[targetKey].First();
+ var secondResponse = session.Session.Entries[3].Request.Headers[targetKey].First();
+
+ Assert.NotEqual(originalSendGuid, firstRequest);
+ Assert.Equal(firstRequest, firstResponse);
+ Assert.NotEqual(firstResponse, secondRequest);
+ Assert.Equal(secondRequest, secondResponse);
}
[Fact]
- public void ContinuationSanitizerMultipleSteps()
+ public void ContinuationSanitizerNonExistentKey()
{
-
+ var session = TestHelpers.LoadRecordSession("Test.RecordEntries/requests_with_continuation.json");
+ var continueSanitizer = new ContinuationSanitizer("non-existent-key", "guid", resetAfterFirst: "true");
+ var targetKey = "correlationId";
+ var originalSendGuid = session.Session.Entries[0].Response.Headers[targetKey].First();
+
+ session.Session.Sanitize(continueSanitizer);
+
+ var firstRequest = session.Session.Entries[0].Response.Headers[targetKey].First();
+ var firstResponse = session.Session.Entries[1].Request.Headers[targetKey].First();
+ var secondRequest = session.Session.Entries[2].Response.Headers[targetKey].First();
+ var secondResponse = session.Session.Entries[3].Request.Headers[targetKey].First();
+
+ Assert.Equal(originalSendGuid, firstRequest);
+ Assert.Equal(firstRequest, firstResponse);
+ Assert.NotEqual(firstResponse, secondRequest);
+ Assert.Equal(secondRequest, secondResponse);
}
}
}
diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/requests_with_continuation.json b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/requests_with_continuation.json
new file mode 100644
index 00000000000..709fade458b
--- /dev/null
+++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/requests_with_continuation.json
@@ -0,0 +1,79 @@
+{
+ "Entries": [
+ {
+ "RequestUri": "http://localhost:3000/request",
+ "RequestMethod": "GET",
+ "RequestHeaders": {
+ "Accept-Encoding": "gzip,deflate",
+ "Connection": "keep-alive"
+ },
+ "RequestBody": null,
+ "StatusCode": 200,
+ "ResponseHeaders": {
+ "Connection": "keep-alive",
+ "Content-Length": "0",
+ "Date": "Wed, 20 Oct 2021 22:45:39 GMT",
+ "correlationId": "a34cafcb-ea7f-4d0f-b5ca-83c714761a50"
+ },
+ "ResponseBody": null
+ },
+ {
+ "RequestUri": "http://localhost:3000/send",
+ "RequestMethod": "POST",
+ "RequestHeaders": {
+ "Accept-Encoding": "gzip,deflate",
+ "Connection": "keep-alive",
+ "correlationId": "a34cafcb-ea7f-4d0f-b5ca-83c714761a50"
+ },
+ "RequestBody": null,
+ "StatusCode": 200,
+ "ResponseHeaders": {
+ "Connection": "keep-alive",
+ "Content-Length": "13",
+ "Content-Type": "application/json; charset=utf-8",
+ "Date": "Wed, 20 Oct 2021 22:45:39 GMT"
+ },
+ "ResponseBody": {
+ "val": "abc"
+ }
+ },
+ {
+ "RequestUri": "http://localhost:3000/request",
+ "RequestMethod": "GET",
+ "RequestHeaders": {
+ "Accept-Encoding": "gzip,deflate",
+ "Connection": "keep-alive"
+ },
+ "RequestBody": null,
+ "StatusCode": 200,
+ "ResponseHeaders": {
+ "Connection": "keep-alive",
+ "Content-Length": "0",
+ "Date": "Wed, 20 Oct 2021 22:45:39 GMT",
+ "correlationId": "b24f75a9-b830-11eb-b949-10e7c6392c5a"
+ },
+ "ResponseBody": null
+ },
+ {
+ "RequestUri": "http://localhost:3000/send",
+ "RequestMethod": "GET",
+ "RequestHeaders": {
+ "Accept-Encoding": "gzip,deflate",
+ "Connection": "keep-alive",
+ "correlationId": "b24f75a9-b830-11eb-b949-10e7c6392c5a"
+ },
+ "RequestBody": null,
+ "StatusCode": 200,
+ "ResponseHeaders": {
+ "Connection": "keep-alive",
+ "Content-Length": "13",
+ "Content-Type": "application/json; charset=utf-8",
+ "Date": "Wed, 20 Oct 2021 22:45:39 GMT"
+ },
+ "ResponseBody": {
+ "val": "abc"
+ }
+ }
+ ],
+ "Variables": {}
+}
diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/ContinuationSanitizer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/ContinuationSanitizer.cs
index adf71f44621..6a687ca8f6e 100644
--- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/ContinuationSanitizer.cs
+++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/ContinuationSanitizer.cs
@@ -10,9 +10,9 @@
namespace Azure.Sdk.Tools.TestProxy.Sanitizers
{
///
- /// Used to sanitize "session" variables. A "session" variable is one that is returned as a result of Response A, then used as INPUT of Response B.
+ /// Used to sanitize "session" variables. A "session" variable is one that is returned as a result of Response A, then used as INPUT of Response B.
///
- /// The value inserted defaults to a new guid if one is not provided. This sanitizer applies at the session level.
+ /// The value inserted defaults to a new guid if one is not provided. This sanitizer applies at the session level, and looks at the HEADERS of the request/response pairs.
///
public class ContinuationSanitizer : RecordedTestSanitizer
{
@@ -33,15 +33,17 @@ public static string GuidReplacer()
///
/// This sanitizer is applied at the session level, and is used to anonymize private keys in response/request pairs.
///
- /// For instance, a request hands back a "sessionId" that needs to be present in the next request.
+ /// For instance, request/response pair A has a RESPONSE that contains a "sessionId" header whos value must be present in the REQUEST of the next request/response pair B.
///
- /// Supports "all further requests get this key" as well as "single response/request pair". Defaults to maintaining same key
- /// for rest of recording.
+ /// This sanitizer supports "all further requests get this the value of this first key" as well as as the more standard "response value gets used in next request" that is described
+ /// in the scenario before.
+ ///
+ /// Defaults to maintaining same key for rest of recording.
///
/// The name of the header whos value will be replaced from response -> next request.
- /// The method by which the value of the targeted key will be replaced. Defaults to guid replacement.
- /// Do we need multiple pairs replaced? Or do we want to replace each value with the same value.
- public ContinuationSanitizer(string key, string method, string resetAfterFirst = "false")
+ /// The method by which the value of the targeted key will be replaced. Currently only supports generating a new guid for the value replacement..
+ /// Do we need multiple pairs replaced? Or do we want to stop after the first set.
+ public ContinuationSanitizer(string key, string method, string resetAfterFirst = "true")
{
_targetKey = key;
_method = method;
@@ -72,7 +74,7 @@ public override void Sanitize(RecordSession session)
if (currentEntry.Request.Headers.ContainsKey(_targetKey) && !String.IsNullOrWhiteSpace(newValue))
{
- currentEntry.Response.Headers[_targetKey] = new string[] { newValue };
+ currentEntry.Request.Headers[_targetKey] = new string[] { newValue };
if (_resetAfterFirst)
{