Skip to content

Commit

Permalink
Merge pull request #1840 from qdraw/feature/202411_cache-control
Browse files Browse the repository at this point in the history
add headers to not cache result
  • Loading branch information
qdraw authored Nov 25, 2024
2 parents bb68e16 + 7e17aea commit 6db94f2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 14 deletions.
4 changes: 4 additions & 0 deletions starsky/starsky/Controllers/ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using starsky.foundation.storage.Interfaces;
using starsky.foundation.storage.Storage;
using starsky.foundation.worker.Interfaces;
using starsky.Helpers;
using starsky.project.web.Helpers;

namespace starsky.Controllers;
Expand Down Expand Up @@ -86,6 +87,7 @@ await _bgTaskQueue.QueueBackgroundWorkItemAsync(
[HttpGet("/api/export/zip/{f}.zip")]
[ProducesResponseType(200)] // "zip file"
[ProducesResponseType(206)] // "Not Ready"
[Produces("application/json")]
public ActionResult Status(string f, bool json = false)
{
if ( !ModelState.IsValid )
Expand All @@ -99,6 +101,8 @@ public ActionResult Status(string f, bool json = false)
case null:
return NotFound("Path is not found");
case false:
// Make sure the status is not cached
CacheControlOverwrite.SetNoCacheResponseHeaders(Request);
Response.StatusCode = 206;
return Json("Not Ready");
}
Expand Down
35 changes: 21 additions & 14 deletions starsky/starsky/Helpers/CacheControlOverwrite.cs
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());
}
}
2 changes: 2 additions & 0 deletions starsky/starskytest/Controllers/ExportControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ public void Status_Returns_JsonResult_When_JsonIsTrue()
new FakeIExport(new Dictionary<string, bool> { { f, true } });
var exportController = new ExportController(_bgTaskQueue,
new FakeSelectorStorage(fakeStorage), fakeExportService);
exportController.ControllerContext.HttpContext = new DefaultHttpContext();

// Act
var result = exportController.Status(f, json);
Expand All @@ -525,6 +526,7 @@ public void Status_Returns_FileResult_When_JsonIsFalse()
new FakeIExport(new Dictionary<string, bool> { { f, true } });
var exportController = new ExportController(_bgTaskQueue,
new FakeSelectorStorage(fakeStorage), fakeExportService);
exportController.ControllerContext.HttpContext = new DefaultHttpContext();

// Act
var result = exportController.Status(f);
Expand Down
76 changes: 76 additions & 0 deletions starsky/starskytest/starsky/Helpers/CacheControlOverwriteTests.cs
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);
}
}

0 comments on commit 6db94f2

Please sign in to comment.