|
| 1 | +package org.kopi.ebics.client; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.net.URL; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Base64; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | + |
| 18 | +import com.sun.net.httpserver.HttpServer; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.kopi.ebics.io.ByteArrayContentFactory; |
| 23 | +import org.kopi.ebics.session.EbicsSession; |
| 24 | +import org.mockito.Mockito; |
| 25 | + |
| 26 | +/** |
| 27 | + * End-to-end test for {@link HttpRequestSender}: spins up an in-process |
| 28 | + * {@link HttpServer}, captures what the sender posts, and asserts the |
| 29 | + * sender returns the server's response body and status code unmodified. |
| 30 | + * |
| 31 | + * <p>Written before migrating from Apache HttpClient 4.x to |
| 32 | + * {@link java.net.http.HttpClient} so the contract is pinned independently |
| 33 | + * of the underlying client. |
| 34 | + */ |
| 35 | +class HttpRequestSenderTest { |
| 36 | + |
| 37 | + private HttpServer server; |
| 38 | + private AtomicReference<String> capturedMethod; |
| 39 | + private AtomicReference<String> capturedContentType; |
| 40 | + private AtomicReference<byte[]> capturedBody; |
| 41 | + private volatile int responseStatus; |
| 42 | + private volatile byte[] responseBody; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void startServer() throws Exception { |
| 46 | + capturedMethod = new AtomicReference<>(); |
| 47 | + capturedContentType = new AtomicReference<>(); |
| 48 | + capturedBody = new AtomicReference<>(); |
| 49 | + responseStatus = 200; |
| 50 | + responseBody = "<response/>".getBytes(StandardCharsets.UTF_8); |
| 51 | + |
| 52 | + server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); |
| 53 | + server.createContext("/", exchange -> { |
| 54 | + capturedMethod.set(exchange.getRequestMethod()); |
| 55 | + capturedContentType.set(exchange.getRequestHeaders().getFirst("Content-Type")); |
| 56 | + capturedBody.set(exchange.getRequestBody().readAllBytes()); |
| 57 | + exchange.sendResponseHeaders(responseStatus, responseBody.length); |
| 58 | + try (OutputStream os = exchange.getResponseBody()) { |
| 59 | + os.write(responseBody); |
| 60 | + } |
| 61 | + }); |
| 62 | + server.start(); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterEach |
| 66 | + void stopServer() { |
| 67 | + if (server != null) { |
| 68 | + server.stop(0); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void postsRequestBodyAndReturnsResponse() throws Exception { |
| 74 | + byte[] requestBody = "<EbicsRequest>hello</EbicsRequest>".getBytes(StandardCharsets.UTF_8); |
| 75 | + |
| 76 | + HttpRequestSender sender = new HttpRequestSender(session(serverUrl())); |
| 77 | + int status = sender.send(new ByteArrayContentFactory(requestBody)); |
| 78 | + |
| 79 | + assertEquals(200, status, "status code must be propagated from server"); |
| 80 | + assertEquals("POST", capturedMethod.get(), "must use POST"); |
| 81 | + assertEquals("text/xml; charset=ISO-8859-1", capturedContentType.get(), |
| 82 | + "Content-Type header must match EBICS expectation"); |
| 83 | + assertArrayEquals(requestBody, capturedBody.get(), |
| 84 | + "request body bytes must reach the server unchanged"); |
| 85 | + assertArrayEquals(responseBody, sender.getResponseBody().getContent().readAllBytes(), |
| 86 | + "response body must be exposed via getResponseBody()"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void propagatesNonSuccessStatusCodes() throws Exception { |
| 91 | + responseStatus = 500; |
| 92 | + responseBody = "<error/>".getBytes(StandardCharsets.UTF_8); |
| 93 | + |
| 94 | + HttpRequestSender sender = new HttpRequestSender(session(serverUrl())); |
| 95 | + int status = sender.send(new ByteArrayContentFactory("x".getBytes(StandardCharsets.UTF_8))); |
| 96 | + |
| 97 | + assertEquals(500, status); |
| 98 | + assertArrayEquals(responseBody, sender.getResponseBody().getContent().readAllBytes()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void routesThroughConfiguredProxyWithoutAuth() throws Exception { |
| 103 | + try (StubProxy proxy = new StubProxy()) { |
| 104 | + proxy.enqueueResponse(200, Map.of(), "<ok/>".getBytes(StandardCharsets.UTF_8)); |
| 105 | + |
| 106 | + EbicsSession session = proxiedSession(proxy.port(), null, null); |
| 107 | + HttpRequestSender sender = new HttpRequestSender(session); |
| 108 | + int status = sender.send(new ByteArrayContentFactory("x".getBytes(StandardCharsets.UTF_8))); |
| 109 | + |
| 110 | + assertEquals(200, status); |
| 111 | + List<StubProxy.RecordedRequest> reqs = proxy.recordedRequests(); |
| 112 | + assertEquals(1, reqs.size(), "proxy must receive exactly one request"); |
| 113 | + // Going through a proxy, the request line carries the absolute URI. |
| 114 | + assertTrue(reqs.get(0).requestLine().contains("http://bank.example/ebics"), |
| 115 | + "request line must contain the absolute target URI; got: " + reqs.get(0).requestLine()); |
| 116 | + assertFalse(reqs.get(0).headers().containsKey("proxy-authorization"), |
| 117 | + "no Proxy-Authorization header expected when no credentials configured"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void retriesWithProxyAuthorizationAfter407Challenge() throws Exception { |
| 123 | + try (StubProxy proxy = new StubProxy()) { |
| 124 | + // First connection: challenge with 407 so the client invokes the Authenticator. |
| 125 | + proxy.enqueueResponse(407, |
| 126 | + Map.of("Proxy-Authenticate", "Basic realm=\"ebics\""), |
| 127 | + new byte[0]); |
| 128 | + // Second connection: the retry carrying Proxy-Authorization. |
| 129 | + proxy.enqueueResponse(200, Map.of(), "<ok/>".getBytes(StandardCharsets.UTF_8)); |
| 130 | + |
| 131 | + EbicsSession session = proxiedSession(proxy.port(), "alice", "s3cret"); |
| 132 | + HttpRequestSender sender = new HttpRequestSender(session); |
| 133 | + int status = sender.send(new ByteArrayContentFactory("x".getBytes(StandardCharsets.UTF_8))); |
| 134 | + |
| 135 | + assertEquals(200, status, "client must complete the request after auth retry"); |
| 136 | + List<StubProxy.RecordedRequest> reqs = proxy.recordedRequests(); |
| 137 | + assertEquals(2, reqs.size(), "proxy must see the original request plus the auth retry"); |
| 138 | + assertFalse(reqs.get(0).headers().containsKey("proxy-authorization"), |
| 139 | + "first request must go without credentials (challenge-response flow)"); |
| 140 | + |
| 141 | + String expectedAuth = "Basic " |
| 142 | + + Base64.getEncoder().encodeToString("alice:s3cret".getBytes(StandardCharsets.UTF_8)); |
| 143 | + assertEquals(expectedAuth, reqs.get(1).header("Proxy-Authorization"), |
| 144 | + "retry must carry Basic Proxy-Authorization derived from configured credentials"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private URL serverUrl() throws Exception { |
| 149 | + return new URL("http://127.0.0.1:" + server.getAddress().getPort() + "/"); |
| 150 | + } |
| 151 | + |
| 152 | + private static EbicsSession session(URL url) { |
| 153 | + EbicsSession session = Mockito.mock(EbicsSession.class, Mockito.RETURNS_DEEP_STUBS); |
| 154 | + Mockito.when(session.getConfiguration().getProperty(Mockito.anyString())).thenReturn(null); |
| 155 | + Mockito.when(session.getUser().getPartner().getBank().getURL()).thenReturn(url); |
| 156 | + return session; |
| 157 | + } |
| 158 | + |
| 159 | + private static EbicsSession proxiedSession(int proxyPort, String user, String pass) throws Exception { |
| 160 | + EbicsSession session = Mockito.mock(EbicsSession.class, Mockito.RETURNS_DEEP_STUBS); |
| 161 | + var conf = session.getConfiguration(); |
| 162 | + Mockito.when(conf.getProperty(Mockito.anyString())).thenReturn(null); |
| 163 | + Mockito.when(conf.getProperty("http.proxy.host")).thenReturn("127.0.0.1"); |
| 164 | + Mockito.when(conf.getProperty("http.proxy.port")).thenReturn(String.valueOf(proxyPort)); |
| 165 | + if (user != null) { |
| 166 | + Mockito.when(conf.getProperty("http.proxy.user")).thenReturn(user); |
| 167 | + Mockito.when(conf.getProperty("http.proxy.password")).thenReturn(pass); |
| 168 | + } |
| 169 | + // Target host is arbitrary — the stub proxy intercepts everything and never forwards. |
| 170 | + Mockito.when(session.getUser().getPartner().getBank().getURL()) |
| 171 | + .thenReturn(new URL("http://bank.example/ebics")); |
| 172 | + return session; |
| 173 | + } |
| 174 | +} |
0 commit comments