diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index fd6ee309b6..0439a441b7 100644 --- a/client/src/main/java/org/asynchttpclient/uri/Uri.java +++ b/client/src/main/java/org/asynchttpclient/uri/Uri.java @@ -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,// diff --git a/client/src/test/java/org/asynchttpclient/BasicHttpTest.java b/client/src/test/java/org/asynchttpclient/BasicHttpTest.java index 2291e7a990..dee17f46af 100755 --- a/client/src/test/java/org/asynchttpclient/BasicHttpTest.java +++ b/client/src/test/java/org/asynchttpclient/BasicHttpTest.java @@ -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()); } @@ -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 -> { diff --git a/client/src/test/java/org/asynchttpclient/uri/UriTest.java b/client/src/test/java/org/asynchttpclient/uri/UriTest.java index 7efffb50ff..61c9487dff 100644 --- a/client/src/test/java/org/asynchttpclient/uri/UriTest.java +++ b/client/src/test/java/org/asynchttpclient/uri/UriTest.java @@ -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()); + } + } }