Skip to content

Commit

Permalink
Merge pull request #741 from tulikabhatt/fix_empty_url
Browse files Browse the repository at this point in the history
Filter out empty or null string while parsing URLs.
Fixes #664
  • Loading branch information
rgallardo-netflix authored Jan 17, 2025
2 parents 7ed4e17 + dfcc4c7 commit 56adbe4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -56,17 +57,18 @@ private static URL[] createUrls(String... urlStrings) {
if (urlStrings == null || urlStrings.length == 0) {
throw new IllegalArgumentException("urlStrings is null or empty");
}
URL[] urls = new URL[urlStrings.length];
try {
for (int i = 0; i < urls.length; i++) {
urls[i] = new URL(urlStrings[i]);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
return urls;
return Arrays.stream(urlStrings)
.filter(s -> s != null && !s.isEmpty())
.map(url -> {
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.toArray(URL[]::new);
}

@Override
public PollingResponse call() throws IOException {
final Map<String, String> map = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ public void testStringConstructorCommonCase() {
assertEquals(url2, reader2.getConfigUrls().get(1).toString());
}

@Test
public void testStringConstructorEmptyString() {
final String url1 = "http://hello:8080/hey";
final String url2 = "";
final String url3 = null;
URLConfigReader reader1 = new URLConfigReader(url1);
assertEquals(url1, reader1.getConfigUrls().get(0).toString());

URLConfigReader reader2 = new URLConfigReader(url1, url2);
assertEquals(url1, reader2.getConfigUrls().get(0).toString());
assertEquals(1, reader2.getConfigUrls().size());

URLConfigReader reader3 = new URLConfigReader(url1, url2, url3);
assertEquals(url1, reader3.getConfigUrls().get(0).toString());
assertEquals(1, reader3.getConfigUrls().size());
}

@Test
public void testStringConstructorMalformedUrl() {
assertThrows(RuntimeException.class, () -> new URLConfigReader("bad url"));
Expand Down

0 comments on commit 56adbe4

Please sign in to comment.