Skip to content

Commit

Permalink
merge SizeLimitHandlerTest and SizeLimitIgnoreTest
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Nov 25, 2024
1 parent 2373a1f commit ea11c33
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -64,9 +63,10 @@ public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][] {
{"jetty94", false},
{"jetty94", true},
{"ee8", false},
{"ee10", false},
{"ee8", true},
{"ee10", false},
{"ee10", true},
});
}
Expand All @@ -85,8 +85,11 @@ public SizeLimitHandlerTest(String environment, boolean httpMode) {
System.setProperty("appengine.use.HttpConnector", Boolean.toString(httpMode));
}

@Before
public void before() throws Exception {
public void start() throws Exception {
start(false);
}

public void start(boolean ignoreResponseLimit) throws Exception {
String app = "sizelimit" + environment;
copyAppToDir(app, temp.getRoot().toPath());
httpClient.start();
Expand All @@ -96,22 +99,27 @@ public void before() throws Exception {
}

@After
public void after() throws Exception
{
public void after() throws Exception {
httpClient.stop();
runtime.close();
}

@Test
public void testResponseContentBelowMaxLength() throws Exception {
start();
long contentLength = MAX_SIZE;
String url = runtime.jettyUrl("/?size=" + contentLength);
CompletableFuture<Result> completionListener = new CompletableFuture<>();
AtomicLong contentReceived = new AtomicLong();
httpClient.newRequest(url).onResponseContentAsync((response, content, callback) -> {
contentReceived.addAndGet(content.remaining());
callback.succeeded();
}).header("setCustomHeader", "true").send(completionListener::complete);
httpClient
.newRequest(url)
.onResponseContentAsync(
(response, content, callback) -> {
contentReceived.addAndGet(content.remaining());
callback.succeeded();
})
.header("setCustomHeader", "true")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.OK_200));
Expand All @@ -121,6 +129,7 @@ public void testResponseContentBelowMaxLength() throws Exception {

@Test
public void testResponseContentAboveMaxLength() throws Exception {
start();
long contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/?size=" + contentLength);

Expand Down Expand Up @@ -148,25 +157,51 @@ public void testResponseContentAboveMaxLength() throws Exception {
assertThat(received.length(), lessThanOrEqualTo(MAX_SIZE));

// No content is sent on the Jetty 9.4 runtime.
if (!"jetty94".equals(environment) && !httpMode)
if (!"jetty94".equals(environment) && !httpMode) {
assertThat(received.toString(), containsString("Response body is too large"));
}
}

@Test
public void testResponseContentAboveMaxLengthIgnored() throws Exception {
start(true);
long contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/?size=" + contentLength);
CompletableFuture<Result> completionListener = new CompletableFuture<>();
AtomicLong contentReceived = new AtomicLong();
httpClient
.newRequest(url)
.onResponseContentAsync(
(response, content, callback) -> {
contentReceived.addAndGet(content.remaining());
callback.succeeded();
})
.header("setCustomHeader", "true")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.OK_200));
assertThat(contentReceived.get(), equalTo(contentLength));
assertThat(result.getResponse().getHeaders().get("custom-header"), equalTo("true"));
}

@Test
public void testResponseContentBelowMaxLengthGzip() throws Exception {
start();
long contentLength = MAX_SIZE;
String url = runtime.jettyUrl("/?size=" + contentLength);
CompletableFuture<Result> completionListener = new CompletableFuture<>();
AtomicLong contentReceived = new AtomicLong();
httpClient.getContentDecoderFactories().clear();
httpClient.newRequest(url)
.onResponseContentAsync((response, content, callback) ->
{
contentReceived.addAndGet(content.remaining());
callback.succeeded();
})
.header(HttpHeader.ACCEPT_ENCODING, "gzip")
.send(completionListener::complete);
httpClient
.newRequest(url)
.onResponseContentAsync(
(response, content, callback) -> {
contentReceived.addAndGet(content.remaining());
callback.succeeded();
})
.header(HttpHeader.ACCEPT_ENCODING, "gzip")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getHeaders().get(HttpHeader.CONTENT_ENCODING), equalTo("gzip"));
Expand All @@ -176,6 +211,7 @@ public void testResponseContentBelowMaxLengthGzip() throws Exception {

@Test
public void testResponseContentAboveMaxLengthGzip() throws Exception {
start();
long contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/?size=" + contentLength);
httpClient.getContentDecoderFactories().clear();
Expand Down Expand Up @@ -209,16 +245,42 @@ public void testResponseContentAboveMaxLengthGzip() throws Exception {
assertThat(received.length(), lessThanOrEqualTo(MAX_SIZE));

// No content is sent on the Jetty 9.4 runtime.
if (!"jetty94".equals(environment) && !httpMode)
if (!"jetty94".equals(environment) && !httpMode) {
assertThat(received.toString(), containsString("Response body is too large"));
}
}

@Test
public void testResponseContentAboveMaxLengthGzipIgnored() throws Exception {
start(true);
long contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/?size=" + contentLength);
CompletableFuture<Result> completionListener = new CompletableFuture<>();
AtomicLong contentReceived = new AtomicLong();
httpClient.getContentDecoderFactories().clear();
httpClient
.newRequest(url)
.onResponseContentAsync(
(response, content, callback) -> {
contentReceived.addAndGet(content.remaining());
callback.succeeded();
})
.header(HttpHeader.ACCEPT_ENCODING, "gzip")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getHeaders().get(HttpHeader.CONTENT_ENCODING), equalTo("gzip"));
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.OK_200));
assertThat(contentReceived.get(), lessThan(contentLength));
}

@Test
public void testRequestContentBelowMaxLength() throws Exception {
start();
int contentLength = MAX_SIZE;

byte[] data = new byte[contentLength];
Arrays.fill(data, (byte)'X');
Arrays.fill(data, (byte) 'X');
ContentProvider content = new ByteBufferContentProvider(BufferUtil.toBuffer(data));
String url = runtime.jettyUrl("/");
ContentResponse response = httpClient.newRequest(url).content(content).send();
Expand All @@ -230,21 +292,24 @@ public void testRequestContentBelowMaxLength() throws Exception {

@Test
public void testRequestContentAboveMaxLength() throws Exception {
start();
int contentLength = MAX_SIZE + 1;

CompletableFuture<Result> completionListener = new CompletableFuture<>();
byte[] data = new byte[contentLength];
Arrays.fill(data, (byte)'X');
Arrays.fill(data, (byte) 'X');
Utf8StringBuilder received = new Utf8StringBuilder();
ContentProvider content = new ByteBufferContentProvider(BufferUtil.toBuffer(data));
String url = runtime.jettyUrl("/");
httpClient.newRequest(url).content(content)
.onResponseContentAsync((response, content1, callback) ->
{
received.append(content1);
callback.succeeded();
})
.send(completionListener::complete);
httpClient
.newRequest(url)
.content(content)
.onResponseContentAsync(
(response, content1, callback) -> {
received.append(content1);
callback.succeeded();
})
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.PAYLOAD_TOO_LARGE_413));
Expand All @@ -257,23 +322,26 @@ public void testRequestContentAboveMaxLength() throws Exception {

@Test
public void testRequestContentBelowMaxLengthGzip() throws Exception {
start();
int contentLength = MAX_SIZE;

CompletableFuture<Result> completionListener = new CompletableFuture<>();
byte[] data = new byte[contentLength];
Arrays.fill(data, (byte)'X');
Arrays.fill(data, (byte) 'X');
Utf8StringBuilder received = new Utf8StringBuilder();
ContentProvider content = new InputStreamContentProvider(gzip(data));

String url = runtime.jettyUrl("/");
httpClient.newRequest(url).content(content)
.onResponseContentAsync((response, content1, callback) ->
{
received.append(content1);
callback.succeeded();
})
.header(HttpHeader.CONTENT_ENCODING, "gzip")
.send(completionListener::complete);
httpClient
.newRequest(url)
.content(content)
.onResponseContentAsync(
(response, content1, callback) -> {
received.append(content1);
callback.succeeded();
})
.header(HttpHeader.CONTENT_ENCODING, "gzip")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.OK_200));
Expand All @@ -282,23 +350,26 @@ public void testRequestContentBelowMaxLengthGzip() throws Exception {

@Test
public void testRequestContentAboveMaxLengthGzip() throws Exception {
start();
int contentLength = MAX_SIZE + 1;

CompletableFuture<Result> completionListener = new CompletableFuture<>();
byte[] data = new byte[contentLength];
Arrays.fill(data, (byte)'X');
Arrays.fill(data, (byte) 'X');
Utf8StringBuilder received = new Utf8StringBuilder();
ContentProvider content = new InputStreamContentProvider(gzip(data));

String url = runtime.jettyUrl("/");
httpClient.newRequest(url).content(content)
.onResponseContentAsync((response, content1, callback) ->
{
httpClient
.newRequest(url)
.content(content)
.onResponseContentAsync(
(response, content1, callback) -> {
received.append(content1);
callback.succeeded();
})
.header(HttpHeader.CONTENT_ENCODING, "gzip")
.send(completionListener::complete);
.header(HttpHeader.CONTENT_ENCODING, "gzip")
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
assertThat(result.getResponse().getStatus(), equalTo(HttpStatus.PAYLOAD_TOO_LARGE_413));
Expand All @@ -311,6 +382,7 @@ public void testRequestContentAboveMaxLengthGzip() throws Exception {

@Test
public void testResponseContentLengthHeader() throws Exception {
start();
long contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/?setContentLength=" + contentLength);
httpClient.getContentDecoderFactories().clear();
Expand All @@ -319,28 +391,31 @@ public void testResponseContentLengthHeader() throws Exception {
assertThat(response.getStatus(), equalTo(HttpStatus.INTERNAL_SERVER_ERROR_500));

// No content is sent on the Jetty 9.4 runtime.
if (!"jetty94".equals(environment))
if (!"jetty94".equals(environment)) {
assertThat(response.getContentAsString(), containsString("Response body is too large"));
}
}

@Test
public void testRequestContentLengthHeader() throws Exception {
start();
CompletableFuture<Result> completionListener = new CompletableFuture<>();
DeferredContentProvider provider = new DeferredContentProvider(ByteBuffer.allocate(1));
int contentLength = MAX_SIZE + 1;
String url = runtime.jettyUrl("/");
Utf8StringBuilder received = new Utf8StringBuilder();
httpClient.newRequest(url)
.header(HttpHeader.CONTENT_LENGTH, Long.toString(contentLength))
.header("foo", "bar")
.content(provider)
.onResponseContentAsync((response, content, callback) ->
{
httpClient
.newRequest(url)
.header(HttpHeader.CONTENT_LENGTH, Long.toString(contentLength))
.header("foo", "bar")
.content(provider)
.onResponseContentAsync(
(response, content, callback) -> {
received.append(content);
callback.succeeded();
provider.close();
})
.send(completionListener::complete);
.send(completionListener::complete);

Result result = completionListener.get(5, TimeUnit.SECONDS);
Response response = result.getResponse();
Expand All @@ -358,11 +433,9 @@ private RuntimeContext<?> runtimeContext() throws Exception {
return RuntimeContext.create(config);
}

private void assertEnvironment() throws Exception
{
private void assertEnvironment() throws Exception {
String match;
switch (environment)
{
switch (environment) {
case "jetty94":
match = "org.eclipse.jetty.server.Request";
break;
Expand Down
Loading

0 comments on commit ea11c33

Please sign in to comment.