Skip to content

Commit 07a6f38

Browse files
committed
Put content-length header tests in specific test classes
Spring AI uses the abstract base class and has other APIs for limiting header size. Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 7aa05da commit 07a6f38

3 files changed

Lines changed: 100 additions & 47 deletions

File tree

‎mcp-test/src/main/java/io/modelcontextprotocol/AbstractMcpClientServerIntegrationTests.java‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@
7272

7373
public abstract class AbstractMcpClientServerIntegrationTests {
7474

75-
protected static final int MAX_REQUEST_SIZE = 2048;
76-
7775
protected ConcurrentHashMap<String, McpClient.SyncSpec> clientBuilders = new ConcurrentHashMap<>();
7876

7977
abstract protected void prepareClients(int port, String mcpEndpoint);
@@ -2430,51 +2428,6 @@ void testResourceSubscription_afterUnsubscribe_noNotification(String clientType)
24302428
}
24312429
}
24322430

2433-
// Bounded read
2434-
@ParameterizedTest(name = "{0} : {displayName} ")
2435-
@MethodSource("clientsForTesting")
2436-
void testRejectsWhenContentLengthHeaderExceedsLimit(String clientType) throws Exception {
2437-
var clientBuilder = clientBuilders.get(clientType);
2438-
2439-
String inputSchema = """
2440-
{
2441-
"type": "object",
2442-
"properties": {
2443-
"message": { "type": "string" }
2444-
},
2445-
"required": ["message"]
2446-
}
2447-
""";
2448-
2449-
McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder()
2450-
.tool(Tool.builder("tool1", McpJsonDefaults.getMapper(), inputSchema)
2451-
.description("tool1 description")
2452-
.build())
2453-
.callHandler((exchange, request) -> CallToolResult.builder()
2454-
.addContent(TextContent.builder(request.arguments().get("message").toString()).build())
2455-
.build())
2456-
.build();
2457-
2458-
var mcpServer = prepareSyncServerBuilder().capabilities(ServerCapabilities.builder().tools(false).build())
2459-
.tools(tool1)
2460-
.build();
2461-
2462-
try (var mcpClient = clientBuilder.build()) {
2463-
String oversizedBody = "a".repeat(MAX_REQUEST_SIZE + 1);
2464-
2465-
mcpClient.initialize();
2466-
assertThat(mcpClient.listTools().tools()).contains(tool1.tool());
2467-
2468-
assertThatThrownBy(() -> mcpClient.callTool(
2469-
McpSchema.CallToolRequest.builder("tool1").arguments(Map.of("message", oversizedBody)).build()))
2470-
.isInstanceOf(RuntimeException.class)
2471-
.hasMessageContaining("413");
2472-
}
2473-
finally {
2474-
mcpServer.closeGracefully();
2475-
}
2476-
}
2477-
24782431
private double evaluateExpression(String expression) {
24792432
// Simple expression evaluator for testing
24802433
return switch (expression) {

‎mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletSseIntegrationTests.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.modelcontextprotocol.client.McpClient;
2525
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
2626
import io.modelcontextprotocol.common.McpTransportContext;
27+
import io.modelcontextprotocol.json.McpJsonDefaults;
2728
import io.modelcontextprotocol.server.McpServer.AsyncSpecification;
2829
import io.modelcontextprotocol.server.McpServer.SyncSpecification;
2930
import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider;
@@ -42,11 +43,13 @@
4243
import org.junit.jupiter.api.Timeout;
4344
import org.junit.jupiter.params.ParameterizedTest;
4445
import org.junit.jupiter.params.provider.Arguments;
46+
import org.junit.jupiter.params.provider.MethodSource;
4547
import org.junit.jupiter.params.provider.ValueSource;
4648
import reactor.core.publisher.Mono;
4749

4850
import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA;
4951
import static org.assertj.core.api.Assertions.assertThat;
52+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5053

5154
@Timeout(15)
5255
class HttpServletSseIntegrationTests extends AbstractMcpClientServerIntegrationTests {
@@ -57,6 +60,8 @@ class HttpServletSseIntegrationTests extends AbstractMcpClientServerIntegrationT
5760

5861
private static final String CUSTOM_MESSAGE_ENDPOINT = "/otherPath/mcp/message";
5962

63+
protected static final int MAX_REQUEST_SIZE = 2048;
64+
6065
// Tomcat is started once for the whole class; each test swaps in its own transport
6166
private static final TomcatTestUtil.DelegatingServlet MCP_SERVLET = new TomcatTestUtil.DelegatingServlet();
6267

@@ -132,6 +137,51 @@ public void after() {
132137
protected void prepareClients(int port, String mcpEndpoint) {
133138
}
134139

140+
@ParameterizedTest(name = "{0} : {displayName} ")
141+
@MethodSource("clientsForTesting")
142+
void testRejectsWhenContentLengthHeaderExceedsLimit(String clientType) {
143+
var clientBuilder = clientBuilders.get(clientType);
144+
145+
String inputSchema = """
146+
{
147+
"type": "object",
148+
"properties": {
149+
"message": { "type": "string" }
150+
},
151+
"required": ["message"]
152+
}
153+
""";
154+
155+
McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder()
156+
.tool(McpSchema.Tool.builder("tool1", McpJsonDefaults.getMapper(), inputSchema)
157+
.description("tool1 description")
158+
.build())
159+
.callHandler((exchange, request) -> McpSchema.CallToolResult.builder()
160+
.addContent(McpSchema.TextContent.builder(request.arguments().get("message").toString()).build())
161+
.build())
162+
.build();
163+
164+
var mcpServer = prepareSyncServerBuilder()
165+
.capabilities(McpSchema.ServerCapabilities.builder().tools(false).build())
166+
.tools(tool1)
167+
.build();
168+
169+
try (var mcpClient = clientBuilder.build()) {
170+
String oversizedBody = "a".repeat(MAX_REQUEST_SIZE + 1);
171+
172+
mcpClient.initialize();
173+
assertThat(mcpClient.listTools().tools()).contains(tool1.tool());
174+
175+
assertThatThrownBy(() -> mcpClient.callTool(
176+
McpSchema.CallToolRequest.builder("tool1").arguments(Map.of("message", oversizedBody)).build()))
177+
.isInstanceOf(RuntimeException.class)
178+
.hasMessageContaining("413");
179+
}
180+
finally {
181+
mcpServer.closeGracefully();
182+
}
183+
}
184+
135185
@Test
136186
void rejectsWhenBodyBytesExceedLimitWithoutContentLengthHeader() throws Exception {
137187
var httpClient = HttpClient.newHttpClient();

‎mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletStreamableIntegrationTests.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.modelcontextprotocol.client.McpClient;
3131
import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
3232
import io.modelcontextprotocol.common.McpTransportContext;
33+
import io.modelcontextprotocol.json.McpJsonDefaults;
3334
import io.modelcontextprotocol.server.McpServer.AsyncSpecification;
3435
import io.modelcontextprotocol.server.McpServer.SyncSpecification;
3536
import io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider;
@@ -50,6 +51,7 @@
5051
import org.junit.jupiter.api.Timeout;
5152
import org.junit.jupiter.params.ParameterizedTest;
5253
import org.junit.jupiter.params.provider.Arguments;
54+
import org.junit.jupiter.params.provider.MethodSource;
5355
import org.junit.jupiter.params.provider.ValueSource;
5456
import org.slf4j.LoggerFactory;
5557
import reactor.core.publisher.Flux;
@@ -61,6 +63,7 @@
6163
import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA;
6264
import static java.nio.charset.StandardCharsets.UTF_8;
6365
import static org.assertj.core.api.Assertions.assertThat;
66+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6467
import static org.awaitility.Awaitility.await;
6568

6669
/**
@@ -90,6 +93,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
9093
// result in a session sweep
9194
private final Duration SESSION_SWEEP_INTERVAL = KEEP_ALIVE_INTERVAL.multipliedBy(2);
9295

96+
protected static final int MAX_REQUEST_SIZE = 2048;
97+
9398
private final HttpClient httpClient = HttpClient.newHttpClient();
9499

95100
@Override
@@ -253,6 +258,51 @@ public void cancel() {
253258
assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
254259
}
255260

261+
@ParameterizedTest(name = "{0} : {displayName} ")
262+
@MethodSource("clientsForTesting")
263+
void testRejectsWhenContentLengthHeaderExceedsLimit(String clientType) {
264+
var clientBuilder = clientBuilders.get(clientType);
265+
266+
String inputSchema = """
267+
{
268+
"type": "object",
269+
"properties": {
270+
"message": { "type": "string" }
271+
},
272+
"required": ["message"]
273+
}
274+
""";
275+
276+
McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder()
277+
.tool(McpSchema.Tool.builder("tool1", McpJsonDefaults.getMapper(), inputSchema)
278+
.description("tool1 description")
279+
.build())
280+
.callHandler((exchange, request) -> McpSchema.CallToolResult.builder()
281+
.addContent(McpSchema.TextContent.builder(request.arguments().get("message").toString()).build())
282+
.build())
283+
.build();
284+
285+
var mcpServer = prepareSyncServerBuilder()
286+
.capabilities(McpSchema.ServerCapabilities.builder().tools(false).build())
287+
.tools(tool1)
288+
.build();
289+
290+
try (var mcpClient = clientBuilder.build()) {
291+
String oversizedBody = "a".repeat(MAX_REQUEST_SIZE + 1);
292+
293+
mcpClient.initialize();
294+
assertThat(mcpClient.listTools().tools()).contains(tool1.tool());
295+
296+
assertThatThrownBy(() -> mcpClient.callTool(
297+
McpSchema.CallToolRequest.builder("tool1").arguments(Map.of("message", oversizedBody)).build()))
298+
.isInstanceOf(RuntimeException.class)
299+
.hasMessageContaining("413");
300+
}
301+
finally {
302+
mcpServer.closeGracefully();
303+
}
304+
}
305+
256306
@ParameterizedTest
257307
@ValueSource(strings = { "text/plain;charset=UTF-8", "application/x-www-form-urlencoded", "multipart/form-data" })
258308
void rejectsInitializeWithNonJsonContentType(String contentType) throws Exception {

0 commit comments

Comments
 (0)