Skip to content

Commit

Permalink
Merge pull request #392 from DFE-Digital/response-cache
Browse files Browse the repository at this point in the history
Allow GET requests to be cached privately for 5m
  • Loading branch information
ethax-ross authored Dec 7, 2020
2 parents 58fb0d8 + 7845bb0 commit ee263ab
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;

namespace GetIntoTeachingApi.Attributes
{
public class PrivateShortTermResponseCacheAttribute : ResponseCacheAttribute
{
public PrivateShortTermResponseCacheAttribute()
{
Location = ResponseCacheLocation.Client;
Duration = 300;
}
}
}
3 changes: 2 additions & 1 deletion GetIntoTeachingApi/Controllers/PrivacyPoliciesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace GetIntoTeachingApi.Controllers
{
[Route("api/privacy_policies")]
[ApiController]
[LogRequests]
[LogRequests]
[PrivateShortTermResponseCache]
[Authorize]
public class PrivacyPoliciesController : ControllerBase
{
Expand Down
11 changes: 7 additions & 4 deletions GetIntoTeachingApi/Controllers/TeachingEventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace GetIntoTeachingApi.Controllers
{
[Route("api/teaching_events")]
[ApiController]
[LogRequests]
[LogRequests]
[Authorize]
public class TeachingEventsController : ControllerBase
{
Expand All @@ -40,7 +40,8 @@ public TeachingEventsController(
}

[HttpGet]
[CrmETag]
[CrmETag]
[PrivateShortTermResponseCache]
[Route("search_indexed_by_type")]
[SwaggerOperation(
Summary = "Searches for teaching events, returning grouped by type.",
Expand Down Expand Up @@ -68,7 +69,8 @@ public async Task<IActionResult> SearchIndexedByType(
}

[HttpGet]
[CrmETag]
[CrmETag]
[PrivateShortTermResponseCache]
[Route("upcoming_indexed_by_type")]
[SwaggerOperation(
Summary = "Retrieves upcoming teaching events grouped by type.",
Expand All @@ -83,7 +85,8 @@ public async Task<IActionResult> SearchIndexedByType(
}

[HttpGet]
[CrmETag]
[CrmETag]
[PrivateShortTermResponseCache]
[Route("{readableId}")]
[SwaggerOperation(
Summary = "Retrieves an event.",
Expand Down
3 changes: 2 additions & 1 deletion GetIntoTeachingApi/Controllers/TypesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace GetIntoTeachingApi.Controllers
{
[Route("api/types")]
[ApiController]
[LogRequests]
[LogRequests]
[PrivateShortTermResponseCache]
[Authorize]
public class TypesController : ControllerBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FluentAssertions;
using GetIntoTeachingApi.Attributes;
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace GetIntoTeachingApiTests.Attributes
{
public class PrivateShortTermResponseCacheAttributeTests
{
[Fact]
public void Constructor_CorrectlySetsCacheControl()
{
var cache = new PrivateShortTermResponseCacheAttribute();

cache.Location.Should().Be(ResponseCacheLocation.Client);
cache.Duration.Should().Be(300);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public void Authorize_IsPresent()
public void LogRequests_IsPresent()
{
typeof(PrivacyPoliciesController).Should().BeDecoratedWith<LogRequestsAttribute>();
}

[Fact]
public void PrivateShortTermResponseCache_IsPresent()
{
typeof(PrivacyPoliciesController).Should().BeDecoratedWith<PrivateShortTermResponseCacheAttribute>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public void CrmETag_IsPresent()
methods.ForEach(m => typeof(TeachingEventsController).GetMethod(m).Should().BeDecoratedWith<CrmETagAttribute>());
}

[Fact]
public void CrmETagPrivateShortTermResponseCache_IsPresent()
{
JobStorage.Current = new Mock<JobStorage>().Object;
var methods = new[] { "Get", "SearchIndexedByType", "UpcomingIndexedByType" };

methods.ForEach(m => typeof(TeachingEventsController).GetMethod(m).Should().BeDecoratedWith<PrivateShortTermResponseCacheAttribute>());
}

[Fact]
public void AddAttendee_InvalidRequest_RespondsWithValidationErrors()
{
Expand Down
6 changes: 6 additions & 0 deletions GetIntoTeachingApiTests/Controllers/TypesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public void Authorize_IsPresent()
public void LogRequests_IsPresent()
{
typeof(TypesController).Should().BeDecoratedWith<LogRequestsAttribute>();
}

[Fact]
public void PrivateShortTermResponseCache_IsPresent()
{
typeof(TypesController).Should().BeDecoratedWith<PrivateShortTermResponseCacheAttribute>();
}

[Fact]
Expand Down

0 comments on commit ee263ab

Please sign in to comment.