|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.client.transport; |
| 6 | + |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpHeaders; |
| 9 | +import java.net.http.HttpResponse; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import reactor.core.publisher.Flux; |
| 16 | +import reactor.test.StepVerifier; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for {@link ResponseSubscribers.SseLineSubscriber}. |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * Verifies that SSE field values are extracted per the <a href= |
| 25 | + * "https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation"> |
| 26 | + * WHATWG HTML Living Standard §9.2.6</a>: the field value is everything after the colon |
| 27 | + * minus a single leading space. In particular, U+2028 (LINE SEPARATOR), U+2029 (PARAGRAPH |
| 28 | + * SEPARATOR) and U+0085 (NEXT LINE) are legal inside a field value and must not truncate |
| 29 | + * it — they are not SSE line terminators. |
| 30 | + * |
| 31 | + * @see <a href="https://github.com/modelcontextprotocol/java-sdk/issues/1136">#1136</a> |
| 32 | + */ |
| 33 | +class ResponseSubscribersTest { |
| 34 | + |
| 35 | + private static final HttpResponse.ResponseInfo RESPONSE_INFO = new HttpResponse.ResponseInfo() { |
| 36 | + |
| 37 | + @Override |
| 38 | + public int statusCode() { |
| 39 | + return 200; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public HttpHeaders headers() { |
| 44 | + return HttpHeaders.of(Map.of(), (name, value) -> true); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public HttpClient.Version version() { |
| 49 | + return HttpClient.Version.HTTP_1_1; |
| 50 | + } |
| 51 | + |
| 52 | + }; |
| 53 | + |
| 54 | + private static List<ResponseSubscribers.SseEvent> parse(List<String> lines) { |
| 55 | + return Flux.<ResponseSubscribers.ResponseEvent>create(sink -> Flux.fromIterable(lines) |
| 56 | + .subscribe(new ResponseSubscribers.SseLineSubscriber(RESPONSE_INFO, sink, Integer.MAX_VALUE))) |
| 57 | + .map(event -> ((ResponseSubscribers.SseResponseEvent) event).sseEvent()) |
| 58 | + .collectList() |
| 59 | + .block(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * A {@code data:} payload containing U+2028, U+2029 or U+0085 must survive parsing |
| 64 | + * intact. A MULTILINE regex used to truncate the value at those characters, because |
| 65 | + * the Java regex engine treats them as line terminators. |
| 66 | + */ |
| 67 | + @Test |
| 68 | + void shouldNotTruncateDataAtUnicodeLineSeparators() { |
| 69 | + List<String> separators = List.of("\u2028", "\u2029", "\u0085"); |
| 70 | + |
| 71 | + for (String separator : separators) { |
| 72 | + String payload = "{\"text\":\"a" + separator + "b\"}"; |
| 73 | + List<ResponseSubscribers.SseEvent> events = parse(List.of("data: " + payload, "")); |
| 74 | + |
| 75 | + assertThat(events).as("payload with U+%04X", (int) separator.charAt(0)).hasSize(1); |
| 76 | + assertThat(events.get(0).data()).isEqualTo(payload); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldPreserveVerticalTabInData() { |
| 82 | + String payload = "{\"text\":\"a\u000Bb\"}"; |
| 83 | + |
| 84 | + List<ResponseSubscribers.SseEvent> events = parse(List.of("data: " + payload, "")); |
| 85 | + |
| 86 | + assertThat(events).hasSize(1); |
| 87 | + assertThat(events.get(0).data()).isEqualTo(payload); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void shouldStripOnlySingleLeadingSpacePerDataLine() { |
| 92 | + // The leading space after the colon is stripped per line; any further |
| 93 | + // whitespace is part of the value. The whole-event trim only affects the |
| 94 | + // first/last line, so the interior line keeps its second space. |
| 95 | + List<ResponseSubscribers.SseEvent> events = parse(List.of("data: first", "data: second", "")); |
| 96 | + |
| 97 | + assertThat(events).hasSize(1); |
| 98 | + assertThat(events.get(0).data()).isEqualTo("first\n second"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void shouldJoinMultipleDataLinesWithNewline() { |
| 103 | + List<ResponseSubscribers.SseEvent> events = parse(List.of("data: first", "data: second", "")); |
| 104 | + |
| 105 | + assertThat(events).hasSize(1); |
| 106 | + assertThat(events.get(0).data()).isEqualTo("first\nsecond"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void shouldCaptureEventIdAndTypeWithUnicodeValue() { |
| 111 | + List<ResponseSubscribers.SseEvent> events = parse( |
| 112 | + List.of("event: message\u2028tail", "id: 42\u2028tail", "data: body", "")); |
| 113 | + |
| 114 | + assertThat(events).hasSize(1); |
| 115 | + assertThat(events.get(0).event()).isEqualTo("message\u2028tail"); |
| 116 | + assertThat(events.get(0).id()).isEqualTo("42\u2028tail"); |
| 117 | + assertThat(events.get(0).data()).isEqualTo("body"); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments