diff --git a/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java b/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java index 83f702b1..186f2f19 100644 --- a/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java +++ b/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java @@ -857,11 +857,24 @@ public static HttpRequest get(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{get(URI)} */ + @Deprecated public static HttpRequest get(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_GET); } + /** + * Start a 'GET' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest get(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_GET); + } + /** * Start a 'GET' request to the given URL along with the query params * @@ -921,11 +934,24 @@ public static HttpRequest post(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{post(URI)} instead */ + @Deprecated public static HttpRequest post(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_POST); } + /** + * Start a 'POST' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest post(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_POST); + } + /** * Start a 'POST' request to the given URL along with the query params * @@ -985,11 +1011,24 @@ public static HttpRequest put(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{put(URI)} instead */ + @Deprecated public static HttpRequest put(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_PUT); } + /** + * Start a 'PUT' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest put(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_PUT); + } + /** * Start a 'PUT' request to the given URL along with the query params * @@ -1049,11 +1088,24 @@ public static HttpRequest delete(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{delete(URI)} instead */ + @Deprecated public static HttpRequest delete(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_DELETE); } + /** + * Start a 'DELETE' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest delete(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_DELETE); + } + /** * Start a 'DELETE' request to the given URL along with the query params * @@ -1113,11 +1165,24 @@ public static HttpRequest head(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{delete(URI)} instead */ + @Deprecated public static HttpRequest head(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_HEAD); } + /** + * Start a 'HEAD' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest head(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_HEAD); + } + /** * Start a 'HEAD' request to the given URL along with the query params * @@ -1177,11 +1242,24 @@ public static HttpRequest options(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{options(URI)} instead */ + @Deprecated public static HttpRequest options(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_OPTIONS); } + /** + * Start an 'OPTIONS' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest options(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_OPTIONS); + } + /** * Start a 'TRACE' request to the given URL * @@ -1200,11 +1278,24 @@ public static HttpRequest trace(final CharSequence url) * @param url * @return request * @throws HttpRequestException + * @deprecated use @link{trace(URI)} instead */ + @Deprecated public static HttpRequest trace(final URL url) throws HttpRequestException { return new HttpRequest(url, METHOD_TRACE); } + /** + * Start a 'TRACE' request to the given URL + * + * @param uri + * @return request + * @throws HttpRequestException + */ + public static HttpRequest trace(final URI uri) throws HttpRequestException { + return new HttpRequest(uri, METHOD_TRACE); + } + /** * Set the 'http.keepAlive' property to the given value. *

@@ -1329,7 +1420,9 @@ public HttpRequest(final CharSequence url, final String method) * @param url * @param method * @throws HttpRequestException + * @deprecated use {@link new(URI , String )} instead. */ + @Deprecated public HttpRequest(final URL url, final String method) throws HttpRequestException { try { @@ -1340,6 +1433,23 @@ public HttpRequest(final URL url, final String method) } } + /** + * Create HTTP connection wrapper + * + * @param uri + * @param method + * @throws HttpRequestException + */ + public HttpRequest(final URI uri, final String method) + throws HttpRequestException { + try { + connection = (HttpURLConnection) uri.toURL().openConnection(); + connection.setRequestMethod(method); + } catch (IOException e) { + throw new HttpRequestException(e); + } + } + @Override public String toString() { return connection.getRequestMethod() + ' ' + connection.getURL(); diff --git a/lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java b/lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java index 0caa28a2..1a4e69e6 100644 --- a/lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java +++ b/lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java @@ -21,58 +21,20 @@ */ package com.github.kevinsawicki.http; -import static com.github.kevinsawicki.http.HttpRequest.CHARSET_UTF8; -import static com.github.kevinsawicki.http.HttpRequest.delete; -import static com.github.kevinsawicki.http.HttpRequest.encode; -import static com.github.kevinsawicki.http.HttpRequest.get; -import static com.github.kevinsawicki.http.HttpRequest.head; -import static com.github.kevinsawicki.http.HttpRequest.options; -import static com.github.kevinsawicki.http.HttpRequest.post; -import static com.github.kevinsawicki.http.HttpRequest.put; -import static com.github.kevinsawicki.http.HttpRequest.trace; -import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; -import static java.net.HttpURLConnection.HTTP_CREATED; -import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; -import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED; -import static java.net.HttpURLConnection.HTTP_OK; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintStream; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.net.URL; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.atomic.AtomicReference; -import java.util.zip.GZIPOutputStream; +import com.github.kevinsawicki.http.HttpRequest.*; import javax.net.ssl.HttpsURLConnection; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URI; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.zip.GZIPOutputStream; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.B64Code; @@ -80,6 +42,12 @@ import org.junit.BeforeClass; import org.junit.Test; +import static com.github.kevinsawicki.http.HttpRequest.*; +import static java.net.HttpURLConnection.*; +import static org.junit.Assert.*; + +//import java.net.URL; + /** * Unit tests of {@link HttpRequest} */ @@ -209,7 +177,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = get(new URL(url)); + HttpRequest request = get(new URI(url)); assertNotNull(request.getConnection()); int code = request.code(); assertTrue(request.ok()); @@ -329,7 +297,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = delete(new URL(url)); + HttpRequest request = delete(new URI(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); @@ -377,7 +345,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = options(new URL(url)); + HttpRequest request = options(new URI(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); @@ -425,7 +393,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = head(new URL(url)); + HttpRequest request = head(new URI(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); @@ -473,7 +441,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = put(new URL(url)); + HttpRequest request = put(new URI(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); @@ -521,7 +489,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); } }; - HttpRequest request = trace(new URL(url)); + HttpRequest request = trace(new URI(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); @@ -569,7 +537,7 @@ public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_CREATED); } }; - HttpRequest request = post(new URL(url)); + HttpRequest request = post(new URI(url)); int code = request.code(); assertEquals("POST", method.get()); assertFalse(request.ok());