Skip to content

Commit

Permalink
move apache specific tests to the apache packge
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Jan 1, 2021
1 parent a64e8b6 commit a1abbe9
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 83 deletions.
2 changes: 1 addition & 1 deletion unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class FieldMatcher implements BodyMatcher {
/**
* Creates a FieldMatcher expecting a map of keys and values
* use like: FieldMatcher.of("fruit", "orange", "quantity" "42")
* @param keyValuePairs an array of key->value pairs to expect
* @param keyValuePairs an array of key-value pairs to expect
* @return a new FieldMatcher
*/
public static FieldMatcher of(String... keyValuePairs) {
Expand Down
2 changes: 1 addition & 1 deletion unirest-mocks/src/main/java/kong/unirest/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Matchers {
/**
* Creates a FieldMatcher expecting a map of keys and values
* use like: FieldMatcher.of("fruit", "orange", "quantity" "42")
* @param keyValuePairs an array of key->value pairs to expect
* @param keyValuePairs an array of key-value pairs to expect
* @return a new FieldMatcher
*/
public BodyMatcher bodyFields(String keyValuePairs){
Expand Down
88 changes: 7 additions & 81 deletions unirest/src/test/java/BehaviorTests/CertificateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,13 @@
import kong.unirest.TestUtil;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;

import java.io.InputStream;
import java.security.KeyStore;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -61,7 +45,7 @@ class CertificateTests extends BddTest {

@Test
void canDoClientCertificates() throws Exception {
Unirest.config().clientCertificateStore(readStore(), "badssl.com");
Unirest.config().clientCertificateStore(TestUtil.readStore(), "badssl.com");

Unirest.get("https://client.badssl.com/")
.asString()
Expand All @@ -85,7 +69,7 @@ void canLoadKeyStoreByPath() {
@Test
void loadWithSSLContext() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config().sslContext(sslContext);
Expand All @@ -97,7 +81,7 @@ void loadWithSSLContext() throws Exception {
@Test
void loadWithSSLContextAndProtocol() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config().sslContext(sslContext).protocols("TLSv1.2");
Expand All @@ -109,7 +93,7 @@ void loadWithSSLContextAndProtocol() throws Exception {
@Test
void loadWithSSLContextAndCipher() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config().sslContext(sslContext).ciphers("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384");
Expand All @@ -121,7 +105,7 @@ void loadWithSSLContextAndCipher() throws Exception {
@Test
void loadWithSSLContextAndCipherAndProtocol() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config()
Expand All @@ -136,7 +120,7 @@ void loadWithSSLContextAndCipherAndProtocol() throws Exception {
@Test
void sslHandshakeFailsWhenServerIsReceivingAnUnsupportedCipher() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config()
Expand All @@ -151,7 +135,7 @@ void sslHandshakeFailsWhenServerIsReceivingAnUnsupportedCipher() throws Exceptio
@Test
void clientPreventsToUseUnsafeProtocol() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.loadKeyMaterial(TestUtil.readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

Unirest.config()
Expand All @@ -170,57 +154,6 @@ void canSetHoestNameVerifyer() {
assertEquals(200, response);
}

@Test
void rawApacheClientCert() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();

HttpResponse response = httpClient.execute(new HttpGet("https://client.badssl.com/"));
assertEquals(200, response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
}

@Test
void rawApacheWithConnectionManager() throws Exception {
SSLContext sc = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sc, (q, w) -> true);


Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslSocketFactory)
.register("http", PlainConnectionSocketFactory.INSTANCE)
.build();

PoolingHttpClientConnectionManager cm =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);

CloseableHttpClient httpClient =
HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(cm)
.build();

Unirest.config().httpClient(httpClient);

Unirest.get("https://client.badssl.com/")
.asString()
.ifFailure(r -> fail(r.getStatus() + " request failed " + r.getBody()))
.ifSuccess(r -> System.out.println(" woot "));

}

@Test
void badName() {
fails("https://wrong.host.badssl.com/",
Expand Down Expand Up @@ -311,11 +244,4 @@ private void canCallAsync(String url) {
}
}

private KeyStore readStore() throws Exception {
try (InputStream keyStoreStream = this.getClass().getResourceAsStream("/certs/badssl.com-client.p12")) {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(keyStoreStream, "badssl.com".toCharArray());
return keyStore;
}
}
}
9 changes: 9 additions & 0 deletions unirest/src/test/java/kong/unirest/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import java.io.*;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
Expand Down Expand Up @@ -190,6 +191,14 @@ public static void reset() {
Util.resetClock();
}

public static KeyStore readStore() throws Exception {
try (InputStream keyStoreStream = TestUtil.class.getResourceAsStream("/certs/badssl.com-client.p12")) {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(keyStoreStream, "badssl.com".toCharArray());
return keyStore;
}
}

@FunctionalInterface
public interface ExRunnable {
void run() throws Exception;
Expand Down
105 changes: 105 additions & 0 deletions unirest/src/test/java/kong/unirest/apache/ExampleCertificateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package kong.unirest.apache;

import BehaviorTests.SSLContextBuilder;
import kong.unirest.Unirest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLContext;

import static kong.unirest.TestUtil.readStore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

@Disabled
public class ExampleCertificateTest {

@Test
void rawApacheClientCert() throws Exception {
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();

HttpResponse response = httpClient.execute(new HttpGet("https://client.badssl.com/"));
assertEquals(200, response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
}

@Test
void rawApacheWithConnectionManager() throws Exception {
SSLContext sc = SSLContextBuilder.create()
.loadKeyMaterial(readStore(), "badssl.com".toCharArray()) // use null as second param if you don't have a separate key password
.build();

SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sc, (q, w) -> true);


Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslSocketFactory)
.register("http", PlainConnectionSocketFactory.INSTANCE)
.build();

PoolingHttpClientConnectionManager cm =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);

CloseableHttpClient httpClient =
HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(cm)
.build();

Unirest.config().httpClient(httpClient);

Unirest.get("https://client.badssl.com/")
.asString()
.ifFailure(r -> fail(r.getStatus() + " request failed " + r.getBody()))
.ifSuccess(r -> System.out.println(" woot "));

}
}

0 comments on commit a1abbe9

Please sign in to comment.