From 665f2f8773194b6d6d984de85a6196222c292cb8 Mon Sep 17 00:00:00 2001 From: Chris R Date: Mon, 26 Sep 2016 12:11:45 -0700 Subject: [PATCH] ResponseCompression style cleanup --- .../ResponseCompressionSample/project.json | 4 +-- .../BodyWrapperStream.cs | 28 ++++++------------- .../ResponseCompressionMiddleware.cs | 1 + .../ResponseCompressionMiddlewareTest.cs | 26 ++++++++--------- .../ResponseCompressionUtilsTest.cs | 8 +++--- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/samples/ResponseCompressionSample/project.json b/samples/ResponseCompressionSample/project.json index 7ad12a138a..0ad34fd2b2 100644 --- a/samples/ResponseCompressionSample/project.json +++ b/samples/ResponseCompressionSample/project.json @@ -1,12 +1,10 @@ { - "version": "1.1.0-*", "dependencies": { "Microsoft.AspNetCore.ResponseCompression": "0.1.0-*", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*" }, "buildOptions": { - "emitEntryPoint": true, - "preserveCompilationContext": true + "emitEntryPoint": true }, "frameworks": { "net451": {}, diff --git a/src/Microsoft.AspNetCore.ResponseCompression/BodyWrapperStream.cs b/src/Microsoft.AspNetCore.ResponseCompression/BodyWrapperStream.cs index 8862e92db5..50e8ede353 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/BodyWrapperStream.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/BodyWrapperStream.cs @@ -46,31 +46,21 @@ namespace Microsoft.AspNetCore.ResponseCompression } } - public override bool CanRead => _bodyOriginalStream.CanRead; + public override bool CanRead => false; - public override bool CanSeek => _bodyOriginalStream.CanSeek; + public override bool CanSeek => false; public override bool CanWrite => _bodyOriginalStream.CanWrite; public override long Length { - get - { - throw new NotSupportedException(); - } + get { throw new NotSupportedException(); } } public override long Position { - get - { - throw new NotSupportedException(); - } - - set - { - throw new NotSupportedException(); - } + get { throw new NotSupportedException(); } + set { throw new NotSupportedException(); } } public override void Flush() @@ -169,16 +159,16 @@ namespace Microsoft.AspNetCore.ResponseCompression { if (!_compressionChecked) { + _compressionChecked = true; + if (IsCompressable()) { - _response.Headers[HeaderNames.ContentEncoding] = _compressionProvider.EncodingName; - _response.Headers.Remove(HeaderNames.ContentMD5); // Reset the MD5 because the content changed. + _response.Headers.Append(HeaderNames.ContentEncoding, _compressionProvider.EncodingName); + _response.Headers.Remove(HeaderNames.ContentMD5); // Reset the MD5 because the content changed. _response.Headers.Remove(HeaderNames.ContentLength); _compressionStream = _compressionProvider.CreateStream(_bodyOriginalStream); } - - _compressionChecked = true; } } diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs index 4d9a0e611c..aa0620b3fe 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs @@ -37,6 +37,7 @@ namespace Microsoft.AspNetCore.ResponseCompression { throw new ArgumentException($"{nameof(options.Value.ShouldCompressResponse)} is not provided in argument {nameof(options)}"); } + _shouldCompressResponse = options.Value.ShouldCompressResponse; _next = next; diff --git a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs index 76cdcc9f14..b526582b93 100644 --- a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs +++ b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests private const string TextPlain = "text/plain"; [Fact] - public void Options_NullShouldCompressResponse() + public void Options_NullShouldCompressResponse_Throws() { Assert.Throws(() => { @@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public void Options_EmptyProviderList() + public void Options_EmptyProviderList_Throws() { Assert.Throws(() => { @@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_Uncompressed() + public async Task Request_NoAcceptEncoding_Uncompressed() { var response = await InvokeMiddleware(100, requestAcceptEncodings: null, responseType: TextPlain); @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_CompressGzip() + public async Task Request_AcceptGzipDeflate_ComrpessedGzip() { var response = await InvokeMiddleware(100, requestAcceptEncodings: new string[] { "gzip", "deflate" }, responseType: TextPlain); @@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_CompressUnknown() + public async Task Request_AcceptUnknown_NotCompressed() { var response = await InvokeMiddleware(100, requestAcceptEncodings: new string[] { "unknown" }, responseType: TextPlain); @@ -79,7 +79,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_CompressStar() + public async Task Request_AcceptStar_Compressed() { var response = await InvokeMiddleware(100, requestAcceptEncodings: new string[] { "*" }, responseType: TextPlain); @@ -87,7 +87,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_CompressIdentity() + public async Task Request_AcceptIdentity_NotCompressed() { var response = await InvokeMiddleware(100, requestAcceptEncodings: new string[] { "identity" }, responseType: TextPlain); @@ -98,7 +98,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests [InlineData(new string[] { "identity;q=0.5", "gzip;q=1" }, 24)] [InlineData(new string[] { "identity;q=0", "gzip;q=0.8" }, 24)] [InlineData(new string[] { "identity;q=0.5", "gzip" }, 24)] - public async Task Request_CompressQuality_Compressed(string[] acceptEncodings, int expectedBodyLength) + public async Task Request_AcceptWithHigherCompressionQuality_Compressed(string[] acceptEncodings, int expectedBodyLength) { var response = await InvokeMiddleware(100, requestAcceptEncodings: acceptEncodings, responseType: TextPlain); @@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests [Theory] [InlineData(new string[] { "gzip;q=0.5", "identity;q=0.8" }, 100)] - public async Task Request_CompressQuality_NotCompressed(string[] acceptEncodings, int expectedBodyLength) + public async Task Request_AcceptWithhigherIdentityQuality_NotCompressed(string[] acceptEncodings, int expectedBodyLength) { var response = await InvokeMiddleware(100, requestAcceptEncodings: acceptEncodings, responseType: TextPlain); @@ -115,7 +115,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_UnauthorizedMimeType() + public async Task Response_UnauthorizedMimeType_NotCompressed() { var response = await InvokeMiddleware(100, requestAcceptEncodings: new string[] { "gzip" }, responseType: "text/html"); @@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_ResponseWithContentRange() + public async Task Response_WithContentRange_NotCompressed() { var response = await InvokeMiddleware(50, requestAcceptEncodings: new string[] { "gzip" }, responseType: TextPlain, addResponseAction: (r) => { @@ -134,7 +134,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public async Task Request_ResponseWithContentEncodingAlreadySet() + public async Task Response_WithContentEncodingAlreadySet_NotCompressed() { var otherContentEncoding = "something"; @@ -151,7 +151,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests [Theory] [InlineData(false, 100)] [InlineData(true, 24)] - public async Task Request_Https(bool enableHttps, int expectedLength) + public async Task Request_Https_CompressedIfEnabled(bool enableHttps, int expectedLength) { var options = new ResponseCompressionOptions() { diff --git a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionUtilsTest.cs b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionUtilsTest.cs index 9efcc79cb3..aae7752479 100644 --- a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionUtilsTest.cs +++ b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionUtilsTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests private const string TextPlain = "text/plain"; [Fact] - public void CreateShouldCompressResponseDelegate_NullMimeTypes() + public void CreateShouldCompressResponseDelegate_NullMimeTypes_Throws() { Assert.Throws(() => { @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests } [Fact] - public void CreateShouldCompressResponseDelegate_Empty() + public void CreateShouldCompressResponseDelegate_Empty_DontCompress() { var httpContext = new DefaultHttpContext(); httpContext.Response.ContentType = TextPlain; @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests [InlineData("text/plain")] [InlineData("text/plain; charset=ISO-8859-4")] [InlineData("text/plain ; charset=ISO-8859-4")] - public void CreateShouldCompressResponseDelegate_True(string contentType) + public void CreateShouldCompressResponseDelegate_WithCharset_Compress(string contentType) { var httpContext = new DefaultHttpContext(); httpContext.Response.ContentType = contentType; @@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests [InlineData("")] [InlineData("text/plain2")] [InlineData("text/PLAIN")] - public void CreateShouldCompressResponseDelegate_False(string contentType) + public void CreateShouldCompressResponseDelegate_OtherContentTypes_NoMatch(string contentType) { var httpContext = new DefaultHttpContext(); httpContext.Response.ContentType = contentType;