Issue #4711 - Reset trailers.

Test code cleanup.
Added test for Response.reset().

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2020-03-25 15:08:43 +01:00
parent f4da9760fd
commit 1a3f39eef1
1 changed files with 59 additions and 26 deletions

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -40,7 +41,6 @@ import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
@ -50,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class HttpTrailersTest extends AbstractTest<TransportScenario>
{
@ -80,13 +81,11 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
{
String trailerName = "Trailer";
String trailerValue = "value";
scenario.start(new AbstractHandler()
scenario.start(new EmptyServerHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
jettyRequest.setHandled(true);
// Read the content first.
ServletInputStream input = jettyRequest.getInputStream();
while (true)
@ -119,13 +118,11 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
public void testEmptyRequestTrailers(Transport transport) throws Exception
{
init(transport);
scenario.start(new AbstractHandler()
scenario.start(new EmptyServerHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
jettyRequest.setHandled(true);
// Read the content first.
ServletInputStream input = jettyRequest.getInputStream();
while (true)
@ -166,16 +163,15 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
private void testResponseTrailers(byte[] content) throws Exception
{
final AtomicBoolean once = new AtomicBoolean(false);
AtomicBoolean once = new AtomicBoolean();
String trailerName = "Trailer";
String trailerValue = "value";
scenario.start(new AbstractHandler()
scenario.start(new EmptyServerHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
jettyRequest.setHandled(true);
Response jettyResponse = (Response)response;
Response jettyResponse = jettyRequest.getResponse();
if (once.compareAndSet(false, true))
{
@ -211,7 +207,7 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
assertEquals(HttpStatus.OK_200, response.getStatus());
assertNull(failure.get());
// subsequent requests should not have trailers
// Subsequent requests should not have trailers.
response = scenario.client.newRequest(scenario.newURI())
.onResponseSuccess(r ->
{
@ -237,16 +233,13 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
public void testEmptyResponseTrailers(Transport transport) throws Exception
{
init(transport);
scenario.start(new AbstractHandler()
scenario.start(new EmptyServerHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
jettyRequest.setHandled(true);
HttpFields trailers = new HttpFields();
Response jettyResponse = (Response)response;
Response jettyResponse = jettyRequest.getResponse();
jettyResponse.setTrailers(() -> trailers);
}
});
@ -282,17 +275,15 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
String trailerName = "Trailer";
String trailerValue = "value";
init(transport);
scenario.start(new AbstractHandler()
scenario.start(new EmptyServerHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
jettyRequest.setHandled(true);
HttpFields trailers = new HttpFields();
trailers.put(trailerName, trailerValue);
Response jettyResponse = (Response)response;
Response jettyResponse = jettyRequest.getResponse();
jettyResponse.setTrailers(() -> trailers);
// Write a large content
@ -328,4 +319,46 @@ public class HttpTrailersTest extends AbstractTest<TransportScenario>
assertNotNull(trailers);
assertEquals(trailerValue, trailers.get(trailerName));
}
@ParameterizedTest
@ArgumentsSource(TransportProvider.class)
public void testResponseResetAlsoResetsTrailers(Transport transport) throws Exception
{
init(transport);
scenario.start(new EmptyServerHandler()
{
@Override
protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
Response jettyResponse = jettyRequest.getResponse();
HttpFields trailers = new HttpFields();
trailers.put("name", "value");
jettyResponse.setTrailers(() -> trailers);
// Fill some other response field.
response.setHeader("name", "value");
response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
response.getWriter();
// Reset the response.
response.reset();
// Must not throw because we have called
// getWriter() above, since we have reset().
response.getOutputStream();
}
});
CountDownLatch latch = new CountDownLatch(1);
scenario.client.newRequest(scenario.newURI())
.timeout(5, TimeUnit.SECONDS)
.send(result ->
{
HttpResponse response = (HttpResponse)result.getResponse();
assertEquals(HttpStatus.OK_200, response.getStatus());
assertNull(response.getTrailers());
assertNull(response.getHeaders().get("name"));
latch.countDown();
});
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}