Skip to content

Commit

Permalink
Add tests for server time ahead and behind local time
Browse files Browse the repository at this point in the history
Sometimes adding tests finds broken product code.  Sometimes it finds broken test environment.
This was the latter.

The wiremock templating returns dates in UTC format not RFC-1123 format.
  • Loading branch information
bitwiseman committed Nov 7, 2019
1 parent bb3a492 commit 37de3a4
Show file tree
Hide file tree
Showing 29 changed files with 627 additions and 16 deletions.
23 changes: 20 additions & 3 deletions src/test/java/org/kohsuke/github/GHRateLimitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,26 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
Thread.sleep(1000);
}


// These three tests should be have the same, showing server time adjustment working
@Test
public void testGitHubRateLimitExpiration() throws Exception {
executeExpirationTest();
}

@Test
public void testGitHubRateLimitExpirationServerFiveMinutesAhead() throws Exception {
executeExpirationTest();
}

@Test
public void testGitHubRateLimitExpirationServerFiveMinutesBehind() throws Exception {
executeExpirationTest();
}

private void executeExpirationTest() throws Exception {
// Customized response that templates the date to keep things working
//snapshotNotAllowed();
snapshotNotAllowed();

assertThat(mockGitHub.getRequestCount(), equalTo(0));
GHRateLimit rateLimit = null;
Expand Down Expand Up @@ -348,6 +364,9 @@ public void testGitHubRateLimitExpiration() throws Exception {
// This time, rateLimit() should find an expired record and get a new one.
Thread.sleep(3000);

assertThat("Header instance has expired",
gitHub.lastRateLimit().isExpired(), is(true));

assertThat("rateLimit() will ask server when header instance expires and it has not called getRateLimit() yet",
gitHub.rateLimit(), not(sameInstance(rateLimit)));

Expand Down Expand Up @@ -400,8 +419,6 @@ public void testGitHubRateLimitExpiration() throws Exception {
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}



private static GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("github-api-test-org").getRepository("github-api");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 404,
"body": "{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3\"}",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "404 Not Found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "user-a0bafdae-2f0d-4d0b-966b-f2408c1240bd.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "user-a0bafdae-2f0d-4d0b-966b-f2408c1240bd.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "orgs_github-api-test-org-7ea37b72-8b01-4ed5-a63d-98d32b2faa0f.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-f22b1cea-3679-481d-8b95-459b2c47bf98.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-a1f82a96-500c-4462-ae7b-e0159afa8208.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-4e2fc33b-fb25-4dfc-9d56-34f9b4d707be.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "user-a460fd69-99f3-46c7-aeb1-888c34085d4a.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-f22b1cea-3679-481d-8b95-459b2c47bf98.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-a1f82a96-500c-4462-ae7b-e0159afa8208.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"status": 200,
"bodyFileName": "rate_limit-4e2fc33b-fb25-4dfc-9d56-34f9b4d707be.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"status": 200,
"bodyFileName": "user-a460fd69-99f3-46c7-aeb1-888c34085d4a.json",
"headers": {
"Date": "{{now}}",
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4897",
"X-RateLimit-Reset": "{{now offset='2 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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"resources": {
"core": {
"limit": 5000,
"remaining": 4896,
"reset": 1570478899
},
"search": {
"limit": 30,
"remaining": 30,
"reset": 1570478180
},
"graphql": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
},
"integration_manifest": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
}
},
"rate": {
"limit": 5000,
"remaining": 4896,
"reset": {{now offset='305 seconds' format='unix'}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"resources": {
"core": {
"limit": 5000,
"remaining": 4897,
"reset": 1570478899
},
"search": {
"limit": 30,
"remaining": 30,
"reset": 1570478180
},
"graphql": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
},
"integration_manifest": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
}
},
"rate": {
"limit": 5000,
"remaining": 4897,
"reset": {{now offset='305 seconds' format='unix'}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"resources": {
"core": {
"limit": 5000,
"remaining": 4897,
"reset": 1570478899
},
"search": {
"limit": 30,
"remaining": 30,
"reset": 1570478180
},
"graphql": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
},
"integration_manifest": {
"limit": 5000,
"remaining": 5000,
"reset": 1570481642
}
},
"rate": {
"limit": 5000,
"remaining": 4897,
"reset": {{now offset='305 seconds' format='unix'}}
}
}
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 168,
"public_gists": 4,
"followers": 136,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2019-09-24T19:32:29Z",
"private_gists": 7,
"total_private_repos": 9,
"owned_private_repos": 0,
"disk_usage": 33697,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"id": "f22b1cea-3679-481d-8b95-459b2c47bf98",
"name": "rate_limit",
"request": {
"url": "/rate_limit",
"method": "GET"
},
"response": {
"status": 200,
"bodyFileName": "rate_limit-f22b1cea-3679-481d-8b95-459b2c47bf98.json",
"headers": {
"Date": "{{now offset='5 minutes' timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4897",
"X-RateLimit-Reset": "{{now offset='303 seconds' format='unix'}}",
"Cache-Control": "no-cache",
"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'",
"Vary": "Accept-Encoding",
"X-GitHub-Request-Id": "E39F:3620:2D9D346:369B91A:5D9B9848"
}
},
"uuid": "f22b1cea-3679-481d-8b95-459b2c47bf98",
"persistent": true,
"scenarioName": "scenario-1-rate_limit",
"requiredScenarioState": "Started",
"newScenarioState": "scenario-1-rate_limit-2",
"insertionIndex": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"id": "a1f82a96-500c-4462-ae7b-e0159afa8208",
"name": "rate_limit",
"request": {
"url": "/rate_limit",
"method": "GET"
},
"response": {
"status": 200,
"bodyFileName": "rate_limit-a1f82a96-500c-4462-ae7b-e0159afa8208.json",
"headers": {
"Date": "{{now offset='5 minutes' timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4897",
"X-RateLimit-Reset": "{{now offset='303 seconds' format='unix'}}",
"Cache-Control": "no-cache",
"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'",
"Vary": "Accept-Encoding",
"X-GitHub-Request-Id": "E39F:3620:2D9D3B3:369BA1D:5D9B984A"
}
},
"uuid": "a1f82a96-500c-4462-ae7b-e0159afa8208",
"persistent": true,
"scenarioName": "scenario-1-rate_limit",
"requiredScenarioState": "scenario-1-rate_limit-2",
"newScenarioState": "scenario-1-rate_limit-3",
"insertionIndex": 3
}
Loading

0 comments on commit 37de3a4

Please sign in to comment.