Files
baiyue-firstleg/integration-service/src/test/java/com/baiyue/integration/provider/MatsonTrackingClientTest.java
T
2026-08-21 16:04:16 +08:00

74 lines
3.4 KiB
Java

package com.baiyue.integration.provider;
import com.baiyue.integration.config.IntegrationProperties;
import com.baiyue.integration.dto.CarrierScheduleDtos.QueryRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
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 MatsonTrackingClientTest {
private HttpServer server;
@AfterEach
void stopServer() {
if (server != null) server.stop(0);
}
@Test
void obtainsDynamicTtrAndCookieBeforePostingAjaxForm() throws Exception {
AtomicReference<String> postedBody = new AtomicReference<>();
AtomicReference<String> postedCookie = new AtomicReference<>();
AtomicReference<String> requestedWith = new AtomicReference<>();
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/page", exchange -> {
exchange.getResponseHeaders().add("Set-Cookie", "matson-session=abc123; Path=/");
respond(exchange, 200, "<input type=\"hidden\" id=\"ttr\" name=\"ttr\" value=\"dynamic123\" />");
});
server.createContext("/query", exchange -> {
postedBody.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
postedCookie.set(exchange.getRequestHeaders().getFirst("Cookie"));
requestedWith.set(exchange.getRequestHeaders().getFirst("X-Requested-With"));
respond(exchange, 200, "{\"containerNumber\":\"MATU459895\",\"checkDigit\":\"6\",\"events\":[]}");
});
server.start();
int port = server.getAddress().getPort();
var matson = new IntegrationProperties.CarrierSchedule.Matson(true,
URI.create("http://127.0.0.1:" + port + "/page"),
URI.create("http://127.0.0.1:" + port + "/query"),
Duration.ofSeconds(2), Duration.ofSeconds(2), Duration.ZERO);
MatsonTrackingClient client = new MatsonTrackingClient();
ReflectionTestUtils.setField(client, "properties", new IntegrationProperties(null, null,
new IntegrationProperties.CarrierSchedule(matson, null, null, null, null)));
ReflectionTestUtils.setField(client, "objectMapper", new ObjectMapper());
client.initialize();
var result = client.query(new QueryRequest("MATSON", "CONTAINER_NO", "MATU4598956"));
assertThat(result.payload().path("containerNumber").asText()).isEqualTo("MATU459895");
assertThat(postedBody.get()).contains("container_trackingnumber=MATU4598956")
.contains("trackingVal=tckconta").contains("ttr=dynamic123");
assertThat(postedCookie.get()).contains("matson-session=abc123");
assertThat(requestedWith.get()).isEqualTo("XMLHttpRequest");
}
private static void respond(HttpExchange exchange, int status, String body) throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.close();
}
}