Skip to content

Commit

Permalink
More Dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Oct 13, 2015
1 parent 7c01408 commit ee32b57
Show file tree
Hide file tree
Showing 71 changed files with 514 additions and 580 deletions.
21 changes: 21 additions & 0 deletions client/src/main/java/org/asynchttpclient/Dsl.java
Expand Up @@ -15,13 +15,34 @@


import org.asynchttpclient.Realm.AuthScheme; import org.asynchttpclient.Realm.AuthScheme;
import org.asynchttpclient.Realm.RealmBuilder; import org.asynchttpclient.Realm.RealmBuilder;
import org.asynchttpclient.proxy.ProxyServer.ProxyServerBuilder;


public final class Dsl { public final class Dsl {


// /////////// Client ////////////////
public static AsyncHttpClient newAsyncHttpClient() {
return new DefaultAsyncHttpClient();
}

public static AsyncHttpClient newAsyncHttpClient(DefaultAsyncHttpClientConfig.Builder configBuilder) {
return new DefaultAsyncHttpClient(configBuilder.build());
}

public static AsyncHttpClient newAsyncHttpClient(AsyncHttpClientConfig config) {
return new DefaultAsyncHttpClient(config);
}

// /////////// ProxyServer ////////////////
public static ProxyServerBuilder newProxyServer(String host, int port) {
return new ProxyServerBuilder(host, port);
}

// /////////// Config ////////////////
public static DefaultAsyncHttpClientConfig.Builder newConfig() { public static DefaultAsyncHttpClientConfig.Builder newConfig() {
return new DefaultAsyncHttpClientConfig.Builder(); return new DefaultAsyncHttpClientConfig.Builder();
} }


// /////////// Realm ////////////////
public static RealmBuilder newRealm(Realm prototype) { public static RealmBuilder newRealm(Realm prototype) {
return new RealmBuilder()// return new RealmBuilder()//
.realmName(prototype.getRealmName())// .realmName(prototype.getRealmName())//
Expand Down
Expand Up @@ -29,10 +29,6 @@
*/ */
public class ProxyServer { public class ProxyServer {


public static ProxyServerBuilder newProxyServer(String host, int port) {
return new ProxyServerBuilder(host, port);
}

private final String host; private final String host;
private final int port; private final int port;
private final int securedPort; private final int securedPort;
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/org/asynchttpclient/util/ProxyUtils.java
Expand Up @@ -117,7 +117,7 @@ public static ProxyServerSelector createProxyServerSelector(Properties propertie
realm = newBasicAuth(principal, password).build(); realm = newBasicAuth(principal, password).build();
} }


ProxyServerBuilder proxyServer = ProxyServer.newProxyServer(host, port).realm(realm); ProxyServerBuilder proxyServer = newProxyServer(host, port).realm(realm);


String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS); String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS);
if (nonProxyHosts != null) { if (nonProxyHosts != null) {
Expand Down Expand Up @@ -162,7 +162,7 @@ public ProxyServer select(Uri uri) {
return null; return null;
} else { } else {
InetSocketAddress address = (InetSocketAddress) proxy.address(); InetSocketAddress address = (InetSocketAddress) proxy.address();
return ProxyServer.newProxyServer(address.getHostName(), address.getPort()).build(); return newProxyServer(address.getHostName(), address.getPort()).build();
} }
case DIRECT: case DIRECT:
return null; return null;
Expand Down
Expand Up @@ -39,7 +39,7 @@ public void asyncStreamGETTest() throws Exception {
final CountDownLatch l = new CountDownLatch(1); final CountDownLatch l = new CountDownLatch(1);
final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();
final AtomicReference<Throwable> throwable = new AtomicReference<>(); final AtomicReference<Throwable> throwable = new AtomicReference<>();
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
c.prepareGet(getTargetUrl()).execute(new AsyncHandlerAdapter() { c.prepareGet(getTargetUrl()).execute(new AsyncHandlerAdapter() {


@Override @Override
Expand Down Expand Up @@ -78,7 +78,7 @@ public void asyncStreamPOSTTest() throws Exception {


final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();


try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
Future<String> f = c.preparePost(getTargetUrl())// Future<String> f = c.preparePost(getTargetUrl())//
.setHeader("Content-Type", "application/x-www-form-urlencoded")// .setHeader("Content-Type", "application/x-www-form-urlencoded")//
.addFormParam("param_1", "value_1")// .addFormParam("param_1", "value_1")//
Expand Down Expand Up @@ -118,7 +118,7 @@ public void asyncStreamInterruptTest() throws Exception {
final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();
final AtomicBoolean bodyReceived = new AtomicBoolean(false); final AtomicBoolean bodyReceived = new AtomicBoolean(false);
final AtomicReference<Throwable> throwable = new AtomicReference<>(); final AtomicReference<Throwable> throwable = new AtomicReference<>();
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
c.preparePost(getTargetUrl())// c.preparePost(getTargetUrl())//
.setHeader("Content-Type", "application/x-www-form-urlencoded")// .setHeader("Content-Type", "application/x-www-form-urlencoded")//
.addFormParam("param_1", "value_1")// .addFormParam("param_1", "value_1")//
Expand Down Expand Up @@ -156,7 +156,7 @@ public void onThrowable(Throwable t) {
public void asyncStreamFutureTest() throws Exception { public void asyncStreamFutureTest() throws Exception {
final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();
final AtomicReference<Throwable> throwable = new AtomicReference<>(); final AtomicReference<Throwable> throwable = new AtomicReference<>();
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
Future<String> f = c.preparePost(getTargetUrl()).addFormParam("param_1", "value_1").execute(new AsyncHandlerAdapter() { Future<String> f = c.preparePost(getTargetUrl()).addFormParam("param_1", "value_1").execute(new AsyncHandlerAdapter() {
private StringBuilder builder = new StringBuilder(); private StringBuilder builder = new StringBuilder();


Expand Down Expand Up @@ -197,7 +197,7 @@ public void onThrowable(Throwable t) {
public void asyncStreamThrowableRefusedTest() throws Exception { public void asyncStreamThrowableRefusedTest() throws Exception {


final CountDownLatch l = new CountDownLatch(1); final CountDownLatch l = new CountDownLatch(1);
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
c.prepareGet(getTargetUrl()).execute(new AsyncHandlerAdapter() { c.prepareGet(getTargetUrl()).execute(new AsyncHandlerAdapter() {


@Override @Override
Expand Down Expand Up @@ -227,7 +227,7 @@ public void onThrowable(Throwable t) {
public void asyncStreamReusePOSTTest() throws Exception { public void asyncStreamReusePOSTTest() throws Exception {


final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
BoundRequestBuilder rb = c.preparePost(getTargetUrl())// BoundRequestBuilder rb = c.preparePost(getTargetUrl())//
.setHeader("Content-Type", "application/x-www-form-urlencoded") .setHeader("Content-Type", "application/x-www-form-urlencoded")
.addFormParam("param_1", "value_1"); .addFormParam("param_1", "value_1");
Expand Down Expand Up @@ -297,7 +297,7 @@ public String onCompleted() throws Exception {
public void asyncStream302RedirectWithBody() throws Exception { public void asyncStream302RedirectWithBody() throws Exception {
final AtomicReference<Integer> statusCode = new AtomicReference<>(0); final AtomicReference<Integer> statusCode = new AtomicReference<>(0);
final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();
try (AsyncHttpClient c = new DefaultAsyncHttpClient(newConfig().followRedirect(true).build())) { try (AsyncHttpClient c = newAsyncHttpClient(newConfig().followRedirect(true).build())) {
Future<String> f = c.prepareGet("http://google.com/").execute(new AsyncHandlerAdapter() { Future<String> f = c.prepareGet("http://google.com/").execute(new AsyncHandlerAdapter() {


public State onStatusReceived(HttpResponseStatus status) throws Exception { public State onStatusReceived(HttpResponseStatus status) throws Exception {
Expand Down Expand Up @@ -340,7 +340,7 @@ public void asyncStreamJustStatusLine() throws Exception {
final int OTHER = 2; final int OTHER = 2;
final boolean[] whatCalled = new boolean[] { false, false, false }; final boolean[] whatCalled = new boolean[] { false, false, false };
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
Future<Integer> statusCode = client.prepareGet(getTargetUrl()).execute(new AsyncHandler<Integer>() { Future<Integer> statusCode = client.prepareGet(getTargetUrl()).execute(new AsyncHandler<Integer>() {
private int status = -1; private int status = -1;


Expand Down Expand Up @@ -403,7 +403,7 @@ public Integer onCompleted() throws Exception {
public void asyncOptionsTest() throws Exception { public void asyncOptionsTest() throws Exception {
final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>(); final AtomicReference<HttpHeaders> responseHeaders = new AtomicReference<>();


try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
final String[] expected = { "GET", "HEAD", "OPTIONS", "POST", "TRACE" }; final String[] expected = { "GET", "HEAD", "OPTIONS", "POST", "TRACE" };
Future<String> f = c.prepareOptions("http://www.apache.org/").execute(new AsyncHandlerAdapter() { Future<String> f = c.prepareOptions("http://www.apache.org/").execute(new AsyncHandlerAdapter() {


Expand Down Expand Up @@ -432,7 +432,7 @@ public String onCompleted() throws Exception {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void closeConnectionTest() throws Exception { public void closeConnectionTest() throws Exception {
try (AsyncHttpClient c = new DefaultAsyncHttpClient()) { try (AsyncHttpClient c = newAsyncHttpClient()) {
Response r = c.prepareGet(getTargetUrl()).execute(new AsyncHandler<Response>() { Response r = c.prepareGet(getTargetUrl()).execute(new AsyncHandler<Response>() {


private Response.ResponseBuilder builder = new Response.ResponseBuilder(); private Response.ResponseBuilder builder = new Response.ResponseBuilder();
Expand Down
Expand Up @@ -15,22 +15,8 @@
*/ */
package org.asynchttpclient; package org.asynchttpclient;


import static org.testng.Assert.assertEquals; import static org.asynchttpclient.Dsl.newAsyncHttpClient;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.*;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import org.asynchttpclient.AsyncHttpClient;
import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationSupport;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
Expand All @@ -42,6 +28,17 @@
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationSupport;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

/** /**
* Tests default asynchronous life cycle. * Tests default asynchronous life cycle.
* *
Expand Down Expand Up @@ -100,7 +97,7 @@ public void run() {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void testStream() throws IOException { public void testStream() throws IOException {
try (AsyncHttpClient ahc = new DefaultAsyncHttpClient()) { try (AsyncHttpClient ahc = newAsyncHttpClient()) {
final AtomicBoolean err = new AtomicBoolean(false); final AtomicBoolean err = new AtomicBoolean(false);
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(); final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
final AtomicBoolean status = new AtomicBoolean(false); final AtomicBoolean status = new AtomicBoolean(false);
Expand Down
Expand Up @@ -176,7 +176,7 @@ protected void inspectException(Throwable t) {
} }


private AsyncHttpClient newClient() { private AsyncHttpClient newClient() {
return new DefaultAsyncHttpClient(newConfig().pooledConnectionIdleTimeout(2000).connectTimeout(20000).requestTimeout(2000).build()); return newAsyncHttpClient(newConfig().pooledConnectionIdleTimeout(2000).connectTimeout(20000).requestTimeout(2000).build());
} }


protected Future<Response> execute(AsyncHttpClient client, Server server, boolean preemptive) throws IOException { protected Future<Response> execute(AsyncHttpClient client, Server server, boolean preemptive) throws IOException {
Expand Down
20 changes: 10 additions & 10 deletions client/src/test/java/org/asynchttpclient/BasicAuthTest.java
Expand Up @@ -161,7 +161,7 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void basicAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
Future<Response> f = client.prepareGet(getTargetUrl())// Future<Response> f = client.prepareGet(getTargetUrl())//
.setRealm(newBasicAuth(USER, ADMIN).build())// .setRealm(newBasicAuth(USER, ADMIN).build())//
.execute(); .execute();
Expand All @@ -174,7 +174,7 @@ public void basicAuthTest() throws IOException, ExecutionException, TimeoutExcep


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void redirectAndDigestAuthTest() throws Exception, ExecutionException, TimeoutException, InterruptedException { public void redirectAndDigestAuthTest() throws Exception, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient(newConfig().followRedirect(true).maxRedirects(10).build())) { try (AsyncHttpClient client = newAsyncHttpClient(newConfig().followRedirect(true).maxRedirects(10).build())) {
Future<Response> f = client.prepareGet(getTargetUrl2())// Future<Response> f = client.prepareGet(getTargetUrl2())//
.setRealm(newBasicAuth(USER, ADMIN).build())// .setRealm(newBasicAuth(USER, ADMIN).build())//
.execute(); .execute();
Expand All @@ -187,7 +187,7 @@ public void redirectAndDigestAuthTest() throws Exception, ExecutionException, Ti


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basic401Test() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void basic401Test() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
BoundRequestBuilder r = client.prepareGet(getTargetUrl())// BoundRequestBuilder r = client.prepareGet(getTargetUrl())//
.setHeader("X-401", "401")// .setHeader("X-401", "401")//
.setRealm(newBasicAuth(USER, ADMIN).build()); .setRealm(newBasicAuth(USER, ADMIN).build());
Expand Down Expand Up @@ -229,7 +229,7 @@ public Integer onCompleted() throws Exception {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
// send the request to the no-auth endpoint to be able to verify the // send the request to the no-auth endpoint to be able to verify the
// auth header is really sent preemptively for the initial call. // auth header is really sent preemptively for the initial call.
Future<Response> f = client.prepareGet(getTargetUrlNoAuth())// Future<Response> f = client.prepareGet(getTargetUrlNoAuth())//
Expand All @@ -245,7 +245,7 @@ public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException,


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void basicAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
Future<Response> f = client.prepareGet(getTargetUrl())// Future<Response> f = client.prepareGet(getTargetUrl())//
.setRealm(newBasicAuth("fake", ADMIN).build())// .setRealm(newBasicAuth("fake", ADMIN).build())//
.execute(); .execute();
Expand All @@ -258,7 +258,7 @@ public void basicAuthNegativeTest() throws IOException, ExecutionException, Time


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthInputStreamTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void basicAuthInputStreamTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
Future<Response> f = client.preparePost(getTargetUrl())// Future<Response> f = client.preparePost(getTargetUrl())//
.setBody(new ByteArrayInputStream("test".getBytes()))// .setBody(new ByteArrayInputStream("test".getBytes()))//
.setRealm(newBasicAuth(USER, ADMIN).build())// .setRealm(newBasicAuth(USER, ADMIN).build())//
Expand All @@ -274,7 +274,7 @@ public void basicAuthInputStreamTest() throws IOException, ExecutionException, T


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthFileTest() throws Exception { public void basicAuthFileTest() throws Exception {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
Future<Response> f = client.preparePost(getTargetUrl())// Future<Response> f = client.preparePost(getTargetUrl())//
.setBody(SIMPLE_TEXT_FILE)// .setBody(SIMPLE_TEXT_FILE)//
.setRealm(newBasicAuth(USER, ADMIN).build())// .setRealm(newBasicAuth(USER, ADMIN).build())//
Expand All @@ -290,7 +290,7 @@ public void basicAuthFileTest() throws Exception {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthAsyncConfigTest() throws Exception { public void basicAuthAsyncConfigTest() throws Exception {
try (AsyncHttpClient client = new DefaultAsyncHttpClient(newConfig().realm(newBasicAuth(USER, ADMIN).build()).build())) { try (AsyncHttpClient client = newAsyncHttpClient(newConfig().realm(newBasicAuth(USER, ADMIN).build()).build())) {
Future<Response> f = client.preparePost(getTargetUrl())// Future<Response> f = client.preparePost(getTargetUrl())//
.setBody(SIMPLE_TEXT_FILE_STRING)// .setBody(SIMPLE_TEXT_FILE_STRING)//
.execute(); .execute();
Expand All @@ -305,7 +305,7 @@ public void basicAuthAsyncConfigTest() throws Exception {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void basicAuthFileNoKeepAliveTest() throws Exception { public void basicAuthFileNoKeepAliveTest() throws Exception {
try (AsyncHttpClient client = new DefaultAsyncHttpClient(newConfig().allowPoolingConnections(false).build())) { try (AsyncHttpClient client = newAsyncHttpClient(newConfig().allowPoolingConnections(false).build())) {


Future<Response> f = client.preparePost(getTargetUrl())// Future<Response> f = client.preparePost(getTargetUrl())//
.setBody(SIMPLE_TEXT_FILE)// .setBody(SIMPLE_TEXT_FILE)//
Expand All @@ -322,7 +322,7 @@ public void basicAuthFileNoKeepAliveTest() throws Exception {


@Test(groups = { "standalone", "default_provider" }) @Test(groups = { "standalone", "default_provider" })
public void noneAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { public void noneAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient()) { try (AsyncHttpClient client = newAsyncHttpClient()) {
BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm(newBasicAuth(USER, ADMIN).build()); BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm(newBasicAuth(USER, ADMIN).build());


Future<Response> f = r.execute(); Future<Response> f = r.execute();
Expand Down

0 comments on commit ee32b57

Please sign in to comment.