Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad58f02

Browse filesBrowse files
committed
Add tests
1 parent bd9ae7d commit ad58f02
Copy full SHA for ad58f02

File tree

Expand file treeCollapse file tree

2 files changed

+41
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-6
lines changed

‎src/main/java/org/kohsuke/github/GitHubRequest.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubRequest.java
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,23 @@ public static Builder<?> newBuilder() {
9292
@Nonnull
9393
static URL getApiURL(String apiUrl, String tailApiUrl) {
9494
try {
95+
String urlString = "";
9596
if (tailApiUrl.startsWith("/")) {
96-
if ("github.com".equals(apiUrl)) {// backward compatibility
97-
return new URL(GitHubClient.GITHUB_URL + tailApiUrl);
97+
if ("github.com".equals(apiUrl)) {
98+
// backward compatibility
99+
urlString = GitHubClient.GITHUB_URL;
98100
} else {
99-
return new URL(apiUrl + tailApiUrl);
101+
urlString = apiUrl;
100102
}
101-
} else {
102-
return new URL(tailApiUrl);
103103
}
104+
urlString += tailApiUrl;
105+
return new URL(urlString);
104106
} catch (MalformedURLException e) {
105107
// The data going into constructing this URL should be controlled by the GitHub API framework,
106108
// so a malformed URL here is a framework runtime error.
107109
// All callers of this method ended up wrapping and throwing GHException,
108110
// indicating the functionality should be moved to the common code path.
109-
throw new GHException("Malformed URL ", e);
111+
throw new GHException("Malformed URL", e);
110112
}
111113
}
112114

‎src/test/java/org/kohsuke/github/GitHubStaticTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GitHubStaticTest.java
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.kohsuke.github;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
45

6+
import java.net.MalformedURLException;
57
import java.net.URL;
68
import java.text.SimpleDateFormat;
79
import java.time.Duration;
@@ -344,6 +346,37 @@ public void testMappingReaderWriter() throws Exception {
344346

345347
}
346348

349+
@Test
350+
public void testGitHubRequest_getApiURL() throws Exception {
351+
assertThat(GitHubRequest.getApiURL("github.com", "/endpoint").toString(),
352+
equalTo("https://api.github.com/endpoint"));
353+
354+
// This URL is completely invalid but doesn't throw
355+
assertThat(GitHubRequest.getApiURL("github.com", "//endpoint&?").toString(),
356+
equalTo("https://api.github.com//endpoint&?"));
357+
358+
assertThat(GitHubRequest.getApiURL("ftp://whoa.github.com", "/endpoint").toString(),
359+
equalTo("ftp://whoa.github.com/endpoint"));
360+
assertThat(GitHubRequest.getApiURL(null, "ftp://api.test.github.com/endpoint").toString(),
361+
equalTo("ftp://api.test.github.com/endpoint"));
362+
363+
GHException e;
364+
e = Assert.assertThrows(GHException.class,
365+
() -> GitHubRequest.getApiURL("gopher://whoa.github.com", "/endpoint"));
366+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
367+
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
368+
369+
e = Assert.assertThrows(GHException.class, () -> GitHubRequest.getApiURL("bogus", "/endpoint"));
370+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
371+
assertThat(e.getCause().getMessage(), equalTo("no protocol: bogus/endpoint"));
372+
373+
e = Assert.assertThrows(GHException.class,
374+
() -> GitHubRequest.getApiURL(null, "gopher://api.test.github.com/endpoint"));
375+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
376+
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
377+
378+
}
379+
347380
static String formatDate(Date dt, String format) {
348381
return formatZonedDate(dt, format, "GMT");
349382
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.