Skip to content

Commit

Permalink
Merge pull request #232
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Dec 1, 2015
2 parents 0397d7a + 0f45d03 commit 402adc3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ public static GitHub connectAnonymously() throws IOException {
return new GitHubBuilder().build();
}

/**
* Connects to GitHub Enterprise anonymously.
*
* All operations that requires authentication will fail.
*/
public static GitHub connectToEnterpriseAnonymously(String apiUrl) throws IOException {
return new GitHubBuilder().withEndpoint(apiUrl).build();
}

/**
* Is this an anonymous connection
* @return {@code true} if operations that require authentication will fail.
Expand Down Expand Up @@ -442,6 +451,29 @@ public boolean isCredentialValid() throws IOException {
}
}

private static class GHApiInfo {
private String rate_limit_url;

void check(String apiUrl) throws IOException {
if (rate_limit_url==null)
throw new IOException(apiUrl+" doesn't look like GitHub API URL");

// make sure that the URL is legitimate
new URL(rate_limit_url);
}
}

/**
* Ensures that the API URL is valid.
*
* <p>
* This method returns normally if the endpoint is reachable and verified to be GitHub API URL.
* Otherwise this method throws {@link IOException} to indicate the problem.
*/
public void checkApiUrlValidity() throws IOException {
retrieve().to("/", GHApiInfo.class).check(apiUrl);
}

/**
* Search issues.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -119,4 +120,10 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException {
GHRateLimit rateLimit = github.getRateLimit();
assertThat(rateLimit.getResetDate(), notNullValue());
}

@Test
public void testGitHubIsApiUrlValid() throws IOException {
GitHub github = GitHub.connectAnonymously();
github.checkApiUrlValidity();
}
}

0 comments on commit 402adc3

Please sign in to comment.