Copilot commented on code in PR #7451: URL: https://github.com/apache/incubator-seata/pull/7451#discussion_r2156987127
########## core/src/test/java/org/apache/seata/core/rpc/netty/http/Http2HttpHandlerTest.java: ########## @@ -0,0 +1,183 @@ +/* + * 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.seata.core.rpc.netty.http; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.netty.buffer.Unpooled; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.http2.DefaultHttp2DataFrame; +import io.netty.handler.codec.http2.DefaultHttp2Headers; +import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame; +import io.netty.handler.codec.http2.Http2Headers; +import io.netty.handler.codec.http2.Http2HeadersFrame; +import io.netty.handler.codec.http2.Http2StreamFrame; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class Http2HttpHandlerTest { + private Http2HttpHandler handler; + private EmbeddedChannel channel; + private TestController testController = new TestController(); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + class TestController { + public String handleRequest(String param) { + return "Processed: " + param; + } + } + + @BeforeEach + void setUp() throws Exception { + handler = new Http2HttpHandler(); + channel = new EmbeddedChannel(handler); + // 注册controller + Method method = TestController.class.getMethod("handleRequest", String.class); + ParamMetaData paramMetaData = new ParamMetaData(); + paramMetaData.setParamConvertType(ParamMetaData.ParamConvertType.REQUEST_PARAM); + paramMetaData.setParamName("param"); + ParamMetaData[] paramMetaDatas = new ParamMetaData[]{paramMetaData}; + HttpInvocation invocation = new HttpInvocation(); + invocation.setController(testController); + invocation.setMethod(method); + invocation.setPath("/test"); + invocation.setParamMetaData(paramMetaDatas); + ControllerManager.addHttpInvocation(invocation); + } + + private Http2StreamFrame waitForHttp2Response(long timeoutMs) { + long startTime = System.currentTimeMillis(); + Http2StreamFrame response = null; + while (response == null && (System.currentTimeMillis() - startTime) < timeoutMs) { + response = channel.readOutbound(); + if (response == null) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException("Interrupted while waiting for response", e); + } + } + } + return response; + } + + @Test + void testHttp2GetRequestWithParameters() throws Exception { + // 构造 GET 请求 + Http2Headers headers = new DefaultHttp2Headers(); + headers.method("GET"); + headers.path("/test?param=testValue"); + Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true); + channel.writeInbound(headersFrame); + + // 使用等待方法获取响应 + Http2StreamFrame responseHeadersFrame = waitForHttp2Response(5000); + assertNotNull(responseHeadersFrame); + assertTrue(responseHeadersFrame instanceof DefaultHttp2HeadersFrame); + DefaultHttp2HeadersFrame respHeaders = (DefaultHttp2HeadersFrame) responseHeadersFrame; + assertEquals("200", respHeaders.headers().status().toString()); + + Http2StreamFrame responseDataFrame = waitForHttp2Response(5000); + assertNotNull(responseDataFrame); + assertTrue(responseDataFrame instanceof DefaultHttp2DataFrame); + DefaultHttp2DataFrame respData = (DefaultHttp2DataFrame) responseDataFrame; + String content = respData.content().toString(StandardCharsets.UTF_8); + assertTrue(content.contains("Processed: testValue")); + } + + @Test + void testHttp2RequestToNonexistentPath() { + Http2Headers headers = new DefaultHttp2Headers(); + headers.method("GET"); + headers.path("/notfound"); + Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true); + channel.writeInbound(headersFrame); + + Http2StreamFrame responseHeadersFrame = channel.readOutbound(); + assertTrue(responseHeadersFrame instanceof DefaultHttp2HeadersFrame); + DefaultHttp2HeadersFrame respHeaders = (DefaultHttp2HeadersFrame) responseHeadersFrame; + assertEquals("404", respHeaders.headers().status().toString()); + } + + @Test + void testHttp2PostRequestWithJsonBody() throws Exception { + // 构造 POST 请求 + String json = OBJECT_MAPPER.writeValueAsString(new HashMap<String, Object>() {{ + put("foo", "bar"); + }}); + // 注意:param 应该放在 path 上,body 里用其它字段 + Http2Headers headers = new DefaultHttp2Headers(); + headers.method("POST"); + headers.path("/test?param=jsonValue"); + Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, false); + channel.writeInbound(headersFrame); + DefaultHttp2DataFrame dataFrame = new DefaultHttp2DataFrame(Unpooled.copiedBuffer(json, StandardCharsets.UTF_8), true); + channel.writeInbound(dataFrame); + + // 读取响应(兼容顺序问题,支持异步等待) + Http2StreamFrame frame1 = null, frame2 = null; + long deadline = System.currentTimeMillis() + 5000; // 最多等5秒 + while ((frame1 == null || frame2 == null) && System.currentTimeMillis() < deadline) { + if (frame1 == null) frame1 = channel.readOutbound(); + if (frame2 == null) frame2 = channel.readOutbound(); + if (frame1 == null || frame2 == null) Thread.sleep(500); + } + assertNotNull(frame1); + assertNotNull(frame2); + DefaultHttp2HeadersFrame respHeaders; + DefaultHttp2DataFrame respData; + if (frame1 instanceof DefaultHttp2HeadersFrame) { + respHeaders = (DefaultHttp2HeadersFrame) frame1; + respData = (DefaultHttp2DataFrame) frame2; + } else { + respHeaders = (DefaultHttp2HeadersFrame) frame2; + respData = (DefaultHttp2DataFrame) frame1; + } + assertEquals("200", respHeaders.headers().status().toString()); + String content = respData.content().toString(StandardCharsets.UTF_8); + assertTrue(content.contains("Processed: jsonValue")); + } + + @Test + void testHttp2BadRequest() { + // 不带 method/path + Http2Headers headers = new DefaultHttp2Headers(); + Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true); + channel.writeInbound(headersFrame); + Http2StreamFrame responseHeadersFrame = channel.readOutbound(); + assertTrue(responseHeadersFrame instanceof DefaultHttp2HeadersFrame); + DefaultHttp2HeadersFrame respHeaders = (DefaultHttp2HeadersFrame) responseHeadersFrame; + assertEquals("400", respHeaders.headers().status().toString()); + } + + @org.junit.jupiter.api.AfterEach + void tearDown() throws Exception { + // ��理ControllerManager Review Comment: The comment appears to contain garbled characters. Please ensure the proper encoding is used so that the intent of the comment is clear. ```suggestion // Clean up ControllerManager ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org