Skip to content

Commit

Permalink
more isolating of apache
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Jan 3, 2021
1 parent e8441e2 commit 685d8f8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 106 deletions.
7 changes: 3 additions & 4 deletions unirest/src/test/java/BehaviorTests/InterceptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -102,9 +101,9 @@ void canReturnEmptyResultRatherThanThrow() throws Exception {
@Test
void totalAsyncFailure_Recovery() throws Exception {
interceptor.failResponse = true;
Unirest.config().addInterceptor((r, c) -> {
throw new IOException("Something horrible happened");
}).interceptor(interceptor);
Unirest.config()
.asyncClient(TestUtil.getFailureAsyncClient())
.interceptor(interceptor);

HttpResponse<String> response = Unirest.get(MockServer.GET).asStringAsync().get();

Expand Down
46 changes: 0 additions & 46 deletions unirest/src/test/java/BehaviorTests/TimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@
import kong.unirest.Config;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -105,46 +101,4 @@ private void makeParallelRequests() throws InterruptedException {
newFixedThreadPool.shutdown();
newFixedThreadPool.awaitTermination(10, TimeUnit.MINUTES);
}

@Test
public void setTimeoutsAndCustomClient() {
try {
Unirest.config().connectTimeout(1000).socketTimeout(2000);
} catch (Exception e) {
fail();
}

try {
Unirest.config().asyncClient(HttpAsyncClientBuilder.create().build());
} catch (Exception e) {
fail();
}

try {
Unirest.config().asyncClient(HttpAsyncClientBuilder.create().build());
Unirest.config().connectTimeout(1000).socketTimeout(2000);
fail();
} catch (Exception e) {
// Ok
}

try {
Unirest.config().httpClient(HttpClientBuilder.create().build());
Unirest.config().connectTimeout(1000).socketTimeout(2000);
fail();
} catch (Exception e) {
// Ok
}
}

private String findAvailableIpAddress() throws IOException {
for (int i = 100; i <= 255; i++) {
String ip = "192.168.1." + i;
if (!InetAddress.getByName(ip).isReachable(1000)) {
return ip;
}
}

throw new RuntimeException("Couldn't find an available IP address in the range of 192.168.0.100-255");
}
}
20 changes: 0 additions & 20 deletions unirest/src/test/java/kong/unirest/ClientFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@

package kong.unirest;

import org.apache.http.HttpRequestInterceptor;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.management.ManagementFactory;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class ClientFactoryTest {

Expand All @@ -51,21 +48,4 @@ void shouldReuseThreadPool() {
assertTrue(ManagementFactory.getThreadMXBean().getThreadCount() < startingCount + 10);
}

@Test
void canSaveSomeOptions(){
HttpRequestInterceptor i = mock(HttpRequestInterceptor.class);
CloseableHttpAsyncClient c = mock(CloseableHttpAsyncClient.class);

Unirest.config()
.addInterceptor(i)
.connectTimeout(4000)
.asyncClient(c);

Unirest.shutDown(false);

assertNotEquals(c, Unirest.config().getAsyncClient());
assertEquals(i, Unirest.config().getInterceptor().get(0));
assertEquals(4000, Unirest.config().getConnectionTimeout());
}

}
37 changes: 13 additions & 24 deletions unirest/src/test/java/kong/unirest/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
package kong.unirest;

import BehaviorTests.MockServer;
import BehaviorTests.UploadProgressTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.Resources;
import kong.unirest.apache.ApacheAsyncClient;
import kong.unirest.apache.ApacheClient;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

import java.io.*;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -106,14 +109,6 @@ public static <T> T readValue(String body, Class<T> as) {
}
}

public static InputStream emptyInput() {
return new ByteArrayInputStream(new byte[]{});
}

public static InputStream toInputStream(String s) {
return new ByteArrayInputStream(s.getBytes());
}

public static <K, V> Map<K, V> mapOf(Object... keyValues) {
Map<K, V> map = new HashMap<>();

Expand All @@ -130,21 +125,6 @@ public static <K, V> Map<K, V> mapOf(Object... keyValues) {
return map;
}

public static void debugApache() {
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");
}

public static <T> T read(String o, Class<T> as){
try {
return om.readValue(o, as);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void assertBasicAuth(String raw, String username, String password) {
assertNotNull(raw, "Authorization Header Missing");
String credentials = raw.replace("Basic ","");
Expand Down Expand Up @@ -213,6 +193,15 @@ public static Client getFailureClient() throws IOException {
return new ApacheClient(client, Unirest.config());
}

public static AsyncClient getFailureAsyncClient() {
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
.addInterceptorFirst((HttpRequestInterceptor) (r, c) -> {
throw new IOException("Something horrible happened");
}).build();
client.start();
return new ApacheAsyncClient(client, Unirest.config());
}

@FunctionalInterface
public interface ExRunnable {
void run() throws Exception;
Expand Down
56 changes: 54 additions & 2 deletions unirest/src/test/java/kong/unirest/apache/ApacheBehaviorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
import BehaviorTests.BddTest;
import BehaviorTests.MockServer;
import kong.unirest.Unirest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;

public class ApacheBehaviorTest extends BddTest {
@Test
Expand All @@ -58,4 +62,52 @@ void issue_41_IllegalThreadStateExceptionUnderHighLoad() throws IOException {
assertSame(second, Unirest.config().getAsyncClient().getClient());
});
}

@Test
public void setTimeoutsAndCustomClient() {
try {
Unirest.config().connectTimeout(1000).socketTimeout(2000);
} catch (Exception e) {
fail();
}

try {
Unirest.config().asyncClient(HttpAsyncClientBuilder.create().build());
} catch (Exception e) {
fail();
}

try {
Unirest.config().asyncClient(HttpAsyncClientBuilder.create().build());
Unirest.config().connectTimeout(1000).socketTimeout(2000);
fail();
} catch (Exception e) {
// Ok
}

try {
Unirest.config().httpClient(HttpClientBuilder.create().build());
Unirest.config().connectTimeout(1000).socketTimeout(2000);
fail();
} catch (Exception e) {
// Ok
}
}

@Test
void canSaveSomeOptions(){
HttpRequestInterceptor i = mock(HttpRequestInterceptor.class);
CloseableHttpAsyncClient c = mock(CloseableHttpAsyncClient.class);

Unirest.config()
.addInterceptor(i)
.connectTimeout(4000)
.asyncClient(c);

Unirest.shutDown(false);

assertNotEquals(c, Unirest.config().getAsyncClient());
assertEquals(i, Unirest.config().getInterceptor().get(0));
assertEquals(4000, Unirest.config().getConnectionTimeout());
}
}
10 changes: 0 additions & 10 deletions unirest/src/test/java/kong/unirest/apache/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

package kong.unirest.apache;

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.jupiter.api.Test;

import java.util.Optional;
Expand All @@ -48,13 +45,6 @@ void canCast() {
assertTrue(Util.tryCast(new Bar(), Foo.class).isPresent());
}

@Test
void canCastAAsyncClient() {
HttpAsyncClient build = HttpAsyncClientBuilder.create().build();

assertTrue(Util.tryCast(build, CloseableHttpAsyncClient.class).isPresent());
}

public abstract static class Foo {}

public class Bar extends Foo {}
Expand Down

0 comments on commit 685d8f8

Please sign in to comment.