|
| 1 | +package io.modelcontextprotocol.server.transport; |
| 2 | + |
| 3 | +import java.io.PrintWriter; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | + |
| 7 | +import jakarta.servlet.AsyncContext; |
| 8 | +import jakarta.servlet.http.HttpServletRequest; |
| 9 | +import jakarta.servlet.http.HttpServletResponse; |
| 10 | + |
| 11 | +import io.modelcontextprotocol.common.McpTransportContext; |
| 12 | +import io.modelcontextprotocol.spec.HttpHeaders; |
| 13 | +import io.modelcontextprotocol.spec.McpStreamableServerSession; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.verify; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit tests for the GET stream of {@link HttpServletStreamableServerTransportProvider}. |
| 24 | + */ |
| 25 | +class HttpServletStreamableServerTransportProviderTests { |
| 26 | + |
| 27 | + private HttpServletStreamableServerTransportProvider provider; |
| 28 | + |
| 29 | + private final Map<String, McpStreamableServerSession> sessions = new ConcurrentHashMap<>(); |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() throws Exception { |
| 33 | + this.provider = HttpServletStreamableServerTransportProvider.builder() |
| 34 | + .jsonMapper(mock(io.modelcontextprotocol.json.McpJsonMapper.class)) |
| 35 | + .mcpEndpoint("/mcp") |
| 36 | + .contextExtractor(request -> McpTransportContext.EMPTY) |
| 37 | + .build(); |
| 38 | + |
| 39 | + McpStreamableServerSession session = mock(McpStreamableServerSession.class); |
| 40 | + McpStreamableServerSession.McpStreamableServerSessionStream stream = mock( |
| 41 | + McpStreamableServerSession.McpStreamableServerSessionStream.class); |
| 42 | + when(session.listeningStream(any())).thenReturn(stream); |
| 43 | + |
| 44 | + var sessionsField = HttpServletStreamableServerTransportProvider.class.getDeclaredField("sessions"); |
| 45 | + sessionsField.setAccessible(true); |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + Map<String, McpStreamableServerSession> sessions = (Map<String, McpStreamableServerSession>) sessionsField |
| 48 | + .get(this.provider); |
| 49 | + sessions.put("session-1", session); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void getStreamCommitsResponseHeadersImmediately() throws Exception { |
| 54 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 55 | + when(request.getRequestURI()).thenReturn("/mcp"); |
| 56 | + when(request.getHeader("Accept")).thenReturn("text/event-stream"); |
| 57 | + when(request.getHeader(HttpHeaders.MCP_SESSION_ID)).thenReturn("session-1"); |
| 58 | + when(request.getHeader("Last-Event-ID")).thenReturn(null); |
| 59 | + AsyncContext asyncContext = mock(AsyncContext.class); |
| 60 | + when(request.startAsync()).thenReturn(asyncContext); |
| 61 | + |
| 62 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 63 | + when(response.getWriter()).thenReturn(mock(PrintWriter.class)); |
| 64 | + |
| 65 | + this.provider.doGet(request, response); |
| 66 | + |
| 67 | + // The status line and the SSE headers must be committed as soon as the stream |
| 68 | + // opens, |
| 69 | + // before any event is written (see gh-1155) |
| 70 | + verify(response).flushBuffer(); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments