Skip to content

Commit 2fdd8c5

Browse files
newtorka-dMatKuhr
authored
Extend Apache HttpClient 5 test assertions for actual headers (2) (#640)
Co-authored-by: Alexander Dümont <[email protected]> Co-authored-by: Matthias Kuhr <[email protected]>
1 parent 3878230 commit 2fdd8c5

File tree

3 files changed

+131
-66
lines changed

3 files changed

+131
-66
lines changed

cloudplatform/connectivity-apache-httpclient5/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,10 @@
128128
<artifactId>testutil</artifactId>
129129
<scope>test</scope>
130130
</dependency>
131+
<dependency>
132+
<groupId>org.junit.jupiter</groupId>
133+
<artifactId>junit-jupiter-params</artifactId>
134+
<scope>test</scope>
135+
</dependency>
131136
</dependencies>
132137
</project>

cloudplatform/connectivity-apache-httpclient5/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/ApacheHttpClient5FactoryBuilderTest.java

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@
44

55
package com.sap.cloud.sdk.cloudplatform.connectivity;
66

7-
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade.DISABLED;
8-
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade.ENABLED;
9-
import static com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination.builder;
10-
import static com.sap.cloud.sdk.cloudplatform.connectivity.ProxyType.INTERNET;
11-
import static com.sap.cloud.sdk.cloudplatform.connectivity.ProxyType.ON_PREMISE;
12-
import static org.assertj.core.api.Assertions.as;
13-
import static org.assertj.core.api.Assertions.assertThat;
147
import static org.assertj.core.api.Assertions.assertThatNoException;
15-
import static org.assertj.core.api.InstanceOfAssertFactories.type;
168

17-
import javax.annotation.Nonnull;
18-
19-
import org.apache.hc.client5.http.config.Configurable;
209
import org.junit.jupiter.api.Test;
2110

2211
class ApacheHttpClient5FactoryBuilderTest
@@ -28,59 +17,4 @@ void testBuilderContainsOptionalParametersOnly()
2817
// make sure we can build a new factory instance without supplying any parameters
2918
assertThatNoException().isThrownBy(() -> new ApacheHttpClient5FactoryBuilder().build());
3019
}
31-
32-
@Test
33-
void testTlsUpgradeToggle()
34-
{
35-
var service = "https://servive";
36-
var proxy = ProxyConfiguration.of("https://proxy");
37-
38-
var destInternet = builder(service).trustAllCertificates().build();
39-
var destOnPremise = builder(service).proxyType(ON_PREMISE).proxyConfiguration(proxy).buildInternal();
40-
var destProxy = builder(service).trustAllCertificates().proxyType(INTERNET).proxyConfiguration(proxy).build();
41-
var destTlsVersion = builder(service).trustAllCertificates().tlsVersion("TLSv1.1").build();
42-
43-
ApacheHttpClient5Factory sut;
44-
45-
// force upgrade=true
46-
sut = new ApacheHttpClient5FactoryBuilder().tlsUpgrade(ENABLED).build();
47-
assertProtocolUpgradeEnabled(sut, destInternet);
48-
assertProtocolUpgradeEnabled(sut, destOnPremise);
49-
assertProtocolUpgradeEnabled(sut, destProxy);
50-
assertProtocolUpgradeEnabled(sut, destTlsVersion);
51-
52-
// force upgrade=false
53-
sut = new ApacheHttpClient5FactoryBuilder().tlsUpgrade(DISABLED).build();
54-
assertProtocolUpgradeDisabled(sut, destInternet);
55-
assertProtocolUpgradeDisabled(sut, destOnPremise);
56-
assertProtocolUpgradeDisabled(sut, destProxy);
57-
assertProtocolUpgradeDisabled(sut, destTlsVersion);
58-
59-
// default
60-
sut = new ApacheHttpClient5FactoryBuilder().build();
61-
assertProtocolUpgradeEnabled(sut, destInternet);
62-
assertProtocolUpgradeDisabled(sut, destOnPremise);
63-
assertProtocolUpgradeEnabled(sut, destProxy);
64-
assertProtocolUpgradeDisabled(sut, destTlsVersion);
65-
}
66-
67-
private void assertProtocolUpgradeEnabled(
68-
@Nonnull final ApacheHttpClient5Factory factory,
69-
@Nonnull final DefaultHttpDestination dest )
70-
{
71-
assertThat(factory.createHttpClient(dest))
72-
.isNotNull()
73-
.extracting("httpClient", as(type(Configurable.class)))
74-
.satisfies(client -> assertThat(client.getConfig().isProtocolUpgradeEnabled()).isTrue());
75-
}
76-
77-
private void assertProtocolUpgradeDisabled(
78-
@Nonnull final ApacheHttpClient5Factory factory,
79-
@Nonnull final DefaultHttpDestination dest )
80-
{
81-
assertThat(factory.createHttpClient(dest))
82-
.isNotNull()
83-
.extracting("httpClient", as(type(Configurable.class)))
84-
.satisfies(client -> assertThat(client.getConfig().isProtocolUpgradeEnabled()).isFalse());
85-
}
8620
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved.
3+
*/
4+
5+
package com.sap.cloud.sdk.cloudplatform.connectivity;
6+
7+
import static java.util.function.Predicate.not;
8+
9+
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
10+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
11+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
12+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
13+
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
14+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
15+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
16+
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade;
17+
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade.AUTOMATIC;
18+
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade.DISABLED;
19+
import static com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5FactoryBuilder.TlsUpgrade.ENABLED;
20+
import static com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination.builder;
21+
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.function.Function;
25+
26+
import javax.annotation.Nonnull;
27+
28+
import org.apache.hc.client5.http.classic.methods.HttpGet;
29+
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.FieldSource;
32+
33+
import com.github.tomakehurst.wiremock.http.Request;
34+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
35+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
36+
import com.github.tomakehurst.wiremock.matching.MatchResult;
37+
import com.github.tomakehurst.wiremock.matching.ValueMatcher;
38+
import com.github.tomakehurst.wiremock.stubbing.SubEvent;
39+
40+
import lombok.RequiredArgsConstructor;
41+
import lombok.SneakyThrows;
42+
import lombok.Value;
43+
44+
@WireMockTest
45+
class ApacheHttpClient5HeaderTest
46+
{
47+
@RequiredArgsConstructor
48+
enum TestDestination
49+
{
50+
INTERNET(url -> builder(url).build()),
51+
PROXY(url -> builder(url).proxyType(ProxyType.INTERNET).proxyConfiguration(ProxyConfiguration.of(url)).build()),
52+
ON_PREMISE(
53+
url -> builder(url)
54+
.proxyType(ProxyType.ON_PREMISE)
55+
.proxyConfiguration(ProxyConfiguration.of(url))
56+
.buildInternal()),
57+
TLS_VERSION(url -> builder(url).tlsVersion("TLSv1.1").build());
58+
59+
private final Function<String, HttpDestination> destinationBuilder;
60+
}
61+
62+
@RequiredArgsConstructor
63+
enum TestAssertion
64+
{
65+
DEFAULT(List.of("Connection", "Host", "User-Agent", "Accept-Encoding"), Map.of()),
66+
UPGRADE(
67+
List.of("Connection", "Host", "User-Agent", "Accept-Encoding", "Upgrade"),
68+
Map.of("Connection", "Upgrade", "Upgrade", "TLS/1.2"));
69+
70+
private final List<String> headersAllowed;
71+
private final Map<String, String> headersRequired;
72+
}
73+
74+
@Value
75+
static class TestCase
76+
{
77+
TestDestination destination;
78+
TlsUpgrade tlsFlag;
79+
TestAssertion assertion;
80+
}
81+
82+
@SuppressWarnings( "unused" )
83+
private static final TestCase[] TEST_CASES =
84+
{
85+
// Forced HTTP/TLS upgrade
86+
new TestCase(TestDestination.INTERNET, ENABLED, TestAssertion.UPGRADE),
87+
new TestCase(TestDestination.PROXY, ENABLED, TestAssertion.UPGRADE),
88+
new TestCase(TestDestination.ON_PREMISE, ENABLED, TestAssertion.UPGRADE),
89+
new TestCase(TestDestination.TLS_VERSION, ENABLED, TestAssertion.UPGRADE),
90+
91+
// Disabled HTTP/TLS upgrade
92+
new TestCase(TestDestination.INTERNET, DISABLED, TestAssertion.DEFAULT),
93+
new TestCase(TestDestination.PROXY, DISABLED, TestAssertion.DEFAULT),
94+
new TestCase(TestDestination.ON_PREMISE, DISABLED, TestAssertion.DEFAULT),
95+
new TestCase(TestDestination.TLS_VERSION, DISABLED, TestAssertion.DEFAULT),
96+
97+
// Automatic HTTP/TLS upgrade
98+
new TestCase(TestDestination.INTERNET, AUTOMATIC, TestAssertion.UPGRADE),
99+
new TestCase(TestDestination.PROXY, AUTOMATIC, TestAssertion.UPGRADE),
100+
new TestCase(TestDestination.ON_PREMISE, AUTOMATIC, TestAssertion.DEFAULT),
101+
new TestCase(TestDestination.TLS_VERSION, AUTOMATIC, TestAssertion.DEFAULT) };
102+
103+
@SneakyThrows
104+
@ParameterizedTest
105+
@FieldSource( "TEST_CASES" )
106+
void testHeader( @Nonnull final TestCase testCase, @Nonnull final WireMockRuntimeInfo server )
107+
{
108+
stubFor(get(anyUrl()).willReturn(ok()));
109+
110+
var sut = new ApacheHttpClient5FactoryBuilder().tlsUpgrade(testCase.tlsFlag).build();
111+
var dest = testCase.destination.destinationBuilder.apply(server.getHttpBaseUrl());
112+
sut.createHttpClient(dest).execute(new HttpGet("/foo"), new BasicHttpClientResponseHandler());
113+
114+
var request = getRequestedFor(anyUrl()).andMatching(allowedHeaders(testCase.assertion.headersAllowed));
115+
testCase.assertion.headersRequired.forEach(( k, v ) -> request.withHeader(k, equalTo(v)));
116+
verify(request);
117+
}
118+
119+
static ValueMatcher<Request> allowedHeaders( @Nonnull final List<String> headers )
120+
{
121+
return req -> {
122+
var excess = req.getAllHeaderKeys().stream().filter(not(headers::contains)).map(SubEvent::error).toList();
123+
return excess.isEmpty() ? MatchResult.exactMatch() : MatchResult.noMatch(excess);
124+
};
125+
}
126+
}

0 commit comments

Comments
 (0)