Repository: hub4j/github-api
Pull request #1972 (2024-10-09 18:04:41)

Do not assume server time is in sync with local machine time on rate limit path
See discussion in #1909 and companion PR in #1971. On this test, I did try and cover the guard for missing dates with tests.

# Description

<!-- Describe your change here -->

# Before submitting a PR:

- [X] Changes must not break binary backwards compatibility. If you are unclear on how to make the change you think is needed while maintaining backward compatibility, [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [X] Add JavaDocs and other comments explaining the behavior. 
- [X] When adding or updating methods that fetch entities, add `@link` JavaDoc entries to the relevant documentation on https://docs.github.com/en/rest . 
- [X] Add tests that cover any added or changed code. This generally requires capturing snapshot test data. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [X] Run `mvn -D enable-ci clean install site` locally. If this command doesn't succeed, your change will not pass CI.
- [X] Push your changes to a branch other than `main`. You will create your PR from that branch.

# When creating a PR: 

- [X] Fill in the "Description" above with clear summary of the changes. This includes:
  - [X] If this PR fixes one or more issues, include "Fixes #<issue number>" lines for each issue. 
  - [X] Provide links to relevant documentation on https://docs.github.com/en/rest where possible. If not including links, explain why not.
- [X] All lines of new code should be covered by tests as reported by code coverage. Any lines that are not covered must have PR comments explaining why they cannot be covered. For example, "Reaching this particular exception is hard and is not a particular common scenario."
- [X] Enable "Allow edits from maintainers".


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hub4j/github-api/pull/1972?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j) Report
Attention: Patch coverage is `75.00000%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 83.10%. Comparing base [(`0c9e195`)](https://app.codecov.io/gh/hub4j/github-api/commit/0c9e1958aca4a0680b4b04e85e73e3469175b9a0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j) to head [(`317f1cb`)](https://app.codecov.io/gh/hub4j/github-api/commit/317f1cb3cd54ec5a8608ad9c462735a2bca50d43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j).

| [Files with missing lines](https://app.codecov.io/gh/hub4j/github-api/pull/1972?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j) | Patch % | Lines |
|---|---|---|
| [...ava/org/kohsuke/github/GitHubRateLimitHandler.java](https://app.codecov.io/gh/hub4j/github-api/pull/1972?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fkohsuke%2Fgithub%2FGitHubRateLimitHandler.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j#diff-c3JjL21haW4vamF2YS9vcmcva29oc3VrZS9naXRodWIvR2l0SHViUmF0ZUxpbWl0SGFuZGxlci5qYXZh) | 75.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/hub4j/github-api/pull/1972?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##               main    #1972   +/-   ##
=========================================
  Coverage     83.09%   83.10%           
- Complexity     2329     2331    +2     
=========================================
  Files           231      231           
  Lines          7168     7172    +4     
  Branches        377      378    +1     
=========================================
+ Hits           5956     5960    +4     
  Misses          974      974           
  Partials        238      238           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hub4j/github-api/pull/1972?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hub4j).


Patch:
diff --git a/src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java b/src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java
index 977c8bf00..8cb591126 100644
--- a/src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java
+++ b/src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java
@@ -5,6 +5,9 @@ import org.kohsuke.github.connector.GitHubConnectorResponse;
 
 import java.io.IOException;
 import java.io.InterruptedIOException;
+import java.time.Duration;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
 
 import javax.annotation.Nonnull;
 
@@ -21,6 +24,11 @@ import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
  */
 public abstract class GitHubRateLimitHandler extends GitHubConnectorResponseErrorHandler {
 
+    /**
+     * On a wait, even if the response suggests a very short wait, wait for a minimum duration.
+     */
+    private static final int MINIMUM_RATE_LIMIT_RETRY_MILLIS = 1000;
+
     /**
      * Create default GitHubRateLimitHandler instance
      */
@@ -71,15 +79,29 @@ public abstract class GitHubRateLimitHandler extends GitHubConnectorResponseErro
                 throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
             }
         }
+    };
 
-        private long parseWaitTime(GitHubConnectorResponse connectorResponse) {
-            String v = connectorResponse.header("X-RateLimit-Reset");
-            if (v == null)
-                return 60 * 1000; // can't tell, return 1 min
+    /*
+     * Exposed for testability. Given an http response, find the rate limit reset header field and parse it. If no
+     * header is found, wait for a reasonably amount of time.
+     */
+    long parseWaitTime(GitHubConnectorResponse connectorResponse) {
+        String v = connectorResponse.header("X-RateLimit-Reset");
+        if (v == null)
+            return Duration.ofMinutes(1).toMillis(); // can't tell, return 1 min
 
-            return Math.max(1000, Long.parseLong(v) * 1000 - System.currentTimeMillis());
+        // Don't use ZonedDateTime.now(), because the local and remote server times may not be in sync
+        // Instead, we can take advantage of the Date field in the response to see what time the remote server
+        // thinks it is
+        String dateField = connectorResponse.header("Date");
+        ZonedDateTime now;
+        if (dateField != null) {
+            now = ZonedDateTime.parse(dateField, DateTimeFormatter.RFC_1123_DATE_TIME);
+        } else {
+            now = ZonedDateTime.now();
         }
-    };
+        return Math.max(MINIMUM_RATE_LIMIT_RETRY_MILLIS, (Long.parseLong(v) - now.toInstant().getEpochSecond()) * 1000);
+    }
 
     /**
      * Fail immediately.


Test patch:
diff --git a/src/test/java/org/kohsuke/github/RateLimitHandlerTest.java b/src/test/java/org/kohsuke/github/RateLimitHandlerTest.java
index 5606d00a0..5e326cbba 100644
--- a/src/test/java/org/kohsuke/github/RateLimitHandlerTest.java
+++ b/src/test/java/org/kohsuke/github/RateLimitHandlerTest.java
@@ -1,6 +1,7 @@
 package org.kohsuke.github;
 
 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+import org.jetbrains.annotations.NotNull;
 import org.junit.Test;
 import org.kohsuke.github.connector.GitHubConnectorResponse;
 
@@ -117,16 +118,56 @@ public class RateLimitHandlerTest extends AbstractGitHubWireMockTest {
     /**
      * Test handler wait.
      *
-     * @throws Exception
+     * @throws IOException
      *             the exception
      */
     @Test
-    public void testHandler_Wait() throws Exception {
+    public void testHandler_Wait() throws IOException {
+        // Customized response that templates the date to keep things working
+        snapshotNotAllowed();
+
+        gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
+                .withRateLimitHandler(new GitHubRateLimitHandler() {
+
+                    @Override
+                    public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
+                        long waitTime = GitHubRateLimitHandler.WAIT.parseWaitTime(connectorResponse);
+                        assertThat(waitTime, equalTo(3 * 1000l));
+
+                        GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
+                    }
+                })
+                .build();
+
+        gitHub.getMyself();
+        assertThat(mockGitHub.getRequestCount(), equalTo(1));
+
+        getTempRepository();
+        assertThat(mockGitHub.getRequestCount(), equalTo(3));
+    }
+
+    /**
+     * Test the wait logic in the case where the "Date" header field is missing from the response.
+     *
+     * @throws IOException
+     *             if the code under test throws that exception
+     */
+    @Test
+    public void testHandler_Wait_Missing_Date_Header() throws IOException {
         // Customized response that templates the date to keep things working
         snapshotNotAllowed();
 
         gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
-                .withRateLimitHandler(GitHubRateLimitHandler.WAIT)
+                .withRateLimitHandler(new GitHubRateLimitHandler() {
+
+                    @Override
+                    public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
+                        long waitTime = GitHubRateLimitHandler.WAIT.parseWaitTime(connectorResponse);
+                        assertThat(waitTime, equalTo(3 * 1000l));
+
+                        GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
+                    }
+                })
                 .build();
 
         gitHub.getMyself();
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait/mappings/2-r_h_t_Wait.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait/mappings/2-r_h_t_Wait.json
index e70c2b006..7ec052385 100644
--- a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait/mappings/2-r_h_t_Wait.json
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait/mappings/2-r_h_t_Wait.json
@@ -20,7 +20,7 @@
       "Status": "403 Forbidden",
       "X-RateLimit-Limit": "5000",
       "X-RateLimit-Remaining": "0",
-      "X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
+      "X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
       "Cache-Control": "private, max-age=60, s-maxage=60",
       "Vary": [
         "Accept, Authorization, Cookie, X-GitHub-OTP",
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/1-user.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/1-user.json
new file mode 100644
index 000000000..467313f14
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/1-user.json
@@ -0,0 +1,45 @@
+{
+  "login": "bitwiseman",
+  "id": 1958953,
+  "node_id": "MDQ6VXNlcjE5NTg5NTM=",
+  "avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
+  "gravatar_id": "",
+  "url": "https://api.github.com/users/bitwiseman",
+  "html_url": "https://github.com/bitwiseman",
+  "followers_url": "https://api.github.com/users/bitwiseman/followers",
+  "following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
+  "gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
+  "starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
+  "subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
+  "organizations_url": "https://api.github.com/users/bitwiseman/orgs",
+  "repos_url": "https://api.github.com/users/bitwiseman/repos",
+  "events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
+  "received_events_url": "https://api.github.com/users/bitwiseman/received_events",
+  "type": "User",
+  "site_admin": false,
+  "name": "Liam Newman",
+  "company": "Cloudbees, Inc.",
+  "blog": "",
+  "location": "Seattle, WA, USA",
+  "email": "bitwiseman@gmail.com",
+  "hireable": null,
+  "bio": "https://twitter.com/bitwiseman",
+  "public_repos": 181,
+  "public_gists": 7,
+  "followers": 146,
+  "following": 9,
+  "created_at": "2012-07-11T20:38:33Z",
+  "updated_at": "2020-02-06T17:29:39Z",
+  "private_gists": 8,
+  "total_private_repos": 10,
+  "owned_private_repos": 0,
+  "disk_usage": 33697,
+  "collaborators": 0,
+  "two_factor_authentication": true,
+  "plan": {
+    "name": "free",
+    "space": 976562499,
+    "collaborators": 0,
+    "private_repos": 10000
+  }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/3-r_h_t_Wait.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/3-r_h_t_Wait.json
new file mode 100644
index 000000000..59d27cff7
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/__files/3-r_h_t_Wait.json
@@ -0,0 +1,126 @@
+{
+  "id": 238757196,
+  "node_id": "MDEwOlJlcG9zaXRvcnkyMzg3NTcxOTY=",
+  "name": "temp-testHandler_Wait",
+  "full_name": "hub4j-test-org/temp-testHandler_Wait",
+  "private": false,
+  "owner": {
+    "login": "hub4j-test-org",
+    "id": 7544739,
+    "node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
+    "avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
+    "gravatar_id": "",
+    "url": "https://api.github.com/users/hub4j-test-org",
+    "html_url": "https://github.com/hub4j-test-org",
+    "followers_url": "https://api.github.com/users/hub4j-test-org/followers",
+    "following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
+    "gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
+    "starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
+    "subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
+    "organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
+    "repos_url": "https://api.github.com/users/hub4j-test-org/repos",
+    "events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
+    "received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
+    "type": "Organization",
+    "site_admin": false
+  },
+  "html_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait",
+  "description": "A test repository for testing the github-api project: temp-testHandler_Wait",
+  "fork": false,
+  "url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait",
+  "forks_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/forks",
+  "keys_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/keys{/key_id}",
+  "collaborators_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/collaborators{/collaborator}",
+  "teams_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/teams",
+  "hooks_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/hooks",
+  "issue_events_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues/events{/number}",
+  "events_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/events",
+  "assignees_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/assignees{/user}",
+  "branches_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/branches{/branch}",
+  "tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/tags",
+  "blobs_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/blobs{/sha}",
+  "git_tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/tags{/sha}",
+  "git_refs_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/refs{/sha}",
+  "trees_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/trees{/sha}",
+  "statuses_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/statuses/{sha}",
+  "languages_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/languages",
+  "stargazers_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/stargazers",
+  "contributors_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/contributors",
+  "subscribers_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/subscribers",
+  "subscription_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/subscription",
+  "commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/commits{/sha}",
+  "git_commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/git/commits{/sha}",
+  "comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/comments{/number}",
+  "issue_comment_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues/comments{/number}",
+  "contents_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/contents/{+path}",
+  "compare_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/compare/{base}...{head}",
+  "merges_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/merges",
+  "archive_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/{archive_format}{/ref}",
+  "downloads_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/downloads",
+  "issues_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/issues{/number}",
+  "pulls_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/pulls{/number}",
+  "milestones_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/milestones{/number}",
+  "notifications_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/notifications{?since,all,participating}",
+  "labels_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/labels{/name}",
+  "releases_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/releases{/id}",
+  "deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-testHandler_Wait/deployments",
+  "created_at": "2020-02-06T18:33:39Z",
+  "updated_at": "2020-02-06T18:33:43Z",
+  "pushed_at": "2020-02-06T18:33:41Z",
+  "git_url": "git://github.com/hub4j-test-org/temp-testHandler_Wait.git",
+  "ssh_url": "git@github.com:hub4j-test-org/temp-testHandler_Wait.git",
+  "clone_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait.git",
+  "svn_url": "https://github.com/hub4j-test-org/temp-testHandler_Wait",
+  "homepage": "http://github-api.kohsuke.org/",
+  "size": 0,
+  "stargazers_count": 0,
+  "watchers_count": 0,
+  "language": null,
+  "has_issues": true,
+  "has_projects": true,
+  "has_downloads": true,
+  "has_wiki": true,
+  "has_pages": false,
+  "forks_count": 0,
+  "mirror_url": null,
+  "archived": false,
+  "disabled": false,
+  "open_issues_count": 0,
+  "license": null,
+  "forks": 0,
+  "open_issues": 0,
+  "watchers": 0,
+  "default_branch": "main",
+  "permissions": {
+    "admin": true,
+    "push": true,
+    "pull": true
+  },
+  "temp_clone_token": "",
+  "allow_squash_merge": true,
+  "allow_merge_commit": true,
+  "allow_rebase_merge": true,
+  "delete_branch_on_merge": false,
+  "organization": {
+    "login": "hub4j-test-org",
+    "id": 7544739,
+    "node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
+    "avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
+    "gravatar_id": "",
+    "url": "https://api.github.com/users/hub4j-test-org",
+    "html_url": "https://github.com/hub4j-test-org",
+    "followers_url": "https://api.github.com/users/hub4j-test-org/followers",
+    "following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
+    "gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
+    "starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
+    "subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
+    "organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
+    "repos_url": "https://api.github.com/users/hub4j-test-org/repos",
+    "events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
+    "received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
+    "type": "Organization",
+    "site_admin": false
+  },
+  "network_count": 0,
+  "subscribers_count": 6
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/1-user.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/1-user.json
new file mode 100644
index 000000000..7be725572
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/1-user.json
@@ -0,0 +1,48 @@
+{
+  "id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
+  "name": "user",
+  "request": {
+    "url": "/user",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "1-user.json",
+    "headers": {
+      "Date": "Thu, 06 Feb 2020 18:33:32 GMT",
+      "Content-Type": "application/json; charset=utf-8",
+      "Server": "GitHub.com",
+      "Status": "200 OK",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4930",
+      "X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding"
+      ],
+      "ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
+      "Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
+      "X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
+      "X-Accepted-OAuth-Scopes": "",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
+      "Access-Control-Allow-Origin": "*",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "1; mode=block",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
+    }
+  },
+  "uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
+  "persistent": true,
+  "insertionIndex": 1
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/2-r_h_t_Wait.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/2-r_h_t_Wait.json
new file mode 100644
index 000000000..09da668e6
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/2-r_h_t_Wait.json
@@ -0,0 +1,50 @@
+{
+  "id": "79fb1092-8bf3-4274-bc8e-ca126c9d9261",
+  "name": "repos_hub4j-test-org_temp-testHandler_Wait_Missing_Date_Header",
+  "request": {
+    "url": "/repos/hub4j-test-org/temp-testHandler_Wait_Missing_Date_Header",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 403,
+    "body": "{\"message\":\"Must have push access to repository\",\"documentation_url\":\"https://developer.github.com/\"}",
+    "headers": {
+      "Content-Type": "application/json; charset=utf-8",
+      "Server": "GitHub.com",
+      "Status": "403 Forbidden",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "0",
+      "X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding"
+      ],
+      "ETag": "W/\"7ff3c96399f7ddf6129622d675ca9935\"",
+      "Last-Modified": "Thu, 06 Feb 2020 18:33:37 GMT",
+      "X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
+      "X-Accepted-OAuth-Scopes": "repo",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
+      "Access-Control-Allow-Origin": "*",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "1; mode=block",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "CC37:2605:3F982:4E949:5E3C5BFC"
+    }
+  },
+  "uuid": "79fb1092-8bf3-4274-bc8e-ca126c9d9261",
+  "persistent": true,
+  "scenarioName": "scenario-1-repos-hub4j-test-org-temp-testHandler_Wait",
+  "requiredScenarioState": "Started",
+  "newScenarioState": "scenario-1-repos-hub4j-test-org-temp-testHandler_Wait-2",
+  "insertionIndex": 2
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/3-r_h_t_Wait.json b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/3-r_h_t_Wait.json
new file mode 100644
index 000000000..23e990450
--- /dev/null
+++ b/src/test/resources/org/kohsuke/github/RateLimitHandlerTest/wiremock/testHandler_Wait_Missing_Date_Header/mappings/3-r_h_t_Wait.json
@@ -0,0 +1,49 @@
+{
+  "id": "574da117-6845-46d8-b2c1-4415546ca670",
+  "name": "repos_hub4j-test-org_temp-testHandler_Wait_Missing_Date_Header",
+  "request": {
+    "url": "/repos/hub4j-test-org/temp-testHandler_Wait_Missing_Date_Header",
+    "method": "GET",
+    "headers": {
+      "Accept": {
+        "equalTo": "application/vnd.github+json"
+      }
+    }
+  },
+  "response": {
+    "status": 200,
+    "bodyFileName": "3-r_h_t_Wait.json",
+    "headers": {
+      "Content-Type": "application/json; charset=utf-8",
+      "Server": "GitHub.com",
+      "Status": "200 OK",
+      "X-RateLimit-Limit": "5000",
+      "X-RateLimit-Remaining": "4922",
+      "X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
+      "Cache-Control": "private, max-age=60, s-maxage=60",
+      "Vary": [
+        "Accept, Authorization, Cookie, X-GitHub-OTP",
+        "Accept-Encoding"
+      ],
+      "ETag": "W/\"858224998ac7d1fd6dcd43f73d375297\"",
+      "Last-Modified": "Thu, 06 Feb 2020 18:33:43 GMT",
+      "X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
+      "X-Accepted-OAuth-Scopes": "repo",
+      "X-GitHub-Media-Type": "unknown, github.v3",
+      "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
+      "Access-Control-Allow-Origin": "*",
+      "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
+      "X-Frame-Options": "deny",
+      "X-Content-Type-Options": "nosniff",
+      "X-XSS-Protection": "1; mode=block",
+      "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
+      "Content-Security-Policy": "default-src 'none'",
+      "X-GitHub-Request-Id": "CC37:2605:3FADC:4EA8C:5E3C5C02"
+    }
+  },
+  "uuid": "574da117-6845-46d8-b2c1-4415546ca670",
+  "persistent": true,
+  "scenarioName": "scenario-1-repos-hub4j-test-org-temp-testHandler_Wait",
+  "requiredScenarioState": "scenario-1-repos-hub4j-test-org-temp-testHandler_Wait-2",
+  "insertionIndex": 3
+}
\ No newline at end of file
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/user-1.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/user-1.json
index e7cd34e01..def774574 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/user-1.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/user-1.json
@@ -15,7 +15,7 @@
     "bodyFileName": "user-1.json",
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Tue, 04 Jul 2023 09:27:51 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "private, max-age=60, s-maxage=60",
       "Vary": [
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-2.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-2.json
index 5897845e0..1a30ed08e 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-2.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-2.json
@@ -20,7 +20,7 @@
     "status": 403,
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Tue, 04 Jul 2023 09:27:52 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "private, max-age=60, s-maxage=60",
       "Vary": [
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-3.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-3.json
index a9ea666bb..8a9c9ed26 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-3.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-3.json
@@ -21,7 +21,7 @@
     "bodyFileName": "users_kohsuke-2.json",
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Tue, 04 Jul 2023 09:27:52 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "private, max-age=60, s-maxage=60",
       "Vary": [
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-4.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-4.json
index 6935c06c3..849c1c730 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-4.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNewWhenOldOneExpires/mappings/users_kohsuke-4.json
@@ -20,7 +20,7 @@
     "bodyFileName": "users_kohsuke-2.json",
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Tue, 04 Jul 2023 09:27:52 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "private, max-age=60, s-maxage=60",
       "Vary": [
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-1.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-1.json
index d000da49a..15e38b4c5 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-1.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-1.json
@@ -20,7 +20,7 @@
     "status": 403,
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Thu, 10 Aug 2023 09:12:37 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "public, max-age=60, s-maxage=60",
       "Vary": [
diff --git a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-2.json b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-2.json
index 87b1110b9..25de34163 100644
--- a/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-2.json
+++ b/src/test/resources/org/kohsuke/github/extras/authorization/AuthorizationTokenRefreshTest/wiremock/testNotNewWhenOldOneIsStillValid/mappings/users_kohsuke-2.json
@@ -19,7 +19,7 @@
     "bodyFileName": "users_kohsuke-1.json",
     "headers": {
       "Server": "GitHub.com",
-      "Date": "Thu, 10 Aug 2023 09:12:37 GMT",
+      "Date": "{{testStartDate timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
       "Content-Type": "application/json; charset=utf-8",
       "Cache-Control": "public, max-age=60, s-maxage=60",
       "Vary": [
@@ -32,7 +32,7 @@
       "x-github-api-version-selected": "2022-11-28",
       "X-RateLimit-Limit": "60",
       "X-RateLimit-Remaining": "59",
-      "X-RateLimit-Reset": "{{testStartDate offset='1 hours' format='unix'}}",
+      "X-RateLimit-Reset": "{{testStartDate offset='5 seconds' format='unix'}}",
       "X-RateLimit-Used": "1",
       "X-RateLimit-Resource": "core",
       "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",