-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Separate out the cache clearing into another class
- Loading branch information
1 parent
4375845
commit 274870e
Showing
9 changed files
with
101 additions
and
52 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Dfe.PlanTech.Domain.Persistence.Models; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dfe.PlanTech.AzureFunctions.Services; | ||
|
||
public class CacheHandler( | ||
HttpClient httpClient, | ||
CacheRefreshConfiguration cacheRefreshConfiguration, | ||
ILogger<CacheHandler> logger): ICacheHandler | ||
{ | ||
/// <summary> | ||
/// Makes a call to the plan tech web app that invalidates the database cache. | ||
/// </summary> | ||
public async Task RequestCacheClear() | ||
{ | ||
if (cacheRefreshConfiguration.ApiKeyName is null) | ||
{ | ||
logger.LogError("No Api Key name has been configured but is required for clearing the website cache"); | ||
return; | ||
} | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Post, cacheRefreshConfiguration.Endpoint); | ||
request.Headers.Add(cacheRefreshConfiguration.ApiKeyName, cacheRefreshConfiguration.ApiKeyValue); | ||
|
||
await httpClient.SendAsync(request); | ||
} | ||
} |
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,6 @@ | ||
namespace Dfe.PlanTech.AzureFunctions.Services; | ||
|
||
public interface ICacheHandler | ||
{ | ||
Task RequestCacheClear(); | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
11 changes: 11 additions & 0 deletions
11
tests/Dfe.PlanTech.AzureFunctions.UnitTests/Helpers/MockHttpHandler.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,11 @@ | ||
namespace Dfe.PlanTech.AzureFunctions.UnitTests.Helpers; | ||
|
||
public class MockHttpHandler : HttpMessageHandler | ||
{ | ||
public readonly List<HttpRequestMessage> Requests = []; | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
Requests.Add(request); | ||
return await Task.FromResult(new HttpResponseMessage()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Dfe.PlanTech.AzureFunctions.UnitTests/Services/CacheHandlerTests.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,41 @@ | ||
using Dfe.PlanTech.AzureFunctions.Services; | ||
using Dfe.PlanTech.AzureFunctions.UnitTests.Helpers; | ||
using Dfe.PlanTech.Domain.Persistence.Models; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
|
||
namespace Dfe.PlanTech.AzureFunctions.UnitTests.Services; | ||
|
||
public class CacheHandlerTests | ||
{ | ||
private const string _refresh_api_key_name = "X-WEBSITE-CACHE-CLEAR-API-KEY"; | ||
private const string _refresh_api_key_value = "mock-refresh-api-key"; | ||
private const string _refresh_endpoint = "http://mock-refresh-endpoint/"; | ||
|
||
private readonly CacheRefreshConfiguration _cacheRefreshConfiguration; | ||
private readonly HttpClient _httpClient; | ||
private readonly MockHttpHandler _httpMessageHandler; | ||
private readonly CacheHandler _cacheHandler; | ||
private readonly ILogger<CacheHandler> _logger; | ||
|
||
public CacheHandlerTests() | ||
{ | ||
_cacheRefreshConfiguration = new CacheRefreshConfiguration(_refresh_endpoint, _refresh_api_key_name, _refresh_api_key_value); | ||
_httpMessageHandler = new MockHttpHandler(); | ||
_httpClient = new HttpClient(_httpMessageHandler); | ||
_logger = Substitute.For<ILogger<CacheHandler>>(); | ||
_cacheHandler = new CacheHandler(_httpClient, _cacheRefreshConfiguration, _logger); | ||
} | ||
|
||
[Fact] | ||
public async Task CacheHandler_Should_Make_Request_With_CorrectHeaders() | ||
{ | ||
await _cacheHandler.RequestCacheClear(); | ||
Assert.Single(_httpMessageHandler.Requests); | ||
var request = _httpMessageHandler.Requests.First(); | ||
Assert.NotNull(request.RequestUri); | ||
Assert.Equal(_refresh_endpoint, request.RequestUri.ToString()); | ||
Assert.True(request.Headers.TryGetValues(_refresh_api_key_name, out var headerValues)); | ||
Assert.Contains(_refresh_api_key_value, headerValues); | ||
} | ||
} |