This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 7be323f42f test(sdk): replace public HTTP endpoint with loopback
fixture (#7891)
7be323f42f is described below
commit 7be323f42ff34aafcefd04b8974fb2015998b1b6
Author: cloudbase-ai <[email protected]>
AuthorDate: Wed Oct 21 13:19:33 2026 +0800
test(sdk): replace public HTTP endpoint with loopback fixture (#7891)
Signed-off-by: cloudbase-ai <[email protected]>
Co-authored-by: cloudbase-ai
<[email protected]>
---
.../sdk/httpclient/HttpShenyuSdkClientTest.java | 56 +++++++++++++++++++---
1 file changed, 50 insertions(+), 6 deletions(-)
diff --git
a/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
b/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
index 98f713b472..444f1e150b 100644
---
a/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
+++
b/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
@@ -17,9 +17,11 @@
package org.apache.shenyu.sdk.httpclient;
+import com.sun.net.httpserver.HttpServer;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.apache.shenyu.sdk.core.ShenyuResponse;
import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
import org.apache.shenyu.sdk.core.client.ShenyuSdkClientFactory;
import org.apache.http.HttpResponse;
@@ -32,14 +34,18 @@ import org.mockito.Mockito;
import java.io.IOException;
import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.List;
import java.util.Properties;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertNotSame;
import static org.mockito.Mockito.mock;
@@ -48,18 +54,56 @@ import static org.mockito.Mockito.when;
public class HttpShenyuSdkClientTest {
- @Test
+ @Test(timeout = 15000)
public void testShenyuHttpClient() throws IOException {
- HttpShenyuSdkClient shenyuHttpClient = mock(HttpShenyuSdkClient.class,
Mockito.CALLS_REAL_METHODS);
+ HttpServer server = HttpServer.create(new
InetSocketAddress("127.0.0.1", 0), 0);
+ HttpShenyuSdkClient shenyuHttpClient = new HttpShenyuSdkClient();
+ AtomicReference<String> requestMethod = new AtomicReference<>();
+ AtomicReference<String> requestUri = new AtomicReference<>();
+ AtomicReference<List<String>> requestHeaders = new AtomicReference<>();
+ String responseBody = "{\"message\":\"本地响应\"}";
try {
- shenyuHttpClient.initClient(new Properties());
+ server.createContext("/sdk", exchange -> {
+ try {
+ requestMethod.set(exchange.getRequestMethod());
+ requestUri.set(exchange.getRequestURI().toString());
+
requestHeaders.set(exchange.getRequestHeaders().get("header"));
+ byte[] bytes =
responseBody.getBytes(StandardCharsets.UTF_8);
+ exchange.getResponseHeaders().set("Content-Type",
"application/json; charset=UTF-8");
+ exchange.getResponseHeaders().set("X-Fixture", "local");
+ exchange.sendResponseHeaders(201, bytes.length);
+ exchange.getResponseBody().write(bytes);
+ } finally {
+ exchange.close();
+ }
+ });
+ server.start();
+ Properties props = new Properties();
+ props.setProperty("http.serverRequestTimeOut", "5000");
+ props.setProperty("http.serverResponseTimeOut", "5000");
+ shenyuHttpClient.initClient(props);
Map<String, Collection<String>> headerMap = new HashMap<>();
headerMap.put("header", Arrays.asList("test1", "test2"));
- ShenyuRequest shenyuRequest =
ShenyuRequest.create(ShenyuRequest.HttpMethod.GET, "https://shenyu.apache.org",
+ String url = "http://127.0.0.1:" + server.getAddress().getPort() +
"/sdk?source=local";
+ ShenyuRequest shenyuRequest =
ShenyuRequest.create(ShenyuRequest.HttpMethod.GET, url,
headerMap, null, null, null);
-
when(shenyuHttpClient.doRequest(shenyuRequest)).thenCallRealMethod();
+
+ ShenyuResponse response =
shenyuHttpClient.doRequest(shenyuRequest);
+
+ Assert.assertEquals("GET", requestMethod.get());
+ Assert.assertEquals("/sdk?source=local", requestUri.get());
+ Assert.assertEquals(Arrays.asList("test1", "test2"),
requestHeaders.get());
+ Assert.assertEquals(201, response.getStatus());
+ Assert.assertEquals(responseBody, response.getBody());
+ Assert.assertSame(shenyuRequest, response.getRequest());
+ Assert.assertTrue(response.getHeaders().entrySet().stream()
+ .anyMatch(header ->
"X-Fixture".equalsIgnoreCase(header.getKey()) &&
header.getValue().contains("local")));
} finally {
- shenyuHttpClient.close();
+ try {
+ shenyuHttpClient.close();
+ } finally {
+ server.stop(0);
+ }
}
}