From d4bd3ca0970b1687ff4f3cf781595f0cfa9d456b Mon Sep 17 00:00:00 2001 From: Michael Bahr Date: Mon, 31 Jul 2017 10:36:11 +0200 Subject: [PATCH 1/5] Clarified assertNotNull messages --- client/src/main/java/org/asynchttpclient/uri/Uri.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index fd6ee309b..b8f3c9612 100644 --- a/client/src/main/java/org/asynchttpclient/uri/Uri.java +++ b/client/src/main/java/org/asynchttpclient/uri/Uri.java @@ -61,9 +61,9 @@ public Uri(String scheme,// String path,// String query) { - this.scheme = assertNotNull(scheme, "scheme"); + this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL."); this.userInfo = userInfo; - this.host = assertNotNull(host, "host"); + this.host = assertNotNull(host, "The host could not be resolved. Please provide a valid host."); this.port = port; this.path = path; this.query = query; From dc357e8227abb919740a70efdda3d0340266a258 Mon Sep 17 00:00:00 2001 From: Michael Bahr Date: Mon, 31 Jul 2017 10:38:14 +0200 Subject: [PATCH 2/5] Update Uri.java --- client/src/main/java/org/asynchttpclient/uri/Uri.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index b8f3c9612..2ca4cd1e6 100644 --- a/client/src/main/java/org/asynchttpclient/uri/Uri.java +++ b/client/src/main/java/org/asynchttpclient/uri/Uri.java @@ -61,7 +61,7 @@ public Uri(String scheme,// String path,// String query) { - this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL."); + this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL including the protocol."); this.userInfo = userInfo; this.host = assertNotNull(host, "The host could not be resolved. Please provide a valid host."); this.port = port; From 16510de9812705b20bd8f40fdb8e2999d38a32f5 Mon Sep 17 00:00:00 2001 From: bahrmichael Date: Mon, 31 Jul 2017 14:15:43 +0200 Subject: [PATCH 3/5] Revert "Update Uri.java" This reverts commit dc357e8 --- client/src/main/java/org/asynchttpclient/uri/Uri.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index 2ca4cd1e6..b8f3c9612 100644 --- a/client/src/main/java/org/asynchttpclient/uri/Uri.java +++ b/client/src/main/java/org/asynchttpclient/uri/Uri.java @@ -61,7 +61,7 @@ public Uri(String scheme,// String path,// String query) { - this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL including the protocol."); + this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL."); this.userInfo = userInfo; this.host = assertNotNull(host, "The host could not be resolved. Please provide a valid host."); this.port = port; From 38aad68d9dfb0768d6a0d8e3389a8f064cd29c30 Mon Sep 17 00:00:00 2001 From: bahrmichael Date: Mon, 31 Jul 2017 14:16:00 +0200 Subject: [PATCH 4/5] Revert "Clarified assertNotNull messages" This reverts commit d4bd3ca --- client/src/main/java/org/asynchttpclient/uri/Uri.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index b8f3c9612..fd6ee309b 100644 --- a/client/src/main/java/org/asynchttpclient/uri/Uri.java +++ b/client/src/main/java/org/asynchttpclient/uri/Uri.java @@ -61,9 +61,9 @@ public Uri(String scheme,// String path,// String query) { - this.scheme = assertNotNull(scheme, "The scheme could not be resolved. Please provide a valid URL."); + this.scheme = assertNotNull(scheme, "scheme"); this.userInfo = userInfo; - this.host = assertNotNull(host, "The host could not be resolved. Please provide a valid host."); + this.host = assertNotNull(host, "host"); this.port = port; this.path = path; this.query = query; From 99af627f1467feb0a6c956a6653e10711ba7423b Mon Sep 17 00:00:00 2001 From: bahrmichael Date: Mon, 31 Jul 2017 14:44:17 +0200 Subject: [PATCH 5/5] Throwing IllegalArgumentException if the parser couldn't extract host/scheme. --- .../java/org/asynchttpclient/uri/Uri.java | 6 ++++ .../org/asynchttpclient/BasicHttpTest.java | 4 +-- .../java/org/asynchttpclient/uri/UriTest.java | 29 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/uri/Uri.java b/client/src/main/java/org/asynchttpclient/uri/Uri.java index fd6ee309b..0439a441b 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 2291e7a99..dee17f46a 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 7efffb50f..61c9487df 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()); + } + } }