Skip to content

Commit

Permalink
feat: global transfer encoding filter
Browse files Browse the repository at this point in the history
  • Loading branch information
llaszkie committed Aug 25, 2023
1 parent 087798c commit c9805cc
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
17 changes: 17 additions & 0 deletions extensions/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,22 @@
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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();
}

}

0 comments on commit c9805cc

Please sign in to comment.