Skip to content

Commit 95de1e8

Browse files
committed
Migrate tests to built-in HTTP client
Apache HttpClient is no longer present and was indirectly reference from odic mockserver
1 parent 3b0b8aa commit 95de1e8

6 files changed

Lines changed: 122 additions & 176 deletions

File tree

demo/integration-tests/webapp-it-base/src/main/java/software/xdev/sse/demo/webapp/base/IntegrationTestDefaults.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package software.xdev.sse.demo.webapp.base;
22

3+
import java.net.URI;
4+
import java.net.http.HttpClient;
5+
import java.net.http.HttpRequest;
36
import java.time.Duration;
47
import java.util.Objects;
58
import java.util.function.Function;
@@ -14,6 +17,8 @@
1417
@SuppressWarnings("java:S119")
1518
public interface IntegrationTestDefaults<SELF extends AbstractBaseTest<?>>
1619
{
20+
Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30);
21+
1722
@SuppressWarnings("unchecked")
1823
default SELF self()
1924
{
@@ -105,4 +110,24 @@ default <V> V waitUntil(final Function<WebDriver, V> isTrue, final Duration dura
105110
{
106111
return new WebDriverWait(this.self().getWebDriver(), duration).until(isTrue);
107112
}
113+
114+
default HttpClient createDefaultHttpClient()
115+
{
116+
return HttpClient.newBuilder()
117+
.followRedirects(HttpClient.Redirect.NEVER)
118+
.connectTimeout(DEFAULT_TIMEOUT)
119+
.build();
120+
}
121+
122+
default HttpRequest.Builder createDefaultHttpRequestBuilder(final String url)
123+
{
124+
return HttpRequest.newBuilder(URI.create(url))
125+
.timeout(DEFAULT_TIMEOUT);
126+
}
127+
128+
default HttpRequest.Builder createDefaultHttpRequestBuilder(final String method, final String relativeUrl)
129+
{
130+
return this.createDefaultHttpRequestBuilder(this.self().appInfra().getExternalHTTPEndpoint() + relativeUrl)
131+
.method(method, HttpRequest.BodyPublishers.noBody());
132+
}
108133
}

demo/integration-tests/webapp-rest-it/src/test/java/software/xdev/sse/demo/rest/cases/LoginOtherTest.java

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,18 @@
22

33
import static org.junit.jupiter.api.Assertions.assertAll;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7-
import java.io.IOException;
8-
import java.net.URI;
7+
import java.net.http.HttpClient;
8+
import java.net.http.HttpRequest;
9+
import java.net.http.HttpResponse;
910
import java.nio.charset.StandardCharsets;
10-
import java.time.Duration;
1111
import java.util.List;
1212
import java.util.Set;
1313
import java.util.function.Function;
1414
import java.util.stream.Stream;
1515

16-
import org.apache.hc.client5.http.classic.methods.HttpDelete;
17-
import org.apache.hc.client5.http.classic.methods.HttpGet;
18-
import org.apache.hc.client5.http.classic.methods.HttpHead;
19-
import org.apache.hc.client5.http.classic.methods.HttpOptions;
20-
import org.apache.hc.client5.http.classic.methods.HttpPost;
21-
import org.apache.hc.client5.http.classic.methods.HttpPut;
22-
import org.apache.hc.client5.http.classic.methods.HttpTrace;
23-
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
24-
import org.apache.hc.client5.http.config.ConnectionConfig;
25-
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
26-
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
27-
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
28-
import org.apache.hc.client5.http.utils.Base64;
29-
import org.apache.hc.core5.http.ClassicHttpResponse;
30-
import org.apache.hc.core5.http.HttpHeaders;
31-
import org.apache.hc.core5.http.HttpResponse;
32-
import org.apache.hc.core5.http.HttpStatus;
33-
import org.apache.hc.core5.util.Timeout;
16+
import org.apache.commons.codec.binary.Base64;
3417
import org.junit.jupiter.api.DisplayName;
3518
import org.junit.jupiter.api.function.Executable;
3619
import org.junit.jupiter.params.ParameterizedTest;
@@ -43,35 +26,33 @@
4326
class LoginOtherTest extends InfraPerClassTest
4427
{
4528
static final Set<String> NON_CSRF_METHODS = Set.of(
46-
HttpGet.METHOD_NAME,
47-
HttpOptions.METHOD_NAME,
48-
HttpHead.METHOD_NAME);
29+
"GET",
30+
"OPTIONS",
31+
"HEAD");
4932

5033
@DisplayName("No session should be created for public static resource")
5134
@ParameterizedTest(name = "{displayName} [method={0}] expect={1}")
5235
@MethodSource
53-
void checkNoSessionCreatedForPublicStaticResource(final String method, final int expectedCode) throws IOException
36+
void checkNoSessionCreatedForPublicStaticResource(final String method, final int expectedCode) throws Exception
5437
{
55-
try(final CloseableHttpClient client = createDefaultHttpClient())
38+
try(final HttpClient client = this.createDefaultHttpClient())
5639
{
57-
final HttpUriRequestBase http = new HttpUriRequestBase(
58-
method,
59-
URI.create(this.appInfra().getExternalHTTPEndpoint() + "/robots.txt"));
60-
try(final ClassicHttpResponse response = client.execute(http, r -> r))
61-
{
62-
assertAll(this.assertsNoSessionNoLoginAndCode(expectedCode, response));
63-
}
40+
assertAll(this.assertsNoSessionNoLoginAndCode(
41+
expectedCode,
42+
client.send(
43+
this.createDefaultHttpRequestBuilder(method, "/robots.txt").build(),
44+
HttpResponse.BodyHandlers.discarding())));
6445
}
6546
}
6647

6748
static Stream<Arguments> checkNoSessionCreatedForPublicStaticResource()
6849
{
6950
return Stream.concat(
7051
NON_CSRF_METHODS.stream()
71-
.map(m -> Arguments.of(m, HttpStatus.SC_OK)),
52+
.map(m -> Arguments.of(m, 200)),
7253
ALL_SUPPORTED_HTTP_METHODS.stream()
7354
.filter(m -> !NON_CSRF_METHODS.contains(m))
74-
.map(m -> Arguments.of(m, HttpStatus.SC_METHOD_NOT_ALLOWED))
55+
.map(m -> Arguments.of(m, 405))
7556
);
7657
}
7758

@@ -83,25 +64,23 @@ void checkNoSessionCreatedForActuator(
8364
final boolean existingPath,
8465
final String method,
8566
final int expectedCode)
86-
throws IOException
67+
throws Exception
8768
{
88-
try(final CloseableHttpClient client = createDefaultHttpClient())
69+
try(final HttpClient client = this.createDefaultHttpClient())
8970
{
90-
final HttpUriRequestBase http = new HttpUriRequestBase(
91-
method,
92-
URI.create(this.appInfra().getExternalHTTPEndpoint() + "/actuator" + (existingPath ? "" : "/abc")));
71+
final HttpRequest.Builder requestBuilder =
72+
this.createDefaultHttpRequestBuilder(method, "/actuator" + (existingPath ? "" : "/abc"));
9373
if(withAuth)
9474
{
9575
final String auth =
9676
this.appInfra().getActuatorUsername() + ":" + this.appInfra().getActuatorPassword();
97-
http.setHeader(
98-
HttpHeaders.AUTHORIZATION,
77+
requestBuilder.header(
78+
"Authorization",
9979
"Basic " + new String(Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1))));
10080
}
101-
try(final ClassicHttpResponse response = client.execute(http, r -> r))
102-
{
103-
assertAll(this.assertsNoSessionNoLoginAndCode(expectedCode, response));
104-
}
81+
assertAll(this.assertsNoSessionNoLoginAndCode(
82+
expectedCode,
83+
client.send(requestBuilder.build(), HttpResponse.BodyHandlers.discarding())));
10584
}
10685
}
10786

@@ -110,53 +89,39 @@ static Stream<Arguments> checkNoSessionCreatedForActuator()
11089
return Stream.of(
11190
// NO AUTH but ENDPOINT EXISTS
11291
ALL_SUPPORTED_HTTP_METHODS.stream()
113-
.map(method -> Arguments.of(false, true, method, HttpStatus.SC_UNAUTHORIZED)),
92+
.map(method -> Arguments.of(false, true, method, 401)),
11493
// AUTH and ENDPOINT EXISTS
11594
NON_CSRF_METHODS.stream()
116-
.map(method -> Arguments.of(true, true, method, HttpStatus.SC_OK)),
95+
.map(method -> Arguments.of(true, true, method, 200)),
11796
ALL_SUPPORTED_HTTP_METHODS.stream()
11897
.filter(m -> !NON_CSRF_METHODS.contains(m))
119-
.map(method -> Arguments.of(true, true, method, HttpStatus.SC_METHOD_NOT_ALLOWED)),
98+
.map(method -> Arguments.of(true, true, method, 405)),
12099
// AUTH and INVALID ENDPOINT
121100
ALL_SUPPORTED_HTTP_METHODS.stream()
122-
.map(method -> Arguments.of(true, false, method, HttpStatus.SC_NOT_FOUND)),
101+
.map(method -> Arguments.of(true, false, method, 404)),
123102
// NO AUTH and INVALID ENDPOINT
124103
ALL_SUPPORTED_HTTP_METHODS.stream()
125-
.map(method -> Arguments.of(false, false, method, HttpStatus.SC_UNAUTHORIZED)),
104+
.map(method -> Arguments.of(false, false, method, 401)),
126105
// TRACE is not supported by Spring Boot
127106
Stream.of(false, true)
128107
.map(existingPath ->
129-
Arguments.of(false, existingPath, HttpTrace.METHOD_NAME, HttpStatus.SC_METHOD_NOT_ALLOWED))
108+
Arguments.of(false, existingPath, "TRACE", 405))
130109
).flatMap(Function.identity());
131110
}
132111

133-
protected static CloseableHttpClient createDefaultHttpClient()
134-
{
135-
final Duration timeout = Duration.ofSeconds(30);
136-
return HttpClientBuilder.create()
137-
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
138-
.setDefaultConnectionConfig(ConnectionConfig.custom()
139-
.setConnectTimeout(Timeout.of(timeout))
140-
.setSocketTimeout(Timeout.of(timeout))
141-
.build())
142-
.build())
143-
.disableRedirectHandling()
144-
.build();
145-
}
146-
147-
private Stream<Executable> assertsNoSessionNoLoginAndCode(final int expectedCode, final HttpResponse response)
112+
private Stream<Executable> assertsNoSessionNoLoginAndCode(final int expectedCode, final HttpResponse<?> response)
148113
{
149114
return Stream.of(
150-
() -> assertEquals(expectedCode, response.getCode()),
151-
() -> assertNull(response.getHeader("Set-Cookie"))
115+
() -> assertEquals(expectedCode, response.statusCode()),
116+
() -> assertTrue(response.headers().firstValue("Set-Cookie").isEmpty())
152117
);
153118
}
154119

155120
static final List<String> ALL_SUPPORTED_HTTP_METHODS = List.of(
156-
HttpGet.METHOD_NAME,
157-
HttpPost.METHOD_NAME,
158-
HttpPut.METHOD_NAME,
159-
HttpDelete.METHOD_NAME,
160-
HttpHead.METHOD_NAME,
161-
HttpOptions.METHOD_NAME);
121+
"GET",
122+
"POST",
123+
"PUT",
124+
"DELETE",
125+
"HEAD",
126+
"OPTIONS");
162127
}

demo/integration-tests/webapp-rest-it/src/test/java/software/xdev/sse/demo/rest/cases/ProductTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void create(final TestBrowser browser)
4848

4949
final WebElement liveResponseTable = this.waitUntil(d -> d.findElement(By.className("live-responses-table")));
5050
// First element contains response body
51-
final String responseText = liveResponseTable.findElements(By.className("microlight")).get(0).getText();
51+
final String responseText = liveResponseTable.findElements(By.className("microlight")).getFirst()
52+
.getAttribute("textContent");
5253
// Id should be 2 as another product was created before
5354
Assertions.assertEquals("""
5455
{

demo/integration-tests/webapp-vaadin-it/src/test/java/software/xdev/sse/demo/vaadin/base/BaseTest.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package software.xdev.sse.demo.vaadin.base;
22

3-
import java.time.Duration;
43
import java.util.Objects;
54
import java.util.function.Consumer;
65

7-
import org.apache.hc.client5.http.config.ConnectionConfig;
8-
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
9-
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
10-
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
11-
import org.apache.hc.core5.util.Timeout;
126
import org.openqa.selenium.JavascriptExecutor;
137

148
import software.xdev.sse.demo.tci.db.DBTCI;
@@ -43,20 +37,6 @@ protected BaseTest()
4337
super(APP_INFRA_FACTORY);
4438
}
4539

46-
protected static CloseableHttpClient createDefaultHttpClient()
47-
{
48-
final Duration timeout = Duration.ofSeconds(30);
49-
return HttpClientBuilder.create()
50-
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
51-
.setDefaultConnectionConfig(ConnectionConfig.custom()
52-
.setConnectTimeout(Timeout.of(timeout))
53-
.setSocketTimeout(Timeout.of(timeout))
54-
.build())
55-
.build())
56-
.disableRedirectHandling()
57-
.build();
58-
}
59-
6040
@Override
6141
public void navigateTo(final String... additionalPathSegments)
6242
{

0 commit comments

Comments
 (0)