package com.baiyue.integration.provider; import com.baiyue.integration.config.IntegrationProperties; import com.baiyue.integration.dto.CarrierScheduleDtos.QueryRequest; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.concurrent.atomic.AtomicReference; import static org.assertj.core.api.Assertions.assertThat; class HedeTrackingClientTest { private HttpServer server; @AfterEach void stopServer() { if (server != null) server.stop(0); } @Test void postsTheConfirmedHedeFormAsUtf8() throws Exception { AtomicReference postedBody = new AtomicReference<>(); server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); server.createContext("/getVBilldynamic", exchange -> { postedBody.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8)); respond(exchange, 200, "" + "
***XYLU8251457
"); }); server.start(); var hede = new IntegrationProperties.CarrierSchedule.Hede(true, URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/getVBilldynamic"), Duration.ofSeconds(2), Duration.ofSeconds(2), Duration.ZERO); HedeTrackingClient client = new HedeTrackingClient(); ReflectionTestUtils.setField(client, "properties", new IntegrationProperties(null, null, new IntegrationProperties.CarrierSchedule(null, hede, null, null, null))); client.initialize(); var response = client.query(new QueryRequest("HEDE", "CONTAINER_NO", "XYLU8251457")); assertThat(response.html()).contains("XYLU8251457"); assertThat(postedBody.get()).isEqualTo( "billno=&cntr=XYLU8251457&selecttype=1&resultlist=1¶mtype=0"); } private static void respond(HttpExchange exchange, int status, String body) throws IOException { byte[] bytes = body.getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8"); exchange.sendResponseHeaders(status, bytes.length); exchange.getResponseBody().write(bytes); exchange.close(); } }