Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/src/main/java/org/asynchttpclient/uri/Uri.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public static Uri create(Uri context, final String originalUrl) {
UriParser parser = new UriParser();
parser.parse(context, originalUrl);

if (null == parser.scheme || null == parser.host) {
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.", parser.scheme, parser.host));
}

return new Uri(parser.scheme,//
parser.userInfo,//
parser.host,//
Expand Down
4 changes: 2 additions & 2 deletions client/src/test/java/org/asynchttpclient/BasicHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public Response onCompleted(Response response) throws Exception {
});
}

@Test(expectedExceptions = NullPointerException.class)
@Test(expectedExceptions = IllegalArgumentException.class)
public void nullSchemeThrowsNPE() throws Throwable {
withClient().run(client -> client.prepareGet("gatling.io").execute());
}
Expand Down Expand Up @@ -859,7 +859,7 @@ public void getShouldAllowBody() throws Throwable {
});
}

@Test(expectedExceptions = NullPointerException.class)
@Test(expectedExceptions = IllegalArgumentException.class)
public void malformedUriThrowsException() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
Expand Down
29 changes: 29 additions & 0 deletions client/src/test/java/org/asynchttpclient/uri/UriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,33 @@ public void testIsWebsocket() {
uri = Uri.create(url);
assertTrue(uri.isWebSocket(), "isWebSocket should return true for wss url");
}

@Test
public void testCreateWithInvalidUrl_throwsIllegalArgumentException() {
// a valid URL would contain the scheme/protocol
String invalidUrl = "localhost";

Throwable exception = null;
try {
// run
Uri.create(invalidUrl);
} catch (IllegalArgumentException ex) {
exception = ex;
}

// verify
assertNotNull(exception);
assertEquals("The UriParser could not extract all required values: scheme=null, host=null. Please make "
+ "sure you provide a valid URL.", exception.getMessage());
}

@Test
public void testCreateWithValidUrl_doesNotThrowException() {
String validUrl = "https://localhost";
try {
Uri.create(validUrl);
} catch (IllegalArgumentException ex) {
fail(ex.getMessage());
}
}
}