From d7baa8f45cf0c4feffe9470424968506a22aba34 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Fri, 3 Jul 2020 22:59:39 +0200 Subject: [PATCH] Made retry-after case insensitive, fixes #475 --- .../SimpleRetryHandlerTest.cs | 29 +++++++++++++++++++ .../RetryHandlers/SimpleRetryHandler.cs | 4 ++- 2 files changed, 32 insertions(+), 1 deletion(-) rename SpotifyAPI.Web.Tests/{Http => RetryHandlers}/SimpleRetryHandlerTest.cs (86%) diff --git a/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs b/SpotifyAPI.Web.Tests/RetryHandlers/SimpleRetryHandlerTest.cs similarity index 86% rename from SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs rename to SpotifyAPI.Web.Tests/RetryHandlers/SimpleRetryHandlerTest.cs index 4e13f516..cc0e77fc 100644 --- a/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs +++ b/SpotifyAPI.Web.Tests/RetryHandlers/SimpleRetryHandlerTest.cs @@ -11,6 +11,35 @@ namespace SpotifyAPI.Web [TestFixture] public class SimpleRetryHandlerTest { + [Test] + public void HandleRetry_WorksWithLowerCaseHeader() + { + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); + setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary { + { "retry-after", "50" } + }); + + var retryCalled = 0; + setup.Retry = (IRequest request) => + { + retryCalled++; + return Task.FromResult(setup.Response.Object); + }; + + var handler = new SimpleRetryHandler(setup.Sleep.Object) + { + TooManyRequestsConsumesARetry = true, + RetryTimes = 1 + }; + Assert.DoesNotThrowAsync(async () => + { + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); + }); + + Assert.AreEqual(1, retryCalled); + } + [Test] public async Task HandleRetry_TooManyRequestsWithNoSuccess() { diff --git a/SpotifyAPI.Web/RetryHandlers/SimpleRetryHandler.cs b/SpotifyAPI.Web/RetryHandlers/SimpleRetryHandler.cs index bd18ac38..275c7d8f 100644 --- a/SpotifyAPI.Web/RetryHandlers/SimpleRetryHandler.cs +++ b/SpotifyAPI.Web/RetryHandlers/SimpleRetryHandler.cs @@ -57,7 +57,9 @@ namespace SpotifyAPI.Web { return null; } - if (int.TryParse(response.Headers["Retry-After"], out int secondsToWait)) + if ( + int.TryParse(response.Headers.GetValueOrDefault("Retry-After"), out int secondsToWait) + || int.TryParse(response.Headers.GetValueOrDefault("retry-after"), out secondsToWait)) { return TimeSpan.FromSeconds(secondsToWait); }