refactored, pre-buffer reuse

This commit is contained in:
Relism
2026-03-15 14:31:52 +01:00
parent f1beb160aa
commit b0d606cb5f
64 changed files with 888 additions and 400 deletions
@@ -0,0 +1,147 @@
package dev.relism;
import dev.relism.http.ContentType;
import dev.relism.models.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
class HttpServerTest {
private HttpServer server;
private int port;
@BeforeEach
void setUp() throws Exception {
// Find a free ephemeral port
try (ServerSocket s = new ServerSocket(0)) {
port = s.getLocalPort();
}
HttpServerConfiguration config = HttpServerConfiguration.builder()
.port(port)
.host("127.0.0.1")
.build();
server = new HttpServer(config);
// Setup some routes
server.get("/api/ping", (req, res) -> "pong");
server.post("/api/echo", (req, res) -> {
byte[] body = req.getBody();
return new Response(201, body, ContentType.TEXT_PLAIN); // echo body & change status
});
server.get("/api/crash", (req, res) -> {
throw new RuntimeException("Simulated Crash");
});
server.start().get(5, TimeUnit.SECONDS);
}
@AfterEach
void tearDown() {
if (server != null) {
server.stop();
}
}
// --- raw socket helper ---
private String sendRawRequest(String rawHttp) throws Exception {
try (Socket socket = new Socket("127.0.0.1", port);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream()) {
out.write(rawHttp.getBytes(StandardCharsets.UTF_8));
out.flush();
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int read;
while ((read = in.read(buffer)) != -1) {
baos.write(buffer, 0, read);
}
return baos.toString(StandardCharsets.UTF_8);
}
}
// --- E2E Tests ---
@Test
void testGet_pingRoute_returns200AndStringBody() throws Exception {
String req = "GET /api/ping HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
String res = sendRawRequest(req);
assertTrue(res.startsWith("HTTP/1.1 200 OK"));
assertTrue(res.contains("Content-Length: 4")); // "pong"
assertTrue(res.endsWith("pong"));
}
@Test
void testPost_echoRoute_returns201AndEchoesBody() throws Exception {
String body = "Hello, Flash!";
String req = "POST /api/echo HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Length: " + body.length() + "\r\n" +
"\r\n" +
body;
String res = sendRawRequest(req);
assertTrue(res.startsWith("HTTP/1.1 201 Created"));
assertTrue(res.contains("Content-Length: " + body.length()));
assertTrue(res.endsWith(body));
}
@Test
void testNotFound_returns404Html() throws Exception {
String req = "GET /api/unknown HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
String res = sendRawRequest(req);
assertTrue(res.startsWith("HTTP/1.1 404 Not Found"));
assertTrue(res.contains("404"));
assertTrue(res.contains("No route matched this request"));
}
@Test
void testException_returns500Html() throws Exception {
String req = "GET /api/crash HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
String res = sendRawRequest(req);
assertTrue(res.startsWith("HTTP/1.1 500 Internal Server Error"));
assertTrue(res.contains("500"));
assertTrue(res.contains("Simulated Crash"));
}
@Test
void testRoot_returns404_noExceptionTossed() throws Exception {
String req = "GET / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
String res = sendRawRequest(req);
assertTrue(res.startsWith("HTTP/1.1 404 Not Found"));
assertTrue(res.contains("No route matched this request."));
}
}