From c9805cc655021e2fd52ff248aa01d113bc1919d7 Mon Sep 17 00:00:00 2001 From: "lukasz.laszkiewicz" Date: Fri, 25 Aug 2023 14:09:09 +0200 Subject: [PATCH] feat: global transfer encoding filter see: https://github.com/quarkusio/quarkus/issues/29059 --- extensions/rest/pom.xml | 17 +++++ .../interceptor/TransferEncodingFilter.java | 9 ++- .../TransferEncodingFilterTest.java | 74 +++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 extensions/rest/src/test/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilterTest.java diff --git a/extensions/rest/pom.xml b/extensions/rest/pom.xml index 3a1a8a8..8a60b67 100644 --- a/extensions/rest/pom.xml +++ b/extensions/rest/pom.xml @@ -32,5 +32,22 @@ org.jboss.spec.javax.ws.rs jboss-jaxrs-api_2.1_spec + + io.quarkus + quarkus-junit5 + test + + + org.mockito + mockito-core + 5.3.1 + test + + + org.mockito + mockito-junit-jupiter + 4.6.1 + test + diff --git a/extensions/rest/src/main/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilter.java b/extensions/rest/src/main/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilter.java index f7bfb86..7dfa4c6 100644 --- a/extensions/rest/src/main/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilter.java +++ b/extensions/rest/src/main/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilter.java @@ -3,11 +3,18 @@ import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.client.ClientResponseContext; import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.ext.Provider; +@Provider public class TransferEncodingFilter implements ClientResponseFilter { + public static final String TRANSFER_ENCODING = "Transfer-Encoding"; + @Override public void filter(ClientRequestContext clientRequestContext, ClientResponseContext clientResponseContext) { - clientResponseContext.getHeaders().remove("Transfer-Encoding"); + String teHeaderValue = clientResponseContext.getHeaderString(TRANSFER_ENCODING); + if (teHeaderValue != null && teHeaderValue.contains("chunked")) { + clientResponseContext.getHeaders().remove(TRANSFER_ENCODING); + } } } diff --git a/extensions/rest/src/test/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilterTest.java b/extensions/rest/src/test/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilterTest.java new file mode 100644 index 0000000..d8de8fe --- /dev/null +++ b/extensions/rest/src/test/java/org/tkit/quarkus/rs/interceptor/TransferEncodingFilterTest.java @@ -0,0 +1,74 @@ +package org.tkit.quarkus.rs.interceptor; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class TransferEncodingFilterTest { + + @Mock + ClientResponseContext clientResponseContextMock; + + TransferEncodingFilter transferEncodingFilter = new TransferEncodingFilter(); + + @AfterEach + void mockReset() { + Mockito.reset(clientResponseContextMock); + } + + @Test + void filterChunked() { + // given + Mockito.when(clientResponseContextMock.getHeaderString("Transfer-Encoding")).thenReturn("chunked"); + + MultivaluedMap headers = new MultivaluedHashMap<>(); + headers.add("Transfer-Encoding","chunked"); + Mockito.when(clientResponseContextMock.getHeaders()).thenReturn(headers); + + // when + transferEncodingFilter.filter(null, clientResponseContextMock); + + // then + assertFalse(headers.containsKey("Transfer-Encoding")); + } + + @Test + void filterOther() { + // given + Mockito.when(clientResponseContextMock.getHeaderString("Transfer-Encoding")).thenReturn("compress"); + + MultivaluedMap headers = new MultivaluedHashMap<>(); + headers.add("Transfer-Encoding","compress"); + Mockito.when(clientResponseContextMock.getHeaders()).thenReturn(headers); + + // when + transferEncodingFilter.filter(null, clientResponseContextMock); + + // then + assertTrue(headers.containsKey("Transfer-Encoding")); + assertEquals("compress", headers.getFirst("Transfer-Encoding")); + } + + @Test + void filterNoHeader() { + // given + Mockito.when(clientResponseContextMock.getHeaderString("Transfer-Encoding")).thenReturn(null); + + // when + transferEncodingFilter.filter(null, clientResponseContextMock); + + // then + Mockito.verify(clientResponseContextMock, Mockito.never()).getHeaders(); + } + +} \ No newline at end of file