Skip to content

Commit

Permalink
Merge pull request #215 from AdrianJSClark/214-504-response-cant-be-d…
Browse files Browse the repository at this point in the history
…eserialized-to-errorresponse

Handle Wider Range of HTTP Error Responses
  • Loading branch information
AdrianJSClark authored Jul 10, 2024
2 parents 15da8f7 + dae28c8 commit 2ce48b4
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 762 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// © 2023-2024 Adrian Clark
// This file is licensed to you under the MIT license.

using System.Net;
using System.Net.Http;
using Aydsko.iRacingData.Constants;
using Aydsko.iRacingData.Exceptions;
Expand Down Expand Up @@ -1489,6 +1490,25 @@ public async Task GetWeatherForecastAsync()
});
}

[Test(TestOf = typeof(DataClient))]
public async Task GetMemberProfileFailsWithGatewayTimeoutAsync()
{
await MessageHandler.QueueResponsesAsync("ResponseUnknown504", true).ConfigureAwait(false);

Assert.That(async () => await sut.GetMemberProfileAsync(341554).ConfigureAwait(false),
Throws.InstanceOf<iRacingUnknownResponseException>()
.And.Property(nameof(iRacingUnknownResponseException.ResponseHttpStatusCode)).EqualTo(HttpStatusCode.GatewayTimeout));
}

[Test(TestOf = typeof(DataClient))]
public async Task GetMemberProfileFailsOnLoginWithGatewayTimeoutAsync()
{
await MessageHandler.QueueResponsesAsync("ResponseUnknown504", false).ConfigureAwait(false);

Assert.That(async () => await sut.GetMemberProfileAsync(341554).ConfigureAwait(false),
Throws.InstanceOf<iRacingLoginFailedException>());
}

protected override void Dispose(bool disposing)
{
if (disposing)
Expand Down
9 changes: 8 additions & 1 deletion src/Aydsko.iRacingData.UnitTests/MockedHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -94,10 +95,16 @@ public async Task QueueResponsesAsync(string testName, bool prefixLoginResponse
statusCode = HttpStatusCode.OK;
}

var contentType = "text/json";
if (responseDictionary.TryGetValue("contentType", out var contentTypeElement))
{
contentType = contentTypeElement.ToString();
}

#pragma warning disable CA2000 // Dispose objects before losing scope - These responses are intentionally created to be returned later. This is OK in a test helper.
var responseMessage = new HttpResponseMessage(statusCode)
{
Content = new StringContent(responseDictionary["content"].ToString(), Encoding.UTF8, "text/json")
Content = new StringContent(responseDictionary["content"].ToString(), Encoding.UTF8, contentType)
};
#pragma warning restore CA2000 // Dispose objects before losing scope

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"statuscode": 504,
"headers": {
"x-ratelimit-remaining": "238",
"x-ratelimit-limit": "240",
"x-ratelimit-reset": "1657533187"
},
"contentType": "text/html",
"content": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\r\n<TITLE>ERROR: The request could not be satisfied</TITLE>\r\n</HEAD>< BODY>\r\n<H1>504 ERROR</H1>\r\n<H2>The request could not be satisfied.</H2>\r\n<HR noshade size=\"1px\">\r\nCloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.\r\nWe can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.\r\n<BR clear=\"all\">\r\nIf you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.\r\n<BR clear=\"all\">\r\n<HR noshade size=\"1px\">\r\n<PRE>\r\nGenerated by cloudfront (CloudFront)\r\nRequest ID: eSHdZe3nj2sm7o-Jg8cdkzntk-xMUR8lISRFnLBAI12OTMPaM4cnTg==\r\n</PRE>\r\n<ADDRESS>\r\n</ADDRESS>\r\n</BODY></HTML>"
}
2 changes: 1 addition & 1 deletion src/Aydsko.iRacingData/Aydsko.iRacingData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<!-- Package Validation (https://docs.microsoft.com/en-us/dotnet/fundamentals/package-validation/overview) -->
<EnablePackageValidation>true</EnablePackageValidation>
<PackageValidationBaselineVersion>2402.2.0</PackageValidationBaselineVersion>
<PackageValidationBaselineVersion>2403.1.0</PackageValidationBaselineVersion>
<GenerateCompatibilitySuppressionFile>true</GenerateCompatibilitySuppressionFile>
<EnableStrictModeForCompatibleFrameworksInPackage>true</EnableStrictModeForCompatibleFrameworksInPackage>
<EnableStrictModeForCompatibleTfms>true</EnableStrictModeForCompatibleTfms>
Expand Down
Loading

0 comments on commit 2ce48b4

Please sign in to comment.