forked from KevinDockx/HttpCacheHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KevinDockx#130 eTag injector reads streams and requires an eTag gener…
…ator
- Loading branch information
todd
committed
Jan 9, 2024
1 parent
401a5d7
commit 672fb27
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
test/Marvin.Cache.Headers.Test/Injectors/ETagInjectorFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |