Classic test suits to execute in plain and TLS modes

This commit is contained in:
Oleg Kalnichevski 2023-11-28 10:48:51 +01:00
parent 9ec63474c8
commit efe2598a30
9 changed files with 238 additions and 22 deletions

View File

@ -0,0 +1,115 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.testing.sync;
import org.apache.hc.core5.http.URIScheme;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
public class HttpIntegrationTests {
@Nested
@DisplayName("Request execution (HTTP/1.1)")
public class RequestExecution extends TestClientRequestExecution {
public RequestExecution() throws Exception {
super(URIScheme.HTTP);
}
}
@Nested
@DisplayName("Request execution (HTTP/1.1, TLS)")
public class RequestExecutionTls extends TestClientRequestExecution {
public RequestExecutionTls() throws Exception {
super(URIScheme.HTTPS);
}
}
@Nested
@DisplayName("Authentication (HTTP/1.1)")
public class Authentication extends TestClientAuthentication {
public Authentication() throws Exception {
super(URIScheme.HTTP);
}
}
@Nested
@DisplayName("Authentication (HTTP/1.1, TLS)")
public class AuthenticationTls extends TestClientAuthentication {
public AuthenticationTls() throws Exception {
super(URIScheme.HTTPS);
}
}
@Nested
@DisplayName("Content coding (HTTP/1.1)")
public class ContentCoding extends TestContentCodings {
public ContentCoding() throws Exception {
super(URIScheme.HTTP);
}
}
@Nested
@DisplayName("Content coding (HTTP/1.1, TLS)")
public class ContentCodingTls extends TestContentCodings {
public ContentCodingTls() throws Exception {
super(URIScheme.HTTPS);
}
}
@Nested
@DisplayName("Redirects (HTTP/1.1)")
public class Redirects extends TestRedirects {
public Redirects() throws Exception {
super(URIScheme.HTTP);
}
}
@Nested
@DisplayName("Redirects (HTTP/1.1, TLS)")
public class RedirectsTls extends TestRedirects {
public RedirectsTls() throws Exception {
super(URIScheme.HTTPS);
}
}
}

View File

@ -0,0 +1,55 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.testing.sync;
import org.apache.hc.core5.http.URIScheme;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
public class HttpMinimalIntegrationTests {
@Nested
@DisplayName("Request execution (HTTP/1.1)")
public class RequestExecution extends TestMinimalClientRequestExecution {
public RequestExecution() throws Exception {
super(URIScheme.HTTP);
}
}
@Nested
@DisplayName("Request execution (HTTP/1.1, TLS)")
public class RequestExecutionTls extends TestMinimalClientRequestExecution {
public RequestExecutionTls() throws Exception {
super(URIScheme.HTTPS);
}
}
}

View File

@ -99,12 +99,20 @@ import org.mockito.Mockito;
/**
* Unit tests for automatic client authentication.
*/
public class TestClientAuthentication {
public abstract class TestClientAuthentication {
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@RegisterExtension
private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
private TestClientResources testResources;
protected TestClientAuthentication(final URIScheme scheme) {
this.testResources = new TestClientResources(scheme, TIMEOUT);
}
public URIScheme scheme() {
return testResources.scheme();
}
public ClassicTestServer startServer(final Authenticator authenticator) throws IOException {
return testResources.startServer(
@ -117,11 +125,11 @@ public class TestClientAuthentication {
return startServer(new BasicTestAuthenticator("test:test", "test realm"));
}
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) {
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
return testResources.startClient(clientCustomizer);
}
public CloseableHttpClient startClient() {
public CloseableHttpClient startClient() throws Exception {
return testResources.startClient(builder -> {});
}

View File

@ -74,22 +74,30 @@ import org.junit.jupiter.api.extension.RegisterExtension;
/**
* Client protocol handling tests.
*/
public class TestClientRequestExecution {
public abstract class TestClientRequestExecution {
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@RegisterExtension
private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
private TestClientResources testResources;
protected TestClientRequestExecution(final URIScheme scheme) {
this.testResources = new TestClientResources(scheme, TIMEOUT);
}
public URIScheme scheme() {
return testResources.scheme();
}
public ClassicTestServer startServer() throws IOException {
return testResources.startServer(null, null, null);
}
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) {
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
return testResources.startClient(clientCustomizer);
}
public CloseableHttpClient startClient() {
public CloseableHttpClient startClient() throws Exception {
return testResources.startClient(builder -> {});
}

View File

@ -76,7 +76,7 @@ public class TestConnectionManagement {
return testResources.startServer(null, null, null);
}
public CloseableHttpClient startClient() {
public CloseableHttpClient startClient() throws Exception {
return testResources.startClient(b -> {}, b -> {});
}

View File

@ -72,22 +72,30 @@ import org.junit.jupiter.api.extension.RegisterExtension;
* require no intervention from the user of HttpClient, but we still want to let clients do their
* own thing if they so wish.
*/
public class TestContentCodings {
public abstract class TestContentCodings {
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@RegisterExtension
private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
private TestClientResources testResources;
protected TestContentCodings(final URIScheme scheme) {
this.testResources = new TestClientResources(scheme, TIMEOUT);
}
public URIScheme scheme() {
return testResources.scheme();
}
public ClassicTestServer startServer() throws IOException {
return testResources.startServer(null, null, null);
}
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) {
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
return testResources.startClient(clientCustomizer);
}
public CloseableHttpClient startClient() {
public CloseableHttpClient startClient() throws Exception {
return testResources.startClient(builder -> {});
}

View File

@ -56,13 +56,20 @@ import org.junit.jupiter.api.extension.RegisterExtension;
/**
* Client protocol handling tests.
*/
public class TestMinimalClientRequestExecution {
public abstract class TestMinimalClientRequestExecution {
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@RegisterExtension
private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
private TestClientResources testResources;
protected TestMinimalClientRequestExecution(final URIScheme scheme) {
this.testResources = new TestClientResources(scheme, TIMEOUT);
}
public URIScheme scheme() {
return testResources.scheme();
}
private static class SimpleService implements HttpRequestHandler {
public SimpleService() {

View File

@ -81,23 +81,31 @@ import org.junit.jupiter.api.extension.RegisterExtension;
/**
* Redirection test cases.
*/
public class TestRedirects {
public abstract class TestRedirects {
public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@RegisterExtension
private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
private TestClientResources testResources;
protected TestRedirects(final URIScheme scheme) {
this.testResources = new TestClientResources(scheme, TIMEOUT);
}
public URIScheme scheme() {
return testResources.scheme();
}
public ClassicTestServer startServer(final HttpProcessor httpProcessor,
final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
return testResources.startServer(null, httpProcessor, handlerDecorator);
}
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) {
public CloseableHttpClient startClient(final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
return testResources.startClient(clientCustomizer);
}
public CloseableHttpClient startClient() {
public CloseableHttpClient startClient() throws Exception {
return testResources.startClient(builder -> {});
}

View File

@ -37,6 +37,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.classic.MinimalHttpClient;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.testing.SSLTestContexts;
import org.apache.hc.core5.function.Decorator;
import org.apache.hc.core5.http.HttpHost;
@ -98,6 +99,10 @@ public class TestClientResources implements BeforeEachCallback, AfterEachCallbac
}
}
public URIScheme scheme() {
return this.scheme;
}
public ClassicTestServer startServer(
final Http1Config http1Config,
final HttpProcessor httpProcessor,
@ -114,11 +119,12 @@ public class TestClientResources implements BeforeEachCallback, AfterEachCallbac
public CloseableHttpClient startClient(
final Consumer<PoolingHttpClientConnectionManagerBuilder> connManagerCustomizer,
final Consumer<HttpClientBuilder> clientCustomizer) {
final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
Assertions.assertNull(connManager);
Assertions.assertNull(client);
final PoolingHttpClientConnectionManagerBuilder connManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
connManagerBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLTestContexts.createClientSSLContext()));
connManagerBuilder.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(timeout)
.build());
@ -136,11 +142,12 @@ public class TestClientResources implements BeforeEachCallback, AfterEachCallbac
return client;
}
public MinimalHttpClient startMinimalClient() {
public MinimalHttpClient startMinimalClient() throws Exception {
Assertions.assertNull(connManager);
Assertions.assertNull(client);
final PoolingHttpClientConnectionManagerBuilder connManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
connManagerBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLTestContexts.createClientSSLContext()));
connManagerBuilder.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(timeout)
.build());
@ -155,7 +162,7 @@ public class TestClientResources implements BeforeEachCallback, AfterEachCallbac
}
public CloseableHttpClient startClient(
final Consumer<HttpClientBuilder> clientCustomizer) {
final Consumer<HttpClientBuilder> clientCustomizer) throws Exception {
return startClient(b -> {}, clientCustomizer);
}