From 4cabbf47ab62e9bdc18477c6e0f416b6df9bda8b Mon Sep 17 00:00:00 2001 From: CooperBigFoot Date: Sun, 15 Nov 2026 09:05:23 +0100 Subject: [PATCH] Fix proxy test fixture closing before request body completes --- .../java/org/kopi/ebics/client/StubProxy.java | 18 ++++++-- .../org/kopi/ebics/client/StubProxyTest.java | 43 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/kopi/ebics/client/StubProxyTest.java diff --git a/src/test/java/org/kopi/ebics/client/StubProxy.java b/src/test/java/org/kopi/ebics/client/StubProxy.java index cbf2ac3c..381f2e23 100644 --- a/src/test/java/org/kopi/ebics/client/StubProxy.java +++ b/src/test/java/org/kopi/ebics/client/StubProxy.java @@ -116,9 +116,21 @@ private static RecordedRequest parseRequest(InputStream in) throws IOException { String value = line.substring(colon + 1).trim(); headers.put(name.toLowerCase(Locale.ROOT), value); } - // Body is intentionally not drained: for our assertions only the - // request line + headers matter, and HttpClient is happy as long as - // it eventually sees a complete response on this connection. + // Finish reading the body before responding and closing the socket. + if ("chunked".equalsIgnoreCase(headers.get("transfer-encoding"))) { + int size; + while ((size = Integer.parseInt(readLine(in).split(";", 2)[0], 16)) != 0) { + in.skipNBytes(size); + if (!readLine(in).isEmpty()) { + throw new IOException("Expected CRLF after request chunk"); + } + } + while (!readLine(in).isEmpty()) { + // Discard trailers. + } + } else if (headers.containsKey("content-length")) { + in.skipNBytes(Long.parseLong(headers.get("content-length"))); + } return new RecordedRequest(requestLine, headers); } diff --git a/src/test/java/org/kopi/ebics/client/StubProxyTest.java b/src/test/java/org/kopi/ebics/client/StubProxyTest.java new file mode 100644 index 00000000..9d0f4d0e --- /dev/null +++ b/src/test/java/org/kopi/ebics/client/StubProxyTest.java @@ -0,0 +1,43 @@ +package org.kopi.ebics.client; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.net.InetAddress; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +class StubProxyTest { + + @Test + void waitsForCompleteRequestBodyBeforeResponding() throws Exception { + for (String framedBody : List.of( + "Content-Length: 2\r\n\r\nab", + "Transfer-Encoding: chunked\r\n\r\n1\r\na\r\n1\r\nb\r\n0\r\n\r\n")) { + try (StubProxy proxy = new StubProxy(); + Socket client = new Socket(InetAddress.getLoopbackAddress(), proxy.port())) { + proxy.enqueueResponse(200, Map.of(), new byte[0]); + var out = client.getOutputStream(); + var in = client.getInputStream(); + String request = "POST http://bank.example/ebics HTTP/1.1\r\n" + framedBody; + // Withhold the last body byte (or final chunk terminator byte). + out.write(request.substring(0, request.length() - 1).getBytes(StandardCharsets.ISO_8859_1)); + out.flush(); + client.setSoTimeout(500); + assertThrows(SocketTimeoutException.class, in::read, + "proxy must not respond before the request body is complete"); + + out.write(request.charAt(request.length() - 1)); + out.flush(); + client.setSoTimeout(5000); + assertEquals("HTTP/1.1 200 Status\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", + new String(in.readAllBytes(), StandardCharsets.ISO_8859_1)); + } + } + } +}