Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable HttpChannelEventTest and port to ee9 #12477

Open
wants to merge 1 commit into
base: jetty-12.1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,55 @@
// ========================================================================
//

package org.eclipse.jetty.server;
package org.eclipse.jetty.ee9.servlet;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee9.nested.HttpChannel;
import org.eclipse.jetty.ee9.nested.Request;
import org.eclipse.jetty.ee9.nested.Response;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.NanoTime;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled // TODO
public class HttpChannelEventTest
{
private Server server;
private LocalConnector connector;

public void start(Handler handler) throws Exception
public void start(HttpServlet servlet) throws Exception
{
server = new Server();
connector = new LocalConnector(server);
server.addConnector(connector);
server.setHandler(handler);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(new ServletHolder(servlet), "/*");
server.setHandler(context);
server.start();
}

Expand All @@ -59,11 +75,10 @@ public void testRequestContentSlice() throws Exception
{
int data = 'x';
CountDownLatch applicationLatch = new CountDownLatch(1);
/* TODO
start(new TestHandler()
start(new TestServlet()
{
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
{
ServletInputStream input = request.getInputStream();
int content = input.read();
Expand All @@ -72,10 +87,7 @@ protected void handle(HttpServletRequest request, HttpServletResponse response)
}
});

*/

CountDownLatch listenerLatch = new CountDownLatch(1);
/* TODO
connector.addBean(new HttpChannel.Listener()
{
@Override
Expand All @@ -87,7 +99,6 @@ public void onRequestContent(Request request, ByteBuffer content)
}
});

*/

HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "localhost");
Expand All @@ -107,20 +118,18 @@ public void onRequestContent(Request request, ByteBuffer content)
public void testResponseContentSlice() throws Exception
{
byte[] data = new byte[]{'y'};
/* TODO
start(new TestHandler()

start(new TestServlet()
{
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.write(true, callback, ByteBuffer.wrap(data));
response.getOutputStream().write(data);
}
});

*/

CountDownLatch latch = new CountDownLatch(1);
/* TODO
connector.addBean(new HttpChannel.Listener()
{
@Override
Expand All @@ -131,8 +140,6 @@ public void onResponseContent(Request request, ByteBuffer content)
}
});

*/

HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "localhost");
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request.toString(), 5, TimeUnit.SECONDS));
Expand All @@ -146,30 +153,31 @@ public void onResponseContent(Request request, ByteBuffer content)
@Test
public void testRequestFailure() throws Exception
{
start(new TestHandler());
start(new TestServlet());

CountDownLatch latch = new CountDownLatch(2);
/* TODO

connector.addBean(new HttpChannel.Listener()
{
@Override
public void onRequestFailure(Request request, Throwable failure)
{
//TODO never called
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onRequestFailure and onComplete methods will never be called for a bad request in jetty-12, as that is determined in core and thus we do not know what context the bad request would have gone to.

So this test is invalid and we should update the javadoc of the listener class to say as much

latch.countDown();
}

@Override
public void onComplete(Request request)
{
//TODO never called
latch.countDown();
}
});

*/

// No Host header, request will fail.
String request = HttpTester.newRequest().toString();
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request, 5, TimeUnit.SECONDS));
HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "");
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request.toString(), 5, TimeUnit.SECONDS));

assertEquals(HttpStatus.BAD_REQUEST_400, response.getStatus());
assertTrue(latch.await(5, TimeUnit.SECONDS));
Expand All @@ -178,9 +186,8 @@ public void onComplete(Request request)
@Test
public void testResponseBeginModifyHeaders() throws Exception
{
start(new TestHandler()
start(new TestServlet()
{
/* TODO
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response)
{
Expand All @@ -190,10 +197,8 @@ protected void handle(HttpServletRequest request, HttpServletResponse response)
response.addHeader("X-Header", "foo");
response.addHeader("X-Header", "bar");
}
*/
});

/* TODO
CountDownLatch latch = new CountDownLatch(1);
connector.addBean(new HttpChannel.Listener()
{
Expand Down Expand Up @@ -224,32 +229,29 @@ public void onComplete(Request request)
List<HttpField> xheaders = response.getFields("X-Header");
assertThat("X-Header count", xheaders.size(), is(1));
assertThat("X-Header[0].value", xheaders.get(0).getValue(), is("zed"));
*/
}

@Test
public void testResponseFailure() throws Exception
{
/* TODO
start(new TestHandler()
start(new TestServlet()
{
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
{
// Closes all connections, response will fail.
connector.getConnectedEndPoints().forEach(EndPoint::close);
}
});

*/

CountDownLatch latch = new CountDownLatch(2);
/* TODO

connector.addBean(new HttpChannel.Listener()
{
@Override
public void onResponseFailure(Request request, Throwable failure)
{
//TODO this is never called
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a bug. I see no reason that a response failure within a context cannot be notified like this.
Further analysis required.

latch.countDown();
}

Expand All @@ -260,10 +262,7 @@ public void onComplete(Request request)
}
});

*/

HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "localhost");
HttpTester.parseResponse(connector.getResponse(request.toString(), 5, TimeUnit.SECONDS));

assertTrue(latch.await(5, TimeUnit.SECONDS));
Expand All @@ -272,11 +271,11 @@ public void onComplete(Request request)
@Test
public void testExchangeTimeRecording() throws Exception
{
start(new TestHandler());
start(new TestServlet());

CountDownLatch latch = new CountDownLatch(1);
AtomicLong elapsed = new AtomicLong();
/* TODO

connector.addBean(new HttpChannel.Listener()
{
private final String attribute = getClass().getName() + ".begin";
Expand All @@ -296,8 +295,6 @@ public void onComplete(Request request)
}
});

*/

HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "localhost");
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request.toString(), 5, TimeUnit.SECONDS));
Expand All @@ -311,10 +308,10 @@ public void onComplete(Request request)
@Test
public void testTransientListener() throws Exception
{
start(new TestHandler());
start(new TestServlet());

CountDownLatch latch = new CountDownLatch(1);
/* TODO

connector.addBean(new HttpChannel.TransientListeners());
connector.addBean(new HttpChannel.Listener()
{
Expand All @@ -332,8 +329,6 @@ public void onComplete(Request request)
}
});

*/

HttpTester.Request request = HttpTester.newRequest();
request.setHeader("Host", "localhost");
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request.toString(), 5, TimeUnit.SECONDS));
Expand All @@ -342,27 +337,15 @@ public void onComplete(Request request)
assertTrue(latch.await(5, TimeUnit.SECONDS));
}

private static class TestHandler extends Handler.Abstract
private static class TestServlet extends HttpServlet
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
handle(request, response);
}
catch (Throwable t)
{
callback.failed(t);
}
finally
{
callback.succeeded();
}
return true;
handle(request, response);
}

protected void handle(Request request, Response response) throws IOException
protected void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
{
}
}
Expand Down
Loading