Skip to content

Commit

Permalink
address case where header is manually checked
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftkey committed Apr 9, 2020
1 parent e87b851 commit e835013
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Octokit.Tests/Exceptions/AbuseExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public void WithRetryAfterHeader_PopulatesRetryAfterSeconds()
Assert.Equal(30, abuseException.RetryAfterSeconds);
}

[Fact]
public void WithRetryAfterCaseInsensitiveHeader_PopulatesRetryAfterSeconds()
{
var headerDictionary = new Dictionary<string, string> { { "retry-after", "20" } };

var response = new Response(HttpStatusCode.Forbidden, null, headerDictionary, "application/json");
var abuseException = new AbuseException(response);

Assert.Equal(20, abuseException.RetryAfterSeconds);
}

[Fact]
public void WithZeroHeaderValue_RetryAfterSecondsIsZero()
{
Expand Down
8 changes: 5 additions & 3 deletions Octokit/Exceptions/AbuseException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
#if !NO_SERIALIZABLE
using System.Runtime.Serialization;
Expand Down Expand Up @@ -44,11 +46,11 @@ public AbuseException(IResponse response, Exception innerException)

private static int? ParseRetryAfterSeconds(IResponse response)
{
string secondsValue;
if (!response.Headers.TryGetValue("Retry-After", out secondsValue)) { return null; }
var header = response.Headers.FirstOrDefault(h => string.Equals(h.Key, "Retry-After", StringComparison.OrdinalIgnoreCase));
if (header.Equals(default(KeyValuePair<string, string>))) { return null; }

int retrySeconds;
if (!int.TryParse(secondsValue, out retrySeconds)) { return null; }
if (!int.TryParse(header.Value, out retrySeconds)) { return null; }
if (retrySeconds < 0) { return null; }

return retrySeconds;
Expand Down

0 comments on commit e835013

Please sign in to comment.