|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server.transport; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.PrintWriter; |
| 9 | +import java.io.StringReader; |
| 10 | +import java.io.StringWriter; |
| 11 | + |
| 12 | +import jakarta.servlet.http.HttpServletRequest; |
| 13 | +import jakarta.servlet.http.HttpServletResponse; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Nested; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.params.ParameterizedTest; |
| 18 | +import org.junit.jupiter.params.provider.ValueSource; |
| 19 | + |
| 20 | +import io.modelcontextprotocol.spec.HttpHeaders; |
| 21 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +/** |
| 29 | + * Unit tests for {@link HttpServletStreamableServerTransportProvider} HTTP request header |
| 30 | + * validation. |
| 31 | + */ |
| 32 | +class HttpServletStreamableServerTransportProviderTests { |
| 33 | + |
| 34 | + private final HttpServletStreamableServerTransportProvider transport = HttpServletStreamableServerTransportProvider |
| 35 | + .builder() |
| 36 | + .build(); |
| 37 | + |
| 38 | + private final HttpServletRequest request = mock(HttpServletRequest.class); |
| 39 | + |
| 40 | + private final HttpServletResponse response = mock(HttpServletResponse.class); |
| 41 | + |
| 42 | + private final StringWriter responseBody = new StringWriter(); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() throws Exception { |
| 46 | + when(request.getRequestURI()).thenReturn("/mcp"); |
| 47 | + when(response.getWriter()).thenReturn(new PrintWriter(responseBody)); |
| 48 | + } |
| 49 | + |
| 50 | + @Nested |
| 51 | + class ProtocolVersionHeader { |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @ValueSource(strings = { ProtocolVersions.MCP_2024_11_05, ProtocolVersions.MCP_2025_03_26, |
| 55 | + ProtocolVersions.MCP_2025_06_18, ProtocolVersions.MCP_2025_11_25 }) |
| 56 | + void doGetWithSupportedProtocolVersionProceeds(String protocolVersion) throws Exception { |
| 57 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 58 | + |
| 59 | + transport.doGet(request, response); |
| 60 | + |
| 61 | + assertThat(responseBody.toString()) |
| 62 | + .as("a supported protocol version must not be rejected") |
| 63 | + .doesNotContain("Unsupported protocol version") |
| 64 | + .contains("Session ID required in mcp-session-id header"); |
| 65 | + } |
| 66 | + |
| 67 | + @ParameterizedTest |
| 68 | + @ValueSource(strings = { "banana", "1999-01-01" }) |
| 69 | + void doGetWithUnsupportedProtocolVersionRejected(String protocolVersion) throws Exception { |
| 70 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 71 | + |
| 72 | + transport.doGet(request, response); |
| 73 | + |
| 74 | + verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 75 | + assertThat(responseBody.toString()).contains("Unsupported protocol version"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void doGetWithMissingProtocolVersionProceeds() throws Exception { |
| 80 | + transport.doGet(request, response); |
| 81 | + |
| 82 | + assertThat(responseBody.toString()) |
| 83 | + .as("a missing protocol version must fall back to the negotiated version") |
| 84 | + .doesNotContain("Unsupported protocol version") |
| 85 | + .contains("Session ID required in mcp-session-id header"); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest |
| 89 | + @ValueSource(strings = { ProtocolVersions.MCP_2024_11_05, ProtocolVersions.MCP_2025_03_26, |
| 90 | + ProtocolVersions.MCP_2025_06_18, ProtocolVersions.MCP_2025_11_25 }) |
| 91 | + void doPostWithSupportedProtocolVersionProceeds(String protocolVersion) throws Exception { |
| 92 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 93 | + when(request.getReader()).thenReturn(new BufferedReader(new StringReader(""))); |
| 94 | + |
| 95 | + transport.doPost(request, response); |
| 96 | + |
| 97 | + assertThat(responseBody.toString()) |
| 98 | + .as("a supported protocol version must not be rejected") |
| 99 | + .doesNotContain("Unsupported protocol version") |
| 100 | + .contains("Invalid message format"); |
| 101 | + } |
| 102 | + |
| 103 | + @ParameterizedTest |
| 104 | + @ValueSource(strings = { "banana", "1999-01-01" }) |
| 105 | + void doPostWithUnsupportedProtocolVersionRejected(String protocolVersion) throws Exception { |
| 106 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 107 | + |
| 108 | + transport.doPost(request, response); |
| 109 | + |
| 110 | + verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 111 | + assertThat(responseBody.toString()).contains("Unsupported protocol version"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void doPostWithMissingProtocolVersionProceeds() throws Exception { |
| 116 | + when(request.getReader()).thenReturn(new BufferedReader(new StringReader(""))); |
| 117 | + |
| 118 | + transport.doPost(request, response); |
| 119 | + |
| 120 | + assertThat(responseBody.toString()) |
| 121 | + .as("a missing protocol version must fall back to the negotiated version") |
| 122 | + .doesNotContain("Unsupported protocol version") |
| 123 | + .contains("Invalid message format"); |
| 124 | + } |
| 125 | + |
| 126 | + @ParameterizedTest |
| 127 | + @ValueSource(strings = { ProtocolVersions.MCP_2024_11_05, ProtocolVersions.MCP_2025_03_26, |
| 128 | + ProtocolVersions.MCP_2025_06_18, ProtocolVersions.MCP_2025_11_25 }) |
| 129 | + void doDeleteWithSupportedProtocolVersionProceeds(String protocolVersion) throws Exception { |
| 130 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 131 | + |
| 132 | + transport.doDelete(request, response); |
| 133 | + |
| 134 | + assertThat(responseBody.toString()) |
| 135 | + .as("a supported protocol version must not be rejected") |
| 136 | + .doesNotContain("Unsupported protocol version") |
| 137 | + .contains("Session ID required in mcp-session-id header"); |
| 138 | + } |
| 139 | + |
| 140 | + @ParameterizedTest |
| 141 | + @ValueSource(strings = { "banana", "1999-01-01" }) |
| 142 | + void doDeleteWithUnsupportedProtocolVersionRejected(String protocolVersion) throws Exception { |
| 143 | + when(request.getHeader(HttpHeaders.PROTOCOL_VERSION)).thenReturn(protocolVersion); |
| 144 | + |
| 145 | + transport.doDelete(request, response); |
| 146 | + |
| 147 | + verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 148 | + assertThat(responseBody.toString()).contains("Unsupported protocol version"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void doDeleteWithMissingProtocolVersionProceeds() throws Exception { |
| 153 | + transport.doDelete(request, response); |
| 154 | + |
| 155 | + assertThat(responseBody.toString()) |
| 156 | + .as("a missing protocol version must fall back to the negotiated version") |
| 157 | + .doesNotContain("Unsupported protocol version") |
| 158 | + .contains("Session ID required in mcp-session-id header"); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments