|
| 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