-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1840 from qdraw/feature/202411_cache-control
add headers to not cache result
- Loading branch information
Showing
4 changed files
with
103 additions
and
14 deletions.
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
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 |
---|---|---|
@@ -1,22 +1,29 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace starsky.Helpers | ||
namespace starsky.Helpers; | ||
|
||
public static class CacheControlOverwrite | ||
{ | ||
public static class CacheControlOverwrite | ||
private const string CacheControl = "Cache-Control"; | ||
|
||
public static void SetNoCacheResponseHeaders(HttpRequest request) | ||
{ | ||
request.HttpContext.Response.Headers.Remove(CacheControl); | ||
request.HttpContext.Response.Headers.Append(CacheControl, | ||
"no-store, no-cache, must-revalidate"); | ||
} | ||
|
||
/// <summary> | ||
/// For Performance on slow devices | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="time">29030400 = 4 weeks</param> | ||
public static void SetExpiresResponseHeaders(HttpRequest request, int time = 29030400) | ||
{ | ||
request.HttpContext.Response.Headers.Remove("Cache-Control"); | ||
request.HttpContext.Response.Headers.Append("Cache-Control", $"private,max-age={time}"); | ||
/// <summary> | ||
/// For Performance on slow devices | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="time">29030400 = 4 weeks</param> | ||
public static void SetExpiresResponseHeaders(HttpRequest request, int time = 29030400) | ||
{ | ||
request.HttpContext.Response.Headers.Remove(CacheControl); | ||
request.HttpContext.Response.Headers.Append(CacheControl, $"private,max-age={time}"); | ||
|
||
request.HttpContext.Response.Headers.Remove("Expires"); | ||
request.HttpContext.Response.Headers.Append("Expires", time.ToString()); | ||
} | ||
request.HttpContext.Response.Headers.Remove("Expires"); | ||
request.HttpContext.Response.Headers.Append("Expires", time.ToString()); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
starsky/starskytest/starsky/Helpers/CacheControlOverwriteTests.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,76 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.Helpers; | ||
|
||
namespace starskytest.starsky.Helpers; | ||
|
||
[TestClass] | ||
public class CacheControlOverwriteTests | ||
{ | ||
[TestMethod] | ||
public void SetNoCacheResponseHeaders_ShouldSetCorrectHeaders() | ||
{ | ||
var context = new DefaultHttpContext(); | ||
var request = context.Request; | ||
|
||
CacheControlOverwrite.SetNoCacheResponseHeaders(request); | ||
|
||
Assert.AreEqual("no-store, no-cache, must-revalidate", | ||
request.HttpContext.Response.Headers.CacheControl); | ||
} | ||
|
||
[TestMethod] | ||
public void SetExpiresResponseHeaders_ShouldSetCorrectHeaders() | ||
{ | ||
var context = new DefaultHttpContext(); | ||
var request = context.Request; | ||
const int time = 29030401; | ||
|
||
CacheControlOverwrite.SetExpiresResponseHeaders(request, time); | ||
|
||
Assert.AreEqual($"private,max-age={time}", | ||
request.HttpContext.Response.Headers.CacheControl); | ||
Assert.AreEqual(time.ToString(), request.HttpContext.Response.Headers.Expires); | ||
} | ||
|
||
[TestMethod] | ||
public void SetExpiresResponseHeaders_ShouldSetDefaultTimeIfNotProvided() | ||
{ | ||
var context = new DefaultHttpContext(); | ||
var request = context.Request; | ||
|
||
CacheControlOverwrite.SetExpiresResponseHeaders(request); | ||
|
||
Assert.AreEqual("private,max-age=29030400", | ||
request.HttpContext.Response.Headers.CacheControl); | ||
Assert.AreEqual("29030400", request.HttpContext.Response.Headers.Expires); | ||
} | ||
|
||
[TestMethod] | ||
public void SetNoCacheResponseHeaders_ShouldRemoveExistingCacheControlHeader() | ||
{ | ||
var context = new DefaultHttpContext(); | ||
var request = context.Request; | ||
request.HttpContext.Response.Headers.Append("Cache-Control", "existing-value"); | ||
|
||
CacheControlOverwrite.SetNoCacheResponseHeaders(request); | ||
|
||
Assert.AreEqual("no-store, no-cache, must-revalidate", | ||
request.HttpContext.Response.Headers.CacheControl); | ||
} | ||
|
||
[TestMethod] | ||
public void SetExpiresResponseHeaders_ShouldRemoveExistingCacheControlAndExpiresHeaders() | ||
{ | ||
var context = new DefaultHttpContext(); | ||
var request = context.Request; | ||
request.HttpContext.Response.Headers.Append("Cache-Control", "existing-value"); | ||
request.HttpContext.Response.Headers.Append("Expires", "existing-value"); | ||
|
||
CacheControlOverwrite.SetExpiresResponseHeaders(request); | ||
|
||
Assert.AreEqual("private,max-age=29030400", | ||
request.HttpContext.Response.Headers.CacheControl); | ||
Assert.AreEqual("29030400", request.HttpContext.Response.Headers.Expires); | ||
} | ||
} |