Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions src/test/java/org/kopi/ebics/client/StubProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/kopi/ebics/client/StubProxyTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
Loading