Skip to content

Commit e7ea5d5

Browse files
Alex's review
1 parent 5a72fa0 commit e7ea5d5

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

datamodel/openapi/openapi-core-apache/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,15 @@
9494
<artifactId>junit-jupiter-params</artifactId>
9595
<scope>test</scope>
9696
</dependency>
97+
<dependency>
98+
<groupId>org.mockito</groupId>
99+
<artifactId>mockito-core</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>io.vavr</groupId>
104+
<artifactId>vavr</artifactId>
105+
<scope>test</scope>
106+
</dependency>
97107
</dependencies>
98-
</project>
108+
</project>

datamodel/openapi/openapi-core-apache/src/main/java/com/sap/cloud/sdk/services/openapi/apache/apiclient/ApiClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static com.sap.cloud.sdk.services.openapi.apache.apiclient.DefaultApiResponseHandler.isJsonMime;
1616
import static lombok.AccessLevel.PRIVATE;
17+
import static org.apache.hc.core5.http.HttpHeaders.CONTENT_ENCODING;
1718

1819
import java.io.ByteArrayOutputStream;
1920
import java.io.File;
@@ -492,7 +493,7 @@ public <T> T invokeAPI(
492493
if( body != null || !formParams.isEmpty() ) {
493494
if( isBodyAllowed(Method.valueOf(method)) ) {
494495
// Add entity if we have content and a valid method
495-
builder.setEntity(serialize(body, formParams, contentTypeObj, headerParams.get("Content-Encoding")));
496+
builder.setEntity(serialize(body, formParams, contentTypeObj, headerParams));
496497
} else {
497498
throw new OpenApiRequestException("method " + method + " does not support a request body");
498499
}
@@ -520,6 +521,8 @@ public <T> T invokeAPI(
520521
* Content type
521522
* @param formParams
522523
* Form parameters
524+
* @param headerParams
525+
* Header parameters, used to check content encoding for JSON serialization
523526
* @return Object
524527
* @throws OpenApiRequestException
525528
* API exception
@@ -529,12 +532,12 @@ private HttpEntity serialize(
529532
@Nullable final Object body,
530533
@Nonnull final Map<String, Object> formParams,
531534
@Nonnull final ContentType contentType,
532-
@Nullable final String contentEncoding )
535+
@Nonnull final Map<String, String> headerParams )
533536
throws OpenApiRequestException
534537
{
535538
final String mimeType = contentType.getMimeType();
536539
if( isJsonMime(mimeType) ) {
537-
return serializeJson(body, contentType, contentEncoding);
540+
return serializeJson(body, contentType, headerParams);
538541
} else if( mimeType.equals(ContentType.MULTIPART_FORM_DATA.getMimeType()) ) {
539542
return serializeMultipart(formParams, contentType);
540543
} else if( mimeType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType()) ) {
@@ -551,18 +554,22 @@ private HttpEntity serialize(
551554
private HttpEntity serializeJson(
552555
@Nullable final Object body,
553556
@Nonnull final ContentType contentType,
554-
@Nullable final String contentEncoding )
557+
@Nonnull final Map<String, String> headerParams )
555558
throws OpenApiRequestException
556559
{
557-
if( "gzip".equals(contentEncoding) ) {
560+
if( "gzip".equalsIgnoreCase(headerParams.get(CONTENT_ENCODING))
561+
|| "gzip".equalsIgnoreCase(headerParams.get(CONTENT_ENCODING.toLowerCase())) ) {
558562
val outputStream = new ByteArrayOutputStream();
559563
try( val gzip = new GZIPOutputStream(outputStream) ) {
560564
gzip.write(objectMapper.writeValueAsBytes(body));
561565
}
562566
catch( IOException e ) {
563-
throw new OpenApiRequestException(e);
567+
throw new OpenApiRequestException("Failed to GZIP compress request body", e);
564568
}
565-
return new ByteArrayEntity(outputStream.toByteArray(), contentType.withCharset(StandardCharsets.UTF_8));
569+
return new ByteArrayEntity(
570+
outputStream.toByteArray(),
571+
contentType.withCharset(StandardCharsets.UTF_8),
572+
"gzip");
566573
}
567574
try {
568575
return new StringEntity(

datamodel/openapi/openapi-core-apache/src/test/java/com/sap/cloud/sdk/services/openapi/apache/apiclient/ApacheApiClientResponseHandlingTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@
1010
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
1111
import static org.assertj.core.api.Assertions.assertThat;
1212

13+
import java.nio.charset.StandardCharsets;
1314
import java.util.ArrayList;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.concurrent.atomic.AtomicReference;
19+
import java.util.zip.GZIPInputStream;
1820

21+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
22+
import org.apache.hc.core5.http.protocol.HttpContext;
1923
import org.junit.jupiter.api.Test;
24+
import org.mockito.Mockito;
2025

2126
import com.fasterxml.jackson.annotation.JsonProperty;
2227
import com.fasterxml.jackson.core.type.TypeReference;
2328
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
2429
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
30+
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
2531
import com.sap.cloud.sdk.services.openapi.apache.core.OpenApiRequestException;
2632
import com.sap.cloud.sdk.services.openapi.apache.core.OpenApiResponse;
2733

34+
import io.vavr.control.Try;
2835
import lombok.Data;
36+
import lombok.SneakyThrows;
2937

3038
@WireMockTest
3139
class ApacheApiClientResponseHandlingTest
@@ -88,14 +96,19 @@ void testCaseInsensitiveHeaderLookup( final WireMockRuntimeInfo wmInfo )
8896
}
8997

9098
@Test
99+
@SneakyThrows
91100
void testGzipEncodedPayload( final WireMockRuntimeInfo wmInfo )
92101
{
93102
stubFor(post(urlEqualTo(TEST_POST_PATH)).willReturn(aResponse().withStatus(200).withBody(TEST_RESPONSE_BODY)));
94103

95-
final ApiClient apiClient = ApiClient.create().withBasePath(wmInfo.getHttpBaseUrl());
96-
104+
final CloseableHttpClient client = Mockito.spy((CloseableHttpClient) ApacheHttpClient5Accessor.getHttpClient());
105+
final ApiClient apiClient = ApiClient.fromHttpClient(client).withBasePath(wmInfo.getHttpBaseUrl());
97106
final TestPostApi api = new TestPostApi(apiClient);
98107
final TestResponse result = api.executeGzipRequest();
108+
Mockito.verify(client, Mockito.times(1)).execute(Mockito.argThat(request -> {
109+
final byte[] c = Try.of(() -> new GZIPInputStream(request.getEntity().getContent()).readAllBytes()).get();
110+
return new String(c, StandardCharsets.UTF_8).contains("test payload");
111+
}), Mockito.any(HttpContext.class), Mockito.any());
99112

100113
assertThat(result).isNotNull();
101114
assertThat(result.getMessage()).isEqualTo("success");

0 commit comments

Comments
 (0)