diff --git a/src/main/java/org/kohsuke/github/GitHubBuilder.java b/src/main/java/org/kohsuke/github/GitHubBuilder.java index 65822741b..1b0034420 100644 --- a/src/main/java/org/kohsuke/github/GitHubBuilder.java +++ b/src/main/java/org/kohsuke/github/GitHubBuilder.java @@ -93,6 +93,7 @@ public class GitHubBuilder implements Cloneable { * @deprecated Use {@link #fromEnvironment()} to pick up standard set of environment variables, so that different * clients of this library will all recognize one consistent set of coordinates. */ + @Deprecated public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName) throws IOException { @@ -122,6 +123,7 @@ public class GitHubBuilder implements Cloneable { * @deprecated Use {@link #fromEnvironment()} to pick up standard set of environment variables, so that different * clients of this library will all recognize one consistent set of coordinates. */ + @Deprecated public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName, @@ -396,11 +398,7 @@ public class GitHubBuilder implements Cloneable { * @return the git hub builder */ public GitHubBuilder withProxy(final Proxy p) { - return withConnector(new ImpatientHttpConnector(new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - return (HttpURLConnection) url.openConnection(p); - } - })); + return withConnector(new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection(p))); } /** diff --git a/src/main/java/org/kohsuke/github/HttpConnector.java b/src/main/java/org/kohsuke/github/HttpConnector.java index 098930fa2..d5e900f5b 100644 --- a/src/main/java/org/kohsuke/github/HttpConnector.java +++ b/src/main/java/org/kohsuke/github/HttpConnector.java @@ -12,6 +12,7 @@ import java.net.URL; *
* For example, you can implement this to st custom timeouts. */ +@FunctionalInterface public interface HttpConnector { /** * Opens a connection to the given URL. @@ -27,18 +28,12 @@ public interface HttpConnector { /** * Default implementation that uses {@link URL#openConnection()}. */ - HttpConnector DEFAULT = new ImpatientHttpConnector(new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - return (HttpURLConnection) url.openConnection(); - } - }); + HttpConnector DEFAULT = new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection()); /** * Stub implementation that is always off-line. */ - HttpConnector OFFLINE = new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - throw new IOException("Offline"); - } + HttpConnector OFFLINE = url -> { + throw new IOException("Offline"); }; } diff --git a/src/test/java/org/kohsuke/github/GitHubConnectionTest.java b/src/test/java/org/kohsuke/github/GitHubConnectionTest.java index 63856f667..882cc3f4b 100644 --- a/src/test/java/org/kohsuke/github/GitHubConnectionTest.java +++ b/src/test/java/org/kohsuke/github/GitHubConnectionTest.java @@ -1,6 +1,5 @@ package org.kohsuke.github; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -106,11 +105,13 @@ public class GitHubConnectionTest extends AbstractGitHubWireMockTest { assertEquals("", github.getClient().login); } - @Ignore @Test public void testGitHubIsApiUrlValid() throws IOException { - GitHub hub = GitHub.connectAnonymously(); - // GitHub hub = GitHub.connectToEnterpriseAnonymously(mockGitHub.apiServer().baseUrl()); + // NOTE: We cannot test connectAnonymously on a general basis because it can hang if + // rate limit is reached. We connectToEnterpriseAnonymously as the nearest equivalent. + // GitHub hub = GitHub.connectAnonymously(); + + GitHub hub = GitHub.connectToEnterpriseAnonymously(mockGitHub.apiServer().baseUrl()); try { hub.checkApiUrlValidity(); } catch (IOException ioe) { diff --git a/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json new file mode 100644 index 000000000..d1fb32fb1 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json @@ -0,0 +1,34 @@ +{ + "current_user_url": "https://api.github.com/user", + "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}", + "authorizations_url": "https://api.github.com/authorizations", + "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}", + "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}", + "emails_url": "https://api.github.com/user/emails", + "emojis_url": "https://api.github.com/emojis", + "events_url": "https://api.github.com/events", + "feeds_url": "https://api.github.com/feeds", + "followers_url": "https://api.github.com/user/followers", + "following_url": "https://api.github.com/user/following{/target}", + "gists_url": "https://api.github.com/gists{/gist_id}", + "hub_url": "https://api.github.com/hub", + "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}", + "issues_url": "https://api.github.com/issues", + "keys_url": "https://api.github.com/user/keys", + "label_search_url": "https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}", + "notifications_url": "https://api.github.com/notifications", + "organization_url": "https://api.github.com/orgs/{org}", + "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}", + "organization_teams_url": "https://api.github.com/orgs/{org}/teams", + "public_gists_url": "https://api.github.com/gists/public", + "rate_limit_url": "https://api.github.com/rate_limit", + "repository_url": "https://api.github.com/repos/{owner}/{repo}", + "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}", + "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}", + "starred_url": "https://api.github.com/user/starred{/owner}{/repo}", + "starred_gists_url": "https://api.github.com/gists/starred", + "user_url": "https://api.github.com/users/{user}", + "user_organizations_url": "https://api.github.com/user/orgs", + "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}", + "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}" +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json new file mode 100644 index 000000000..19a8e7bfc --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json @@ -0,0 +1,45 @@ +{ + "id": "2ce6df61-378a-43bd-9d37-d0851f8699bf", + "name": "", + "request": { + "url": "/", + "method": "GET", + "headers": { + "Accept": { + "equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" + } + } + }, + "response": { + "status": 200, + "bodyFileName": "-2ce6df61-378a-43bd-9d37-d0851f8699bf.json", + "headers": { + "Date": "Wed, 26 Feb 2020 03:55:33 GMT", + "Content-Type": "application/json; charset=utf-8", + "Server": "GitHub.com", + "Status": "200 OK", + "X-RateLimit-Limit": "60", + "X-RateLimit-Remaining": "58", + "X-RateLimit-Reset": "1582692906", + "Cache-Control": "public, max-age=60, s-maxage=60", + "Vary": [ + "Accept", + "Accept-Encoding, Accept, X-Requested-With" + ], + "ETag": "W/\"323592c0d9feaab7a8e4a4e2a555feec\"", + "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": "EE6F:3AF2:5C10D:6DDAD:5E55EC35" + } + }, + "uuid": "2ce6df61-378a-43bd-9d37-d0851f8699bf", + "persistent": true, + "insertionIndex": 1 +} \ No newline at end of file