diff --git a/sdk/core/Azure.Core.TestFramework/src/RecordedTestSanitizer.cs b/sdk/core/Azure.Core.TestFramework/src/RecordedTestSanitizer.cs index 10aadea39223..613b0110137a 100644 --- a/sdk/core/Azure.Core.TestFramework/src/RecordedTestSanitizer.cs +++ b/sdk/core/Azure.Core.TestFramework/src/RecordedTestSanitizer.cs @@ -93,7 +93,10 @@ public virtual void Sanitize(RecordEntry entry) SanitizeHeaders(entry.Response.Headers); - SanitizeBody(entry.Response); + if (entry.RequestMethod != RequestMethod.Head) + { + SanitizeBody(entry.Response); + } } public virtual void Sanitize(RecordSession session) diff --git a/sdk/core/Azure.Core/tests/RecordSessionTests.cs b/sdk/core/Azure.Core/tests/RecordSessionTests.cs index 621019896ab4..aba3efc5a458 100644 --- a/sdk/core/Azure.Core/tests/RecordSessionTests.cs +++ b/sdk/core/Azure.Core/tests/RecordSessionTests.cs @@ -409,6 +409,48 @@ public void RecordSessionLookupSkipsRequestBodyWhenFilterIsOn() Assert.Throws(() => playbackTransport.Process(message)); } + [Test] + public void ContentLengthNotChangedOnHeadRequestWithEmptyBody() + { + ContentLengthUpdatedCorrectlyOnEmptyBody(isHeadRequest: true); + } + + [Test] + public void ContentLengthResetToZeroOnGetRequestWithEmptyBody() + { + ContentLengthUpdatedCorrectlyOnEmptyBody(isHeadRequest: false); + } + + private void ContentLengthUpdatedCorrectlyOnEmptyBody(bool isHeadRequest) + { + var sanitizer = new RecordedTestSanitizer(); + var entry = new RecordEntry() + { + RequestUri = "http://localhost/", + RequestMethod = isHeadRequest ? RequestMethod.Head : RequestMethod.Get, + Response = + { + Headers = + { + {"Content-Length", new[] {"41"}}, + {"Some-Header", new[] {"Random value"}}, + {"Some-Other-Header", new[] {"V"}} + }, + Body = new byte[0] + } + }; + sanitizer.Sanitize(entry); + + if (isHeadRequest) + { + Assert.AreEqual(new[] { "41" }, entry.Response.Headers["Content-Length"]); + } + else + { + Assert.AreEqual(new[] { "0" }, entry.Response.Headers["Content-Length"]); + } + } + private class TestSanitizer : RecordedTestSanitizer { public override string SanitizeVariable(string variableName, string environmentVariableValue)