Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,15 @@ private void doLongPolling(final String server) {
Assert.notNull(responseBody, "Resolve response body failed.");
String json = responseBody.string();
LOG.info("listener result: [{}]", json);
JsonObject responseFromServer = GsonUtils.getGson().fromJson(json, JsonObject.class);
JsonElement element = responseFromServer.get("data");
if (element.isJsonNull()) {
JsonElement responseFromServer = GsonUtils.getGson().fromJson(json, JsonElement.class);
if (Objects.isNull(responseFromServer) || responseFromServer.isJsonNull()) {
return;
}
groupJson = responseFromServer.getAsJsonArray("data");
JsonElement element = responseFromServer.getAsJsonObject().get("data");
if (Objects.isNull(element) || element.isJsonNull()) {
return;
}
groupJson = element.getAsJsonArray();
} catch (IOException e) {
String message = String.format("listener configs fail, server:[%s], %s", server, e.getMessage());
throw new ShenyuException(message, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.apache.shenyu.common.config.ShenyuConfig;
import org.apache.shenyu.common.constant.HttpConstants;
import org.apache.shenyu.common.dto.ConfigData;
Expand All @@ -33,6 +34,7 @@
import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
import org.apache.shenyu.sync.data.api.AiProxyApiKeyDataSubscriber;
import org.apache.shenyu.sync.data.http.config.HttpConfig;
import org.apache.shenyu.sync.data.http.refresh.AbstractDataRefresh;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -58,6 +60,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -90,6 +94,8 @@ public final class HttpSyncDataServiceTest {

@BeforeEach
public void before() {
Map<?, ?> groupCache = (Map<?, ?>) ReflectionTestUtils.getField(AbstractDataRefresh.class, "GROUP_CACHE");
Objects.requireNonNull(groupCache).clear();
this.wireMockServer = new WireMockServer(
options()
.extensions(mock(ResponseTemplateTransformer.class))
Expand Down Expand Up @@ -165,6 +171,35 @@ public void test() {
verify(proxySelectorDataSubscriber, atLeastOnce()).refresh();
}

@Test
public void testDoLongPollingIgnoresResponseWithoutData() {
httpSyncDataService.close();
assertAll(
() -> assertLongPollingResponseIgnored("{\"code\":500,\"message\":\"error\"}"),
() -> assertLongPollingResponseIgnored("{\"data\":null}"),
() -> assertLongPollingResponseIgnored("null"),
() -> assertLongPollingResponseIgnored("")
);
}

private void assertLongPollingResponseIgnored(final String responseBody) {
StubMapping stubMapping = wireMockServer.stubFor(post(urlPathEqualTo("/configs/listener"))
.atPriority(1)
.willReturn(aResponse()
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
.withBody(responseBody)
.withStatus(200))
);

try {
assertDoesNotThrow(() -> {
ReflectionTestUtils.invokeMethod(httpSyncDataService, "doLongPolling", getMockServerUrl());
});
} finally {
wireMockServer.removeStub(stubMapping);
}
}

private String getMockServerUrl() {
return "http://127.0.0.1:" + wireMockServer.port();
}
Expand Down
Loading