Do not compress response if it is already compressed (#305) (#365)

This commit is contained in:
melnikov77 2018-09-20 12:01:06 -07:00 committed by Chris Ross
parent 4a4dde182f
commit 6699804fe9
2 changed files with 9 additions and 3 deletions

View File

@ -155,6 +155,11 @@ namespace Microsoft.AspNetCore.ResponseCompression
return false; return false;
} }
if (context.Response.Headers.ContainsKey(HeaderNames.ContentEncoding))
{
return false;
}
var mimeType = context.Response.ContentType; var mimeType = context.Response.ContentType;
if (string.IsNullOrEmpty(mimeType)) if (string.IsNullOrEmpty(mimeType))

View File

@ -321,8 +321,9 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests
CheckResponseNotCompressed(response, expectedBodyLength: 50, sendVaryHeader: false); CheckResponseNotCompressed(response, expectedBodyLength: 50, sendVaryHeader: false);
} }
[Fact] [Fact]
public async Task Response_WithContentEncodingAlreadySet_Stacked() public async Task Response_WithContentEncodingAlreadySet_NotReCompressed()
{ {
var otherContentEncoding = "something"; var otherContentEncoding = "something";
@ -332,8 +333,8 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests
}); });
Assert.True(response.Content.Headers.ContentEncoding.Contains(otherContentEncoding)); Assert.True(response.Content.Headers.ContentEncoding.Contains(otherContentEncoding));
Assert.True(response.Content.Headers.ContentEncoding.Contains("gzip")); Assert.False(response.Content.Headers.ContentEncoding.Contains("gzip"));
Assert.Equal(24, response.Content.Headers.ContentLength); Assert.Equal(50, response.Content.Headers.ContentLength);
} }
[Theory] [Theory]