Skip to content

Commit

Permalink
Adding compressPath.exclude tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Sep 19, 2024
1 parent 381d030 commit c92e9b1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public String getCompressionEncoding(List<String> requestAcceptEncoding, Request
if (!isMethodSupported(request.getMethod()))
return null;

if (!compressPaths.test(pathInContext))
return null;

return matchedEncoding;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
package org.eclipse.jetty.compression;

import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.jetty.client.ContentResponse;
import org.eclipse.jetty.client.HttpClient;
Expand All @@ -36,11 +40,13 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class CompressionHandlerTest extends AbstractCompressionTest
{
Expand All @@ -61,6 +67,22 @@ public void stopAll()
LifeCycle.stop(server);
}

public static Stream<Arguments> pathExcludesCases()
{
List<Arguments> cases = new ArrayList<>();
List<String> texts = List.of("texts/quotes.txt", "texts/long.txt", "texts/logo.svg");

for (Class<? extends Compression> compressionClass : compressions())
{
cases.add(Arguments.of(compressionClass, "texts/quotes.txt", "text/plain;charset=utf-8", "/path/to/quotes.txt", true));
cases.add(Arguments.of(compressionClass, "texts/logo.svg", "image/svg+xml", "/path/to/logo.svg", true));
cases.add(Arguments.of(compressionClass, "texts/long.txt", "text/plain;charset=utf-8", "/path/to/long.txt", true));
cases.add(Arguments.of(compressionClass, "images/logo.png", "image/png", "/images/logo.png", false));
}

return cases.stream();
}

/**
* Testing how CompressionHandler acts with a single compression implementation added.
* Using all defaults for both the compression impl, and the CompressionHandler.
Expand All @@ -80,7 +102,7 @@ public void testDefaultCompressionConfiguration(Class<Compression> compressionCl
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.setStatus(200);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "texts/plain;charset=utf-8");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain;charset=utf-8");
Content.Sink.write(response, true, message, callback);
return true;
}
Expand Down Expand Up @@ -126,7 +148,7 @@ public void testDefaultCompressionConfigurationText(Class<Compression> compressi
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.setStatus(200);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "texts/plain;charset=utf-8");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain;charset=utf-8");
Content.Sink.write(response, true, resourceBody, callback);
return true;
}
Expand All @@ -152,81 +174,96 @@ public boolean handle(Request request, Response response, Callback callback) thr
assertThat(content, is(resourceBody));
}

/**
* Testing how CompressionHandler acts with a single compression implementation added.
* Using all defaults for both the compression impl, and the CompressionHandler.
*/
@ParameterizedTest
@MethodSource("textInputs")
public void testExcludeCompressionConfigurationText(Class<Compression> compressionClass, String resourceName) throws Exception
@Test
public void testDefaultConfiguration() throws Exception
{
newCompression(compressionClass);
Path resourcePath = MavenPaths.findTestResourceFile(resourceName);
String resourceBody = Files.readString(resourcePath, UTF_8);

CompressionHandler compressionHandler = new CompressionHandler();
compressionHandler.addCompression(compression);
CompressionConfig config = CompressionConfig.builder().build();

compressionHandler.putConfiguration("/", config);
compressionHandler.setHandler(new Handler.Abstract()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.setStatus(200);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "texts/plain;charset=utf-8");
Content.Sink.write(response, true, resourceBody, callback);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain;charset=utf-8");
Content.Sink.write(response, true, "Hello World", callback);
return true;
}
});

startServer(compressionHandler);

URI serverURI = server.getURI();
client.getContentDecoderFactories().clear();

ContentResponse response = client.newRequest(serverURI.getHost(), serverURI.getPort())
.method(HttpMethod.GET)
.headers((headers) ->
{
headers.put(HttpHeader.ACCEPT_ENCODING, compression.getEncodingName());
})
.path("/textbody")
.path("/hello")
.send();
dumpResponse(response);
assertThat(response.getStatus(), is(200));
assertThat(response.getHeaders().get(HttpHeader.CONTENT_ENCODING), is(compression.getEncodingName()));
String content = new String(decompress(response.getContent()), UTF_8);
assertThat(content, is(resourceBody));
assertThat(response.getContentAsString(), is("Hello World"));
}

@Test
public void testDefaultConfiguration() throws Exception
/**
* Testing how CompressionHandler acts with a single compression implementation added.
* Configuration is excluding {@code *.png} paths.
*/
@ParameterizedTest
@MethodSource("pathExcludesCases")
public void testPathExcludeCompressionConfigurationText(Class<Compression> compressionClass,
String resourceName,
String resourceContentType,
String requestedPath,
boolean expectedIsCompressed) throws Exception
{
newCompression(compressionClass);
Path resourcePath = MavenPaths.findTestResourceFile(resourceName);
byte[] resourceBody = Files.readAllBytes(resourcePath);

CompressionHandler compressionHandler = new CompressionHandler();
compressionHandler.addCompression(compression);
CompressionConfig config = CompressionConfig.builder()
.compressPathExclude("*.png")
.build();

compressionHandler.putConfiguration("/", config);
compressionHandler.setHandler(new Handler.Abstract()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.setStatus(200);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "texts/plain;charset=utf-8");
Content.Sink.write(response, true, "Hello World", callback);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, resourceContentType);
response.write(true, ByteBuffer.wrap(resourceBody), callback);
return true;
}
});

startServer(compressionHandler);

URI serverURI = server.getURI();
client.getContentDecoderFactories().clear();

ContentResponse response = client.newRequest(serverURI.getHost(), serverURI.getPort())
.method(HttpMethod.GET)
.path("/hello")
.headers((headers) ->
{
headers.put(HttpHeader.ACCEPT_ENCODING, compression.getEncodingName());
})
.path(requestedPath)
.send();
dumpResponse(response);
assertThat(response.getStatus(), is(200));
assertThat(response.getContentAsString(), is("Hello World"));
if (expectedIsCompressed)
{
assertThat(response.getHeaders().get(HttpHeader.CONTENT_ENCODING), is(compression.getEncodingName()));
byte[] content = decompress(response.getContent());
assertThat(content, is(resourceBody));
}
else
{
assertFalse(response.getHeaders().contains(HttpHeader.CONTENT_ENCODING));
byte[] content = response.getContent();
assertThat(content, is(resourceBody));
}
}

private void dumpResponse(org.eclipse.jetty.client.Response response)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c92e9b1

Please sign in to comment.