fix: handle empty JSON responses in ResponseSubscribers
- Remove length check in hookOnComplete() to always emit AggregateResponseEvent - Ensures proper completion handling regardless of response content length - Add test for empty application/json responses with 200 OK status
This commit is contained in:
parent
3a95f757f3
commit
5aba8a0e8f
2 changed files with 95 additions and 4 deletions
|
|
@ -238,10 +238,9 @@ class ResponseSubscribers {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void hookOnComplete() {
|
protected void hookOnComplete() {
|
||||||
if (this.eventBuilder.length() > 0) {
|
String data = this.eventBuilder.toString();
|
||||||
String data = this.eventBuilder.toString();
|
this.sink.next(new AggregateResponseEvent(responseInfo, data));
|
||||||
this.sink.next(new AggregateResponseEvent(responseInfo, data));
|
|
||||||
}
|
|
||||||
this.sink.complete();
|
this.sink.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2024-2025 the original author or authors.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.modelcontextprotocol.client.transport;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.atLeastOnce;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.Timeout;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
|
||||||
|
import io.modelcontextprotocol.server.transport.TomcatTestUtil;
|
||||||
|
import io.modelcontextprotocol.spec.McpSchema;
|
||||||
|
import io.modelcontextprotocol.spec.ProtocolVersions;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles emplty application/json response with 200 OK status code.
|
||||||
|
*
|
||||||
|
* @author codezkk
|
||||||
|
*/
|
||||||
|
public class HttpClientStreamableHttpTransportEmptyJsonResponseTest {
|
||||||
|
|
||||||
|
static int PORT = TomcatTestUtil.findAvailablePort();
|
||||||
|
|
||||||
|
static String host = "http://localhost:" + PORT;
|
||||||
|
|
||||||
|
static HttpServer server;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void startContainer() throws IOException {
|
||||||
|
|
||||||
|
server = HttpServer.create(new InetSocketAddress(PORT), 0);
|
||||||
|
|
||||||
|
// Empty, 200 OK response for the /mcp endpoint
|
||||||
|
server.createContext("/mcp", exchange -> {
|
||||||
|
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||||
|
exchange.sendResponseHeaders(200, 0);
|
||||||
|
exchange.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.setExecutor(null);
|
||||||
|
server.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void stopContainer() {
|
||||||
|
server.stop(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regardless of the response (even if the response is null and the content-type is
|
||||||
|
* present), notify should handle it correctly.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(3)
|
||||||
|
void testNotificationInitialized() throws URISyntaxException {
|
||||||
|
|
||||||
|
var uri = new URI(host + "/mcp");
|
||||||
|
var mockRequestCustomizer = mock(SyncHttpRequestCustomizer.class);
|
||||||
|
var transport = HttpClientStreamableHttpTransport.builder(host)
|
||||||
|
.httpRequestCustomizer(mockRequestCustomizer)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
var initializeRequest = new McpSchema.InitializeRequest(ProtocolVersions.MCP_2025_03_26,
|
||||||
|
McpSchema.ClientCapabilities.builder().roots(true).build(),
|
||||||
|
new McpSchema.Implementation("Spring AI MCP Client", "0.3.1"));
|
||||||
|
var testMessage = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_INITIALIZE,
|
||||||
|
"test-id", initializeRequest);
|
||||||
|
|
||||||
|
StepVerifier.create(transport.sendMessage(testMessage)).verifyComplete();
|
||||||
|
|
||||||
|
// Verify the customizer was called
|
||||||
|
verify(mockRequestCustomizer, atLeastOnce()).customize(any(), eq("GET"), eq(uri), eq(
|
||||||
|
"{\"jsonrpc\":\"2.0\",\"method\":\"initialize\",\"id\":\"test-id\",\"params\":{\"protocolVersion\":\"2025-03-26\",\"capabilities\":{\"roots\":{\"listChanged\":true}},\"clientInfo\":{\"name\":\"Spring AI MCP Client\",\"version\":\"0.3.1\"}}}"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue