Skip to content

Commit

Permalink
Undisable and fix ee9 ResponseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Nov 13, 2024
1 parent 60c2b29 commit 887e78a
Showing 1 changed file with 57 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -108,6 +107,8 @@
// @checkstyle-disable-check : AvoidEscapedUnicodeCharactersCheck
public class ResponseTest
{
private static final String HOST = "myhost";
private static final int PORT = 8888;
private Server _server;
private ContextHandler _context;
private HttpChannel _channel;
Expand All @@ -127,7 +128,7 @@ public void init() throws Exception
_context.setHandler(new DumpHandler());
_server.start();

SocketAddress local = InetSocketAddress.createUnresolved("myhost", 8888);
SocketAddress local = InetSocketAddress.createUnresolved(HOST, PORT);
EndPoint endPoint = new ByteArrayEndPoint(scheduler, 5000)
{
@Override
Expand Down Expand Up @@ -402,51 +403,51 @@ public void testLocaleFormat() throws Exception
}

@Test
@Disabled // TODO
public void testResponseCharacterEncoding() throws Exception
{
//test setting the default response character encoding
//test that without a default or any content type, use iso-8859-1
Response response = getResponse();
assertThat("utf-16", Matchers.equalTo(response.getCharacterEncoding()));
assertThat("iso-8859-1", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));
//getWriter should not have modified character encoding
response.getWriter();
assertThat("iso-8859-1", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));

response = getResponse();
//test setting the default response character encoding
_server.stop();
ContextHandler context = new ContextHandler();
context.setDefaultResponseCharacterEncoding("utf-16");
context.setHandler(new DumpHandler());
_server.setHandler(context);
_server.start();

response = getResponse(context);

assertThat("utf-16", Matchers.equalToIgnoringCase(context.getServletContext().getResponseCharacterEncoding()));
assertThat("utf-16", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));

//test that explicit overrides default
response = getResponse();
response = getResponse(context);
response.setCharacterEncoding("ascii");
assertThat("ascii", Matchers.equalTo(response.getCharacterEncoding()));
assertThat("ascii", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));
//getWriter should not change explicit character encoding
response.getWriter();
assertThat("ascii", Matchers.equalTo(response.getCharacterEncoding()));

response = getResponse();
assertThat("ascii", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));

//test that assumed overrides default
response = getResponse();
response = getResponse(context);
response.setContentType("application/json");
assertThat("utf-8", Matchers.equalTo(response.getCharacterEncoding()));
assertThat("utf-8", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));
response.getWriter();
//getWriter should not have modified character encoding
assertThat("utf-8", Matchers.equalTo(response.getCharacterEncoding()));

response = getResponse();
assertThat("utf-8", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));

//test that inferred overrides default
response = getResponse();
response = getResponse(context);
response.setContentType("application/xhtml+xml");
assertThat("utf-8", Matchers.equalTo(response.getCharacterEncoding()));
assertThat("utf-8", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));
//getWriter should not have modified character encoding
response.getWriter();
assertThat("utf-8", Matchers.equalTo(response.getCharacterEncoding()));

response = getResponse();

//test that without a default or any content type, use iso-8859-1
response = getResponse();
assertThat("iso-8859-1", Matchers.equalTo(response.getCharacterEncoding()));
//getWriter should not have modified character encoding
response.getWriter();
assertThat("iso-8859-1", Matchers.equalTo(response.getCharacterEncoding()));
assertThat("utf-8", Matchers.equalToIgnoringCase(response.getCharacterEncoding()));
}

@Test
Expand Down Expand Up @@ -1424,7 +1425,7 @@ public String toString()
(ServletPrintWriterCase)writer ->
{
writer.println("ABC");
return "ABC" + lineSep;
return "ABC" + lineSep;
})
);

Expand Down Expand Up @@ -1470,7 +1471,7 @@ public String toString()
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("writerCases")
public void testServletPrintWriter(@SuppressWarnings("unused") String description,
ServletPrintWriterCase writerConsumer) throws IOException
ServletPrintWriterCase writerConsumer) throws IOException
{
Response response = getResponse();
response.setCharacterEncoding(UTF_8.name());
Expand Down Expand Up @@ -1580,7 +1581,6 @@ public void testStatusCodesNoErrorHandler(int code, String message, String expec
}

@Test
@Disabled // TODO
public void testWriteCheckError() throws Exception
{
Response response = getResponse();
Expand Down Expand Up @@ -1695,7 +1695,6 @@ public static Stream<Arguments> redirects()

@ParameterizedTest
@MethodSource("redirects")
@Disabled // TODO
public void testSendRedirect(String destination, String expected, boolean cookie)
throws Exception
{
Expand All @@ -1710,9 +1709,13 @@ public void testSendRedirect(String destination, String expected, boolean cookie
{
for (String host : hosts)
{
//clear session cache
_context.stop();
_context.start();

Response response = getResponse();
Request request = response.getHttpChannel().getRequest();

request.getHttpChannel().getHttpConfiguration().setRelativeRedirectAllowed(false);
HttpURI.Mutable uri = HttpURI.build(request.getHttpURI(),
"/path/info;param;jsessionid=12345?query=0&more=1#target");
uri.scheme("http");
Expand All @@ -1734,10 +1737,10 @@ public void testSendRedirect(String destination, String expected, boolean cookie

String location = response.getHeader("Location");

expected = expected
.replace("@HOST@", host == null ? request.getLocalAddr() : host)
.replace("@PORT@", host == null ? ":8888" : (port == 80 ? "" : (":" + port)));
assertThat(host + ":" + port, location, equalTo(expected));
String expectedWithSubstitutions = expected;
expectedWithSubstitutions = expectedWithSubstitutions.replace("@HOST@", host == null ? request.getLocalAddr() : host)
.replace("@PORT@", host == null ? ":8888" : ":" + String.valueOf(PORT));
assertThat(host + ":" + port, location, equalTo(expectedWithSubstitutions));
}
}
}
Expand Down Expand Up @@ -1768,7 +1771,7 @@ public void testSendRedirectRelative()
{"../locati%C3%abn", "/locati%C3%abn"},
{"../other%2fplace", "/other%2fplace"},
{"http://somehost.com/other/location", "http://somehost.com/other/location"},
};
};

int[] ports = new int[]{8080, 80};
String[] hosts = new String[]{null, "myhost", "192.168.0.1", "[0::1]"};
Expand Down Expand Up @@ -2255,22 +2258,32 @@ public void testEnsureConsumeAllOrNotPersistentHttp11() throws Exception
assertThat(response.getHttpFields().get(HttpHeader.CONNECTION), is("one, two, three, close"));
}

private Response getResponse(HttpVersion version)
{
return getResponse(_context, version);
}

private Response getResponse(ContextHandler context)
{
return getResponse(context, HttpVersion.HTTP_1_0);
}

private Response getResponse()
{
return getResponse(HttpVersion.HTTP_1_0);
return getResponse(_context, HttpVersion.HTTP_1_0);
}

private Response getResponse(HttpVersion version)
private Response getResponse(ContextHandler context, HttpVersion version)
{
_channel.recycle();

MetaData.Request reqMeta = new MetaData.Request("GET", HttpURI.from("http://myhost:8888/path/info"), version, HttpFields.EMPTY);

org.eclipse.jetty.server.Request coreRequest = new MockRequest(reqMeta, _context.getServletContext().getCoreContext());
org.eclipse.jetty.server.Request coreRequest = new MockRequest(reqMeta, context.getServletContext().getCoreContext());
org.eclipse.jetty.server.Response coreResponse = new MockResponse(coreRequest);

_channel.onRequest(new ContextHandler.CoreContextRequest(coreRequest, _context.getCoreContextHandler().getContext(), _channel));
_channel.getRequest().setContext(_context._apiContext, URIUtil.decodePath(org.eclipse.jetty.server.Request.getPathInContext(coreRequest)));
_channel.onRequest(new ContextHandler.CoreContextRequest(coreRequest, context.getCoreContextHandler().getContext(), _channel));
_channel.getRequest().setContext(context._apiContext, URIUtil.decodePath(org.eclipse.jetty.server.Request.getPathInContext(coreRequest)));
_channel.onProcess(coreResponse, Callback.NOOP);

BufferUtil.clear(_content);
Expand Down Expand Up @@ -2502,7 +2515,7 @@ public void reset()
@Override
public CompletableFuture<Void> writeInterim(int status, HttpFields headers)
{
return null;
return null;
}
}
}

0 comments on commit 887e78a

Please sign in to comment.