Skip to content

Commit

Permalink
Fix bug when sanitizing empty body (#14885)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored Sep 4, 2020
1 parent 10a513e commit bd63163
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions sdk/core/Azure.Core/tests/RecordSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,48 @@ public void RecordSessionLookupSkipsRequestBodyWhenFilterIsOn()
Assert.Throws<TestRecordingMismatchException>(() => 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)
Expand Down

0 comments on commit bd63163

Please sign in to comment.