Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Overwrite headers when serving response from cache (backport to 1.0.1) #107

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal async Task<bool> TryServeCachedResponseAsync(ResponseCachingContext con
response.StatusCode = context.CachedResponse.StatusCode;
foreach (var header in context.CachedResponse.Headers)
{
response.Headers.Add(header);
response.Headers[header.Key] = header.Value;
}

response.Headers[HeaderNames.Age] = context.CachedEntryAge.Value.TotalSeconds.ToString("F0", CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -262,7 +262,7 @@ internal async Task FinalizeCacheHeadersAsync(ResponseCachingContext context)
{
if (!string.Equals(header.Key, HeaderNames.Age, StringComparison.OrdinalIgnoreCase))
{
context.CachedResponse.Headers.Add(header);
context.CachedResponse.Headers[header.Key] = header.Value;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ await cache.SetAsync(
LoggedMessage.CachedResponseServed);
}

[Fact]
public async Task TryServeFromCacheAsync_CachedResponseFound_OverwritesExistingHeaders()
{
var cache = new TestResponseCache();
var sink = new TestSink();
var middleware = TestUtils.CreateTestMiddleware(testSink: sink, cache: cache, keyProvider: new TestResponseCachingKeyProvider("BaseKey"));
var context = TestUtils.CreateTestContext();

context.HttpContext.Response.Headers["MyHeader"] = "OldValue";
await cache.SetAsync(
"BaseKey",
new CachedResponse()
{
Headers = new HeaderDictionary()
{
{ "MyHeader", "NewValue" }
},
Body = new SegmentReadStream(new List<byte[]>(0), 0)
},
TimeSpan.Zero);

Assert.True(await middleware.TryServeFromCacheAsync(context));
Assert.Equal("NewValue", context.HttpContext.Response.Headers["MyHeader"]);
Assert.Equal(1, cache.GetCount);
TestUtils.AssertLoggedMessages(
sink.Writes,
LoggedMessage.CachedResponseServed);
}

[Fact]
public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseNotFound_Fails()
{
Expand Down