Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving from URL to URI #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
110 changes: 110 additions & 0 deletions lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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.
* <p>
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
76 changes: 22 additions & 54 deletions lib/src/test/java/com/github/kevinsawicki/http/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,33 @@
*/
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;
import org.junit.After;
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}
*/
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down