Skip to content
Merged
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 @@ -17,59 +17,53 @@

package org.apache.stormcrawler.protocol;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;

/** Takes care of initialising Jetty for testing protocol implementation * */
public abstract class AbstractProtocolTest {

protected static Server httpServer;

protected static final Integer HTTP_PORT = findRandomOpenPortOnAllLocalInterfaces();
protected static Integer HTTP_PORT;

@BeforeEach
void initJetty() throws Exception {
if (httpServer != null) {
if (httpServer != null && httpServer.isRunning()) {
return;
}
assertNotEquals(Integer.valueOf(-1), HTTP_PORT);
httpServer = new Server(HTTP_PORT);

httpServer = new Server(0);
final HandlerList handlers = new HandlerList();
handlers.setHandlers(getHandlers());
httpServer.setHandler(handlers);
httpServer.start();
}

protected Handler[] getHandlers() {
return new Handler[] {new WildcardResourceHandler()};
HTTP_PORT = ((ServerConnector) httpServer.getConnectors()[0]).getLocalPort();
Assertions.assertNotNull(HTTP_PORT);
}

@AfterAll
static void stopJetty() {
try {
static void stopJetty() throws Exception {
if (httpServer != null) {
httpServer.stop();
} catch (Exception ignored) {
httpServer = null;
}
}

private static Integer findRandomOpenPortOnAllLocalInterfaces() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
return -1;
}
protected Handler[] getHandlers() {
return new Handler[] {new WildcardResourceHandler()};
}

public static class WildcardResourceHandler extends AbstractHandler {
Expand Down