This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new c1033ecded fix: specify UTF-8 charset for SSE event and comment byte
encoding and add corresponding unit tests (#6930)
c1033ecded is described below
commit c1033ecded588f9bf816a6615e7dd4347321d6e5
Author: Limbo <[email protected]>
AuthorDate: Tue Sep 15 23:40:40 2026 +0800
fix: specify UTF-8 charset for SSE event and comment byte encoding and add
corresponding unit tests (#6930)
Co-authored-by: Liming Deng <[email protected]>
---
.../mcp/server/transport/SseEventFormatter.java | 5 +-
.../server/transport/SseEventFormatterTest.java | 67 ++++++++++++++++++++++
2 files changed, 70 insertions(+), 2 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatter.java
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatter.java
index 5cbcdf6eaf..d20509a7d5 100644
---
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatter.java
+++
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatter.java
@@ -23,6 +23,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.server.ServerWebExchange;
+import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
@@ -73,7 +74,7 @@ public final class SseEventFormatter {
String formattedEvent = sseData.toString();
LOG.debug("Formatted SSE event: {}", formattedEvent.trim());
- return
exchange.getResponse().bufferFactory().wrap(formattedEvent.getBytes());
+ return
exchange.getResponse().bufferFactory().wrap(formattedEvent.getBytes(StandardCharsets.UTF_8));
}
/**
@@ -86,6 +87,6 @@ public final class SseEventFormatter {
public static DataBuffer formatComment(final String comment, final
ServerWebExchange exchange) {
String formattedComment = ": " + comment + "\n\n";
LOG.debug("Formatted SSE comment: {}", formattedComment.trim());
- return
exchange.getResponse().bufferFactory().wrap(formattedComment.getBytes());
+ return
exchange.getResponse().bufferFactory().wrap(formattedComment.getBytes(StandardCharsets.UTF_8));
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatterTest.java
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatterTest.java
new file mode 100644
index 0000000000..18eb658e35
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/transport/SseEventFormatterTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.mcp.server.transport;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.core.io.buffer.DataBufferUtils;
+import org.springframework.http.codec.ServerSentEvent;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+/**
+ * Test cases for {@link SseEventFormatter}.
+ */
+final class SseEventFormatterTest {
+
+ @Test
+ void testFormatEventUsesUtf8() {
+ MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("/mcp/sse").build());
+ ServerSentEvent<String> event =
ServerSentEvent.builder("{\"message\":\"你好,世界\"}")
+ .event("message")
+ .build();
+
+ DataBuffer buffer = SseEventFormatter.formatEvent(event, exchange);
+
+ assertArrayEquals("event: message\ndata:
{\"message\":\"你好,世界\"}\n\n".getBytes(StandardCharsets.UTF_8),
+ readAndRelease(buffer));
+ }
+
+ @Test
+ void testFormatCommentUsesUtf8() {
+ MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("/mcp/sse").build());
+
+ DataBuffer buffer = SseEventFormatter.formatComment("保持连接", exchange);
+
+ assertArrayEquals(": 保持连接\n\n".getBytes(StandardCharsets.UTF_8),
readAndRelease(buffer));
+ }
+
+ private static byte[] readAndRelease(final DataBuffer buffer) {
+ try {
+ byte[] bytes = new byte[buffer.readableByteCount()];
+ buffer.read(bytes);
+ return bytes;
+ } finally {
+ DataBufferUtils.release(buffer);
+ }
+ }
+}