Skip to content

Commit b8aedbe

Browse files
Michael Bahrslandelle
Michael Bahr
authored andcommitted
Improved error messages if an invalid URL was passed (AsyncHttpClient#1442)
1 parent 1d8a630 commit b8aedbe

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

client/src/main/java/org/asynchttpclient/uri/Uri.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static Uri create(Uri context, final String originalUrl) {
3636
UriParser parser = new UriParser();
3737
parser.parse(context, originalUrl);
3838

39+
if (parser.scheme == null || parser.host == null) {
40+
throw new IllegalArgumentException(String.format("The UriParser could not extract all required values: scheme=%s, host=%s. Please make sure you provide a valid URL.",
41+
parser.scheme, parser.host));
42+
}
43+
3944
return new Uri(parser.scheme,//
4045
parser.userInfo,//
4146
parser.host,//

client/src/test/java/org/asynchttpclient/BasicHttpTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public Response onCompleted(Response response) throws Exception {
240240
});
241241
}
242242

243-
@Test(expectedExceptions = NullPointerException.class)
243+
@Test(expectedExceptions = IllegalArgumentException.class)
244244
public void nullSchemeThrowsNPE() throws Throwable {
245245
withClient().run(client -> client.prepareGet("gatling.io").execute());
246246
}
@@ -859,7 +859,7 @@ public void getShouldAllowBody() throws Throwable {
859859
});
860860
}
861861

862-
@Test(expectedExceptions = NullPointerException.class)
862+
@Test(expectedExceptions = IllegalArgumentException.class)
863863
public void malformedUriThrowsException() throws Throwable {
864864
withClient().run(client -> {
865865
withServer(server).run(server -> {

client/src/test/java/org/asynchttpclient/uri/UriTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,33 @@ public void testIsWebsocket() {
358358
uri = Uri.create(url);
359359
assertTrue(uri.isWebSocket(), "isWebSocket should return true for wss url");
360360
}
361+
362+
@Test
363+
public void testCreateWithInvalidUrl_throwsIllegalArgumentException() {
364+
// a valid URL would contain the scheme/protocol
365+
String invalidUrl = "localhost";
366+
367+
Throwable exception = null;
368+
try {
369+
// run
370+
Uri.create(invalidUrl);
371+
} catch (IllegalArgumentException ex) {
372+
exception = ex;
373+
}
374+
375+
// verify
376+
assertNotNull(exception);
377+
assertEquals("The UriParser could not extract all required values: scheme=null, host=null. Please make "
378+
+ "sure you provide a valid URL.", exception.getMessage());
379+
}
380+
381+
@Test
382+
public void testCreateWithValidUrl_doesNotThrowException() {
383+
String validUrl = "https://localhost";
384+
try {
385+
Uri.create(validUrl);
386+
} catch (IllegalArgumentException ex) {
387+
fail(ex.getMessage());
388+
}
389+
}
361390
}

0 commit comments

Comments
 (0)