2026-09-28 04:55:00 +08:00
|
|
|
|
package com.moyi.controlplane.task;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2026-09-29 01:29:07 +08:00
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2026-09-28 04:55:00 +08:00
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
import com.moyi.controlplane.common.ApiException;
|
|
|
|
|
|
import com.moyi.controlplane.security.SecretCipher;
|
2026-10-21 18:57:35 +08:00
|
|
|
|
import com.moyi.controlplane.provider.transport.ProviderRequestException;
|
2026-09-28 04:55:00 +08:00
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2026-09-29 17:00:55 +08:00
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2026-09-28 04:55:00 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
|
|
import org.junit.jupiter.params.provider.CsvSource;
|
|
|
|
|
|
import org.springframework.core.env.Environment;
|
2026-09-29 17:00:55 +08:00
|
|
|
|
import org.springframework.mock.env.MockEnvironment;
|
2026-09-28 04:55:00 +08:00
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncProviderClientTest {
|
2026-10-13 20:13:21 +08:00
|
|
|
|
@Test void imagesPreserveUsageAndGeminiUsesItsNativeEndpoint() throws Exception {
|
|
|
|
|
|
HttpServer server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);
|
|
|
|
|
|
var path=new AtomicReference<String>();
|
|
|
|
|
|
server.createContext("/",exchange->{
|
|
|
|
|
|
path.set(exchange.getRequestURI().getPath());
|
|
|
|
|
|
String body=path.get().contains("generateContent")
|
|
|
|
|
|
? "{\"candidates\":[{\"content\":{\"parts\":[{\"inlineData\":{\"mimeType\":\"image/png\",\"data\":\"cG5n\"}}]}}],\"usageMetadata\":{\"promptTokenCount\":20,\"candidatesTokenCount\":100}}"
|
|
|
|
|
|
: "{\"data\":[{\"b64_json\":\"cG5n\"}],\"usage\":{\"input_tokens\":10,\"output_tokens\":50}}";
|
|
|
|
|
|
byte[] bytes=body.getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200,bytes.length);
|
|
|
|
|
|
try(var out=exchange.getResponseBody()){out.write(bytes);}
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
var jdbc=mock(JdbcTemplate.class); var cipher=new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class),eq("relay"))).thenReturn(Map.of("code","apipro","protocol","openai-compatible","base_url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1","credential_ciphertext",cipher.encrypt("test-key")));
|
|
|
|
|
|
var client=new AsyncProviderClient(jdbc,cipher,new ObjectMapper(),mock(Environment.class));
|
|
|
|
|
|
var image=(Map<?,?>)client.submit("relay","gpt-image-2","image",Map.of("prompt","cat")).get("_moyiInlineResult");
|
|
|
|
|
|
assertEquals("/v1/images/generations",path.get()); assertTrue(image.containsKey("usage"));
|
|
|
|
|
|
var gemini=(Map<?,?>)client.submit("relay","gemini-3-pro-image-preview","image",Map.of("prompt","cat")).get("_moyiInlineResult");
|
|
|
|
|
|
assertEquals("/v1beta/models/gemini-3-pro-image-preview:generateContent",path.get());
|
|
|
|
|
|
assertEquals("cG5n",gemini.get("base64")); assertTrue(gemini.containsKey("usageMetadata"));
|
|
|
|
|
|
} finally { server.stop(0); }
|
|
|
|
|
|
}
|
2026-09-28 04:55:00 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
void upstreamErrorDoesNotExposeProviderMessage() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/", exchange -> {
|
|
|
|
|
|
byte[] body = "{\"code\":1001,\"message\":\"secret account and API key details\"}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
|
|
|
|
|
exchange.sendResponseHeaders(200, body.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(body); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
String baseUrl = "http://127.0.0.1:" + server.getAddress().getPort() + "/tripo";
|
|
|
|
|
|
AsyncProviderClient client = clientFor(baseUrl);
|
|
|
|
|
|
|
|
|
|
|
|
ApiException error = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
|
|
|
|
|
|
assertEquals("UPSTREAM_PROVIDER_ERROR", error.getCode());
|
|
|
|
|
|
assertEquals("上游 3D 任务创建失败", error.getMessage());
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-29 01:29:07 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
void tripoUsesCurrentOfficialV3EndpointAndBearerAuth() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/v3/generation/text-to-model", exchange -> {
|
|
|
|
|
|
assertEquals("Bearer test-key", exchange.getRequestHeaders().getFirst("Authorization"));
|
|
|
|
|
|
byte[] body = "{\"code\":0,\"data\":{\"task_id\":\"task-1\"}}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, body.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(body); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
|
|
|
|
|
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", "chair");
|
|
|
|
|
|
assertEquals("task-1", ((Map<?,?>) result.get("data")).get("task_id"));
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
void tripoImageInputUsesDocumentedInputField() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/v3/generation/image-to-model", exchange -> {
|
|
|
|
|
|
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
|
|
|
|
|
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"input\":\"https://example.test/image.png\""));
|
|
|
|
|
|
org.junit.jupiter.api.Assertions.assertFalse(body.contains("\"image\":\""));
|
|
|
|
|
|
byte[] response = "{\"code\":0,\"data\":{\"task_id\":\"task-image\"}}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, response.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
|
|
|
|
|
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", Map.of("prompt", "chair", "media_url", "https://example.test/image.png"));
|
|
|
|
|
|
assertEquals("task-image", ((Map<?,?>) result.get("data")).get("task_id"));
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
void openAiCompatibleSpeechStoresBoundedBinaryAudio() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/audio/speech", exchange -> {
|
|
|
|
|
|
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
|
|
|
|
|
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"voice\":\"alloy\""));
|
|
|
|
|
|
exchange.getResponseHeaders().set("Content-Type", "audio/mpeg");
|
|
|
|
|
|
byte[] audio = "fake-mp3".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, audio.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(audio); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
|
|
|
|
|
SecretCipher cipher = new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class), eq("provider-audio"))).thenReturn(Map.of(
|
|
|
|
|
|
"code", "relay-audio", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")
|
|
|
|
|
|
));
|
|
|
|
|
|
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
|
|
|
|
|
Map<String,Object> result = client.submit("provider-audio", "tts-1", "audio", Map.of("prompt", "hello"));
|
|
|
|
|
|
Map<?,?> inline = (Map<?,?>) result.get("_moyiInlineResult");
|
|
|
|
|
|
assertEquals("audio/mpeg", inline.get("mimeType"));
|
|
|
|
|
|
assertEquals("ZmFrZS1tcDM=", inline.get("base64"));
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
void openAiCompatibleVideoUsesMultipartRequest() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/videos", exchange -> {
|
|
|
|
|
|
assertTrue(exchange.getRequestHeaders().getFirst("Content-Type").startsWith("multipart/form-data; boundary="));
|
|
|
|
|
|
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
|
|
|
|
|
assertTrue(body.contains("name=\"model\""));
|
|
|
|
|
|
assertTrue(body.contains("sora-2"));
|
|
|
|
|
|
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, response.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
|
|
|
|
|
SecretCipher cipher = new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
|
|
|
|
|
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")
|
|
|
|
|
|
));
|
|
|
|
|
|
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
|
|
|
|
|
Map<String,Object> result = client.submit("provider-video", "sora-2", "video", Map.of("prompt", "a cat"));
|
|
|
|
|
|
assertEquals("video-1", result.get("id"));
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-29 17:00:55 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
void asyncPollingReusesTheSubmittedKeyFingerprint() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
AtomicReference<String> submitAuthorization = new AtomicReference<>();
|
|
|
|
|
|
AtomicReference<String> pollAuthorization = new AtomicReference<>();
|
|
|
|
|
|
server.createContext("/videos", exchange -> {
|
|
|
|
|
|
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
|
|
|
|
|
|
if (exchange.getRequestURI().getPath().equals("/videos")) {
|
|
|
|
|
|
submitAuthorization.set(authorization);
|
|
|
|
|
|
exchange.getRequestBody().readAllBytes();
|
|
|
|
|
|
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, response.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
|
|
|
|
|
} else {
|
|
|
|
|
|
pollAuthorization.set(authorization);
|
|
|
|
|
|
byte[] response = "{\"id\":\"video-1\",\"status\":\"succeeded\"}".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, response.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(response); }
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
|
|
|
|
|
SecretCipher cipher = new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
|
|
|
|
|
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", ""
|
|
|
|
|
|
));
|
|
|
|
|
|
MockEnvironment environment = new MockEnvironment()
|
|
|
|
|
|
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "key-a|key-b")
|
|
|
|
|
|
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_VIDEO_SORA_2", "wrong-model-key");
|
|
|
|
|
|
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), environment);
|
|
|
|
|
|
|
|
|
|
|
|
Map<String,Object> submitted = client.submit("provider-video", "sora-2", "route-a", "video", Map.of("prompt", "a cat"));
|
|
|
|
|
|
String fingerprint = String.valueOf(submitted.get("_moyiUpstreamKeyFingerprint"));
|
|
|
|
|
|
assertTrue(fingerprint.matches("[0-9a-f]{64}"));
|
|
|
|
|
|
client.poll("provider-video", "route-a", "sora-2", "video-1", fingerprint);
|
|
|
|
|
|
assertEquals(submitAuthorization.get(), pollAuthorization.get());
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-28 04:55:00 +08:00
|
|
|
|
@ParameterizedTest
|
2026-10-21 18:57:35 +08:00
|
|
|
|
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "408,UPSTREAM_PROVIDER_ERROR", "409,UPSTREAM_PROVIDER_ERROR", "503,UPSTREAM_PROVIDER_ERROR"})
|
2026-09-28 04:55:00 +08:00
|
|
|
|
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/", exchange -> {
|
|
|
|
|
|
byte[] body = "private account details".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(status, body.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(body); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort() + "/tripo");
|
|
|
|
|
|
ApiException submit = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
|
|
|
|
|
|
assertEquals(code, submit.getCode());
|
|
|
|
|
|
assertEquals("上游 3D 任务创建失败(HTTP " + status + ")", submit.getMessage());
|
2026-10-21 18:57:35 +08:00
|
|
|
|
assertEquals(status != 408 && status != 409 && status < 500, ((ProviderRequestException) submit).definitelyNotAccepted());
|
2026-09-28 04:55:00 +08:00
|
|
|
|
ApiException poll = assertThrows(ApiException.class, () -> client.poll("provider-vast", "task-test"));
|
|
|
|
|
|
assertEquals(code, poll.getCode());
|
|
|
|
|
|
assertEquals("上游异步任务查询失败(HTTP " + status + ")", poll.getMessage());
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
server.stop(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-21 18:57:35 +08:00
|
|
|
|
@Test void successfulHttpResponseWithMalformedBodyHasUnknownAcceptance() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
server.createContext("/", exchange -> {
|
|
|
|
|
|
byte[] body = "not-json; private upstream detail".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(200, body.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(body); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
var client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
|
|
|
|
|
var failure = assertThrows(ProviderRequestException.class, () -> client.submit("provider-vast", "tripo", "3d", "chair"));
|
|
|
|
|
|
assertEquals(ProviderRequestException.Acceptance.UNKNOWN, failure.acceptance());
|
|
|
|
|
|
org.junit.jupiter.api.Assertions.assertFalse(failure.getMessage().contains("private upstream detail"));
|
|
|
|
|
|
} finally { server.stop(0); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
|
|
@CsvSource({"image,gpt-image-2", "video,sora-2", "audio,tts-1"})
|
|
|
|
|
|
void compatibleSubmit5xxRemainsUnknownAndNeverUsesFallbackRoute(String type, String model) throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
var calls = new java.util.concurrent.atomic.AtomicInteger();
|
|
|
|
|
|
server.createContext("/", exchange -> {
|
|
|
|
|
|
calls.incrementAndGet();
|
|
|
|
|
|
exchange.getRequestBody().readAllBytes();
|
|
|
|
|
|
byte[] body = "private account details".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
exchange.sendResponseHeaders(503, body.length);
|
|
|
|
|
|
try (var output = exchange.getResponseBody()) { output.write(body); }
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
var jdbc = mock(JdbcTemplate.class);
|
|
|
|
|
|
var cipher = new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class), eq("relay"))).thenReturn(Map.of(
|
|
|
|
|
|
"code", "relay", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")));
|
|
|
|
|
|
var client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
|
|
|
|
|
var failure = assertThrows(ProviderRequestException.class, () -> client.submit("relay", model, type, "chair"));
|
|
|
|
|
|
assertEquals(ProviderRequestException.Acceptance.UNKNOWN, failure.acceptance());
|
|
|
|
|
|
assertEquals(1, calls.get());
|
|
|
|
|
|
} finally { server.stop(0); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test void acceptedPostWithDroppedResponseIsUnknownAndIsNotTransparentlyRepeated() throws Exception {
|
|
|
|
|
|
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
|
|
|
|
var calls = new java.util.concurrent.atomic.AtomicInteger();
|
|
|
|
|
|
server.createContext("/", exchange -> {
|
|
|
|
|
|
exchange.getRequestBody().readAllBytes();
|
|
|
|
|
|
calls.incrementAndGet();
|
|
|
|
|
|
exchange.close(); // Simulate acceptance followed by loss of the HTTP response.
|
|
|
|
|
|
});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
try {
|
|
|
|
|
|
var client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
|
|
|
|
|
var failure = assertThrows(ProviderRequestException.class, () -> client.submit("provider-vast", "tripo", "3d", "chair"));
|
|
|
|
|
|
assertEquals(ProviderRequestException.Acceptance.UNKNOWN, failure.acceptance());
|
|
|
|
|
|
assertEquals(1, calls.get());
|
|
|
|
|
|
} finally { server.stop(0); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test void invalidMediaIsKnownToFailBeforeSendingGenerationRequest() {
|
|
|
|
|
|
var client = clientFor("http://127.0.0.1:1");
|
|
|
|
|
|
var failure = assertThrows(ProviderRequestException.class, () -> client.submit("provider-vast", "tripo", "3d", Map.of("media_url", "http://127.0.0.1/private")));
|
|
|
|
|
|
assertEquals(ProviderRequestException.Acceptance.NOT_SENT, failure.acceptance());
|
|
|
|
|
|
assertEquals("INVALID_MEDIA_URL", failure.getCode());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-28 04:55:00 +08:00
|
|
|
|
private AsyncProviderClient clientFor(String baseUrl) {
|
|
|
|
|
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
|
|
|
|
|
SecretCipher cipher = new SecretCipher("test-encryption");
|
|
|
|
|
|
when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of(
|
2026-09-29 01:29:07 +08:00
|
|
|
|
"code", "vast-tripo", "protocol", "http", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
|
2026-09-28 04:55:00 +08:00
|
|
|
|
));
|
|
|
|
|
|
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|