Skip to content

Commit

Permalink
KevinDockx#130 eTag injector reads streams and requires an eTag gener…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
todd committed Jan 9, 2024
1 parent 401a5d7 commit 672fb27
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Marvin.Cache.Headers/DefaultETagInjector.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Marvin.Cache.Headers.Interfaces;
Expand All @@ -14,7 +15,7 @@ public class DefaultETagInjector : IETagInjector

public DefaultETagInjector(IETagGenerator eTagGenerator)
{
_eTagGenerator = eTagGenerator;
_eTagGenerator = eTagGenerator ?? throw new ArgumentNullException(nameof(eTagGenerator));
}

public async Task<ETag> RetrieveETag(ETagContext eTagContext)
Expand Down
51 changes: 51 additions & 0 deletions test/Marvin.Cache.Headers.Test/Injectors/ETagInjectorFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Any comments, input: @KevinDockx
// Any issues, requests: https://github.com/KevinDockx/HttpCacheHeaders

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Marvin.Cache.Headers.Interfaces;
using Microsoft.AspNetCore.Http;
using Moq;
using Xunit;

namespace Marvin.Cache.Headers.Test.Stores;

public class ETagInjectorFacts
{
[Fact]
public void Ctor_ThrowsArgumentNullException_WhenETagGeneratorIsNull()
{
Assert.Throws<ArgumentNullException>(() => new DefaultETagInjector(null));
}

[Theory]
[InlineData("payload")]
[InlineData("")]
public async Task RetrieveETag_Returns_ETag_BasedOnResponseBody(string payload)
{
// arrange
var eTagGenerator = new Mock<IETagGenerator>();

eTagGenerator
.Setup(x => x.GenerateETag(It.IsAny<StoreKey>(), It.IsAny<string>()))
.ReturnsAsync(new ETag(ETagType.Strong, "B56"));

var httpContext = new Mock<IHttpContextAccessor>();
httpContext.Setup(x => x.HttpContext.Response.Body)
.Returns(new MemoryStream(Encoding.UTF8.GetBytes(payload)));

var target = new DefaultETagInjector(eTagGenerator.Object);

// act
var result = await target.RetrieveETag(new ETagContext(new StoreKey(), httpContext.Object.HttpContext));

// assert
Assert.NotNull(result);
Assert.Equal(ETagType.Strong, result.ETagType);
Assert.Equal("B56", result.Value);
eTagGenerator.Verify(x => x.GenerateETag(It.IsAny<StoreKey>(), payload), Times.Exactly(1));
httpContext.Verify(x => x.HttpContext.Response.Body, Times.AtLeastOnce());
}
}

0 comments on commit 672fb27

Please sign in to comment.