Skip to content

Commit 328ed7f

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

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
@@ -238,7 +238,7 @@ public Response onCompleted(Response response) throws Exception {
238238
});
239239
}
240240

241-
@Test(expectedExceptions = NullPointerException.class)
241+
@Test(expectedExceptions = IllegalArgumentException.class)
242242
public void nullSchemeThrowsNPE() throws Throwable {
243243
withClient().run(client -> client.prepareGet("gatling.io").execute());
244244
}
@@ -854,7 +854,7 @@ public void getShouldAllowBody() throws Throwable {
854854
});
855855
}
856856

857-
@Test(expectedExceptions = NullPointerException.class)
857+
@Test(expectedExceptions = IllegalArgumentException.class)
858858
public void malformedUriThrowsException() throws Throwable {
859859
withClient().run(client -> {
860860
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)