Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.server.ServerWebExchange;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public static DataBuffer formatEvent(final ServerSentEvent<?> event, final Serve
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));
}

/**
Expand All @@ -86,6 +87,6 @@ public static DataBuffer formatEvent(final ServerSentEvent<?> event, final Serve
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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading