-
Notifications
You must be signed in to change notification settings - Fork 4
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 #16 from PhilPJL/develop
Merge develop into master
- Loading branch information
Showing
26 changed files
with
1,712 additions
and
1,341 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
86 changes: 45 additions & 41 deletions
86
ImpSoft.OctopusEnergy.Api.Tests/FakeHttpMessageHandler.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 |
---|---|---|
@@ -1,61 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using CommunityToolkit.Diagnostics; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ImpSoft.OctopusEnergy.Api.Tests | ||
namespace ImpSoft.OctopusEnergy.Api.Tests; | ||
|
||
internal class FakeHttpMessageHandler<TResponse> : HttpClientHandler where TResponse : class, new() | ||
{ | ||
class FakeHttpMessageHandler<TResponse> : HttpClientHandler where TResponse : class | ||
public FakeHttpMessageHandler(Uri expectedUri, TResponse response) | ||
{ | ||
public FakeHttpMessageHandler(Uri expectedUri, TResponse response) | ||
{ | ||
Preconditions.IsNotNull(response, nameof(response)); | ||
AutomaticDecompression = System.Net.DecompressionMethods.All; | ||
ResponseObject = response; | ||
ExpectedRequestUri = expectedUri; | ||
} | ||
Guard.IsNotNull(response); | ||
Guard.IsNotNull(expectedUri); | ||
|
||
public FakeHttpMessageHandler(Uri expectedUri, string response) | ||
{ | ||
Preconditions.IsNotNullOrWhiteSpace(response, nameof(response)); | ||
AutomaticDecompression = System.Net.DecompressionMethods.All; | ||
ResponseString = response; | ||
ExpectedRequestUri = expectedUri; | ||
} | ||
#if NET48_OR_GREATER | ||
AutomaticDecompression = System.Net.DecompressionMethods.None; | ||
#else | ||
AutomaticDecompression = System.Net.DecompressionMethods.All; | ||
#endif | ||
ResponseObject = response; | ||
ExpectedRequestUri = expectedUri; | ||
|
||
public FakeHttpMessageHandler(IList<(Uri, TResponse response)> pages) | ||
{ | ||
// TODO: enable faking/testing paged responses | ||
} | ||
ResponseString = string.Empty; | ||
} | ||
|
||
private TResponse ResponseObject { get; } | ||
private string ResponseString { get; } | ||
public Uri ExpectedRequestUri { get; } | ||
// ReSharper disable once MemberCanBePrivate.Global | ||
public Uri ExpectedRequestUri { get; } | ||
private TResponse? ResponseObject { get; } | ||
private string ResponseString { get; } | ||
|
||
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
await Task.Yield(); | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
await Task.Yield(); | ||
|
||
if (request.RequestUri.AbsoluteUri != ExpectedRequestUri.AbsoluteUri) | ||
if (request.RequestUri?.AbsoluteUri != ExpectedRequestUri.AbsoluteUri) | ||
{ | ||
return new HttpResponseMessage | ||
{ | ||
return new HttpResponseMessage | ||
{ | ||
StatusCode = System.Net.HttpStatusCode.NotFound | ||
}; | ||
} | ||
StatusCode = System.Net.HttpStatusCode.NotFound | ||
}; | ||
} | ||
|
||
Debug.WriteLine(ExpectedRequestUri.AbsoluteUri); | ||
Debug.WriteLine(ExpectedRequestUri.AbsoluteUri); | ||
|
||
var httpResponse = ResponseObject != null | ||
? new HttpResponseMessage { Content = new StringContent(JsonSerializer.Serialize(ResponseObject)) } | ||
: new HttpResponseMessage { Content = new StringContent(ResponseString) }; | ||
var httpResponse = ResponseObject != null | ||
? new HttpResponseMessage { Content = new StringContent(JsonSerializer.Serialize(ResponseObject)) } | ||
: new HttpResponseMessage { Content = new StringContent(ResponseString) }; | ||
|
||
if (httpResponse.Content.Headers.ContentType != null) | ||
{ | ||
httpResponse.Content.Headers.ContentType.MediaType = "application/json"; | ||
|
||
return httpResponse; | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException( | ||
$"{nameof(httpResponse.Content.Headers.ContentType)} is unexpectedly null."); | ||
} | ||
|
||
return httpResponse; | ||
} | ||
} | ||
} |
Oops, something went wrong.