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
10 changes: 1 addition & 9 deletions sdk/core/Azure.Core/tests/TestFramework/RecordSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ public void Sanitize(RecordedTestSanitizer sanitizer)
{
lock (Entries)
Comment thread
heaths marked this conversation as resolved.
{
foreach (RecordEntry entry in Entries)
{
sanitizer.Sanitize(entry);
}

foreach (var variable in Variables.ToArray())
{
Variables[variable.Key] = sanitizer.SanitizeVariable(variable.Key, variable.Value);
}
sanitizer.Sanitize(this);
}
}

Expand Down
33 changes: 20 additions & 13 deletions sdk/core/Azure.Core/tests/TestFramework/RecordedTestSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Azure.Core.Testing
Expand Down Expand Up @@ -40,12 +42,11 @@ public virtual byte[] SanitizeBody(string contentType, byte[] body)
}

public virtual string SanitizeVariable(string variableName, string environmentVariableValue) => environmentVariableValue;

public virtual void SanitizeBody(RecordEntryMessage message)
{
if (message.Body != null)
{
int contentLength = message.Body.Length;

message.TryGetContentType(out string contentType);

if (message.TryGetBodyAsText(out string text))
Expand All @@ -57,9 +58,8 @@ public virtual void SanitizeBody(RecordEntryMessage message)
message.Body = SanitizeBody(contentType, message.Body);
}

UpdateSanitizedContentLength(message.Headers, contentLength, message.Body?.Length ?? 0);
UpdateSanitizedContentLength(message.Headers, message.Body?.Length ?? 0);
}

}

public virtual void Sanitize(RecordEntry entry)
Expand All @@ -75,6 +75,18 @@ public virtual void Sanitize(RecordEntry entry)
SanitizeBody(entry.Response);
}

public virtual void Sanitize(RecordSession session)
{
foreach (RecordEntry entry in session.Entries)
{
Sanitize(entry);
}

foreach (KeyValuePair<string, string> variable in session.Variables.ToArray())
{
session.Variables[variable.Key] = SanitizeVariable(variable.Key, variable.Value);
}
}

/// <summary>
/// Optionally update the Content-Length header if we've sanitized it
Expand All @@ -83,18 +95,13 @@ public virtual void Sanitize(RecordEntry entry)
/// wasn't already present.
/// </summary>
/// <param name="headers">The Request or Response headers</param>
/// <param name="originalLength">THe original Content-Length</param>
/// <param name="sanitizedLength">The sanitized Content-Length</param>
protected static void UpdateSanitizedContentLength(IDictionary<string, string[]> headers, int originalLength, int sanitizedLength)
protected static void UpdateSanitizedContentLength(IDictionary<string, string[]> headers, int sanitizedLength)
{
// Note: If the RequestBody/ResponseBody was set to null by our
// sanitizer, we'll pass 0 as the sanitizedLength and use that as
// our new Content-Length. That's fine for all current scenarios
// (i.e., we never do that), but it's possible we may want to
// remove the Content-Length header in the future.
if (originalLength != sanitizedLength && headers.ContainsKey("Content-Length"))
// Only update Content-Length if already present.
if (headers.ContainsKey("Content-Length"))
{
headers["Content-Length"] = new string[] { sanitizedLength.ToString() };
headers["Content-Length"] = new string[] { sanitizedLength.ToString(CultureInfo.InvariantCulture) };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void SanitizeTokenRequest(RecordEntry entry)
{
entry.Request.Body = Encoding.UTF8.GetBytes("Sanitized");

UpdateSanitizedContentLength(entry.Request.Headers, 0, entry.Request.Body.Length);
UpdateSanitizedContentLength(entry.Request.Headers, entry.Request.Body.Length);

}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading