From 03f2293800fc3f40c6db902b492278c155c0d813 Mon Sep 17 00:00:00 2001 From: Jaxel Rojas Date: Wed, 25 Jan 2023 08:06:37 -0400 Subject: [PATCH] refactor: moved checksum calculation to middleware --- src/Carter.Cache/CarterCachingMiddleware.cs | 19 +++++++++++++++++-- .../Extensions/HttpContextExtensions.cs | 13 ------------- .../Unit/CachingServiceFxtures.cs | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Carter.Cache/CarterCachingMiddleware.cs b/src/Carter.Cache/CarterCachingMiddleware.cs index 5e560aa..1c380fd 100644 --- a/src/Carter.Cache/CarterCachingMiddleware.cs +++ b/src/Carter.Cache/CarterCachingMiddleware.cs @@ -1,4 +1,7 @@ -using System.IO; +using System; +using System.IO; +using System.Linq; +using System.Security.Cryptography; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -48,7 +51,7 @@ private async Task SetCache(HttpContext ctx, RequestDelegate next, CachingOption if (options.ValidResponse(ctx)) { - string checksum = ctx.CalculateChecksum(bytes); + string checksum = CalculateChecksum(bytes); ctx.AddEtagToContext(checksum); if (memoryStream.Length > 0) @@ -68,4 +71,16 @@ private async Task SetCache(HttpContext ctx, RequestDelegate next, CachingOption } private async Task CheckCache(HttpContext ctx, CachingOption options) => await service.CheckCache(ctx, options); + + internal static string CalculateChecksum(byte[] content) + { + if (content.Length == 0) //Dont process an empty byte array + { + return string.Empty; + } + + using var sha1 = SHA1.Create(); + + return Convert.ToBase64String(sha1.ComputeHash(content.ToArray())); + } } diff --git a/src/Carter.Cache/Extensions/HttpContextExtensions.cs b/src/Carter.Cache/Extensions/HttpContextExtensions.cs index 97bec24..ebf0b2b 100644 --- a/src/Carter.Cache/Extensions/HttpContextExtensions.cs +++ b/src/Carter.Cache/Extensions/HttpContextExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using System.Security.Cryptography; using Microsoft.AspNetCore.Http; using Microsoft.Net.Http.Headers; @@ -44,15 +42,4 @@ internal static void AddEtagToContext(this HttpContext ctx, string checksum) ctx.Response.Headers[HeaderNames.ETag] = $"\"{checksum}\""; } - - internal static string CalculateChecksum(this HttpContext ctx, byte[] content) - { - if (content.Length == 0) //Dont process an empty byte array - { - return string.Empty; - } - - using var sha1 = SHA1.Create(); - return Convert.ToBase64String(sha1.ComputeHash(content.ToArray())); - } } diff --git a/tests/Carter.Cache.Tests/Unit/CachingServiceFxtures.cs b/tests/Carter.Cache.Tests/Unit/CachingServiceFxtures.cs index ae4c233..5ae4a74 100644 --- a/tests/Carter.Cache.Tests/Unit/CachingServiceFxtures.cs +++ b/tests/Carter.Cache.Tests/Unit/CachingServiceFxtures.cs @@ -158,7 +158,7 @@ public async Task Service_set_cache_default_with_request_body() A.CallTo(() => response.StatusCode).Returns(StatusCodes.Status200OK); A.CallTo(() => response.Headers.ContainsKey(HeaderNames.ETag)).Returns(true); - A.CallTo(() => response.Headers[HeaderNames.ETag]).Returns(ctx.CalculateChecksum(body)); + A.CallTo(() => response.Headers[HeaderNames.ETag]).Returns(CarterCachingMiddleware.CalculateChecksum(body)); var cachedResponse = new CachedResponse(ctx, body);