Copilot commented on code in PR #1382: URL: https://github.com/apache/pekko-http/pull/1382#discussion_r4455334066
########## http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/WebSocketHandlerTypesTest.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.pekko.http.javadsl.server.directives; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; +import org.apache.pekko.NotUsed; +import org.apache.pekko.http.javadsl.Http; +import org.apache.pekko.http.javadsl.ServerBinding; +import org.apache.pekko.http.javadsl.model.StatusCodes; +import org.apache.pekko.http.javadsl.model.Uri; +import org.apache.pekko.http.javadsl.model.ws.Message; +import org.apache.pekko.http.javadsl.model.ws.TextMessage; +import org.apache.pekko.http.javadsl.model.ws.WebSocket; +import org.apache.pekko.http.javadsl.model.ws.WebSocketRequest; +import org.apache.pekko.http.javadsl.server.Route; +import org.apache.pekko.http.javadsl.testkit.JUnitJupiterRouteTest; +import org.apache.pekko.http.javadsl.testkit.WSProbe; +import org.apache.pekko.japi.Pair; +import org.apache.pekko.stream.javadsl.Flow; +import org.apache.pekko.stream.javadsl.Keep; +import org.apache.pekko.stream.javadsl.Sink; +import org.apache.pekko.stream.javadsl.Source; +import org.junit.jupiter.api.Test; + +/** + * The Java WebSocket APIs accept handlers whose element types are subtypes or supertypes of {@link + * Message}, e.g. a {@code Flow<Message, TextMessage, NotUsed>}. + */ +public class WebSocketHandlerTypesTest extends JUnitJupiterRouteTest { + + // what Flow.of(Message.class).map(...) infers when the function returns a TextMessage + private static final Flow<Message, TextMessage, NotUsed> upperCase = + Flow.of(Message.class) + .map(msg -> TextMessage.create(msg.asTextMessage().getStrictText().toUpperCase())); Review Comment: The real TCP server can receive a streamed `TextMessage` when the request payload spans input chunks, even for the short `"hi"` message. Calling `getStrictText()` then fails the handler and makes `singleWebSocketRequestAcceptsMessageSubtype` depend on TCP chunk boundaries. Make the handler an instance field and use `mapAsync` with `toStrict` before uppercasing. ########## http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/WebSocketHandlerTypesTest.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.pekko.http.javadsl.server.directives; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; +import org.apache.pekko.NotUsed; +import org.apache.pekko.http.javadsl.Http; +import org.apache.pekko.http.javadsl.ServerBinding; +import org.apache.pekko.http.javadsl.model.StatusCodes; +import org.apache.pekko.http.javadsl.model.Uri; +import org.apache.pekko.http.javadsl.model.ws.Message; +import org.apache.pekko.http.javadsl.model.ws.TextMessage; +import org.apache.pekko.http.javadsl.model.ws.WebSocket; +import org.apache.pekko.http.javadsl.model.ws.WebSocketRequest; +import org.apache.pekko.http.javadsl.server.Route; +import org.apache.pekko.http.javadsl.testkit.JUnitJupiterRouteTest; +import org.apache.pekko.http.javadsl.testkit.WSProbe; +import org.apache.pekko.japi.Pair; +import org.apache.pekko.stream.javadsl.Flow; +import org.apache.pekko.stream.javadsl.Keep; +import org.apache.pekko.stream.javadsl.Sink; +import org.apache.pekko.stream.javadsl.Source; +import org.junit.jupiter.api.Test; + +/** + * The Java WebSocket APIs accept handlers whose element types are subtypes or supertypes of {@link + * Message}, e.g. a {@code Flow<Message, TextMessage, NotUsed>}. + */ +public class WebSocketHandlerTypesTest extends JUnitJupiterRouteTest { + + // what Flow.of(Message.class).map(...) infers when the function returns a TextMessage + private static final Flow<Message, TextMessage, NotUsed> upperCase = + Flow.of(Message.class) + .map(msg -> TextMessage.create(msg.asTextMessage().getStrictText().toUpperCase())); Review Comment: The no-argument `toUpperCase()` uses the JVM's default locale, but the echo tests always expect `"HI"`. With a Turkish default locale (`-Duser.language=tr -Duser.country=TR`), `"hi"` becomes `"Hİ"`, causing three of the new tests to fail even when the WebSocket adaptation is correct. Use `Locale.ROOT` to make the handler's output locale-independent. ########## http-tests/src/test/java/org/apache/pekko/http/javadsl/server/directives/WebSocketHandlerTypesTest.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.pekko.http.javadsl.server.directives; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; +import org.apache.pekko.NotUsed; +import org.apache.pekko.http.javadsl.Http; +import org.apache.pekko.http.javadsl.ServerBinding; +import org.apache.pekko.http.javadsl.model.StatusCodes; +import org.apache.pekko.http.javadsl.model.Uri; +import org.apache.pekko.http.javadsl.model.ws.Message; +import org.apache.pekko.http.javadsl.model.ws.TextMessage; +import org.apache.pekko.http.javadsl.model.ws.WebSocket; +import org.apache.pekko.http.javadsl.model.ws.WebSocketRequest; +import org.apache.pekko.http.javadsl.server.Route; +import org.apache.pekko.http.javadsl.testkit.JUnitJupiterRouteTest; +import org.apache.pekko.http.javadsl.testkit.WSProbe; +import org.apache.pekko.japi.Pair; +import org.apache.pekko.stream.javadsl.Flow; +import org.apache.pekko.stream.javadsl.Keep; +import org.apache.pekko.stream.javadsl.Sink; +import org.apache.pekko.stream.javadsl.Source; +import org.junit.jupiter.api.Test; + +/** + * The Java WebSocket APIs accept handlers whose element types are subtypes or supertypes of {@link + * Message}, e.g. a {@code Flow<Message, TextMessage, NotUsed>}. + */ +public class WebSocketHandlerTypesTest extends JUnitJupiterRouteTest { + + // what Flow.of(Message.class).map(...) infers when the function returns a TextMessage + private static final Flow<Message, TextMessage, NotUsed> upperCase = + Flow.of(Message.class) + .map(msg -> TextMessage.create(msg.asTextMessage().getStrictText().toUpperCase())); + + private static final Source<TextMessage, NotUsed> greeting = + Source.single(TextMessage.create("hello")) + .concat(Source.<TextMessage>maybe().mapMaterializedValue(f -> NotUsed.getInstance())); + + private final Route route = + concat( + path("directive", () -> handleWebSocketMessages(upperCase)), + path("compress", () -> handleWebSocketMessages(upperCase, msg -> true)), + path("protocol", () -> handleWebSocketMessagesForProtocol(upperCase, "echo")), + path( + "protocol-compress", + () -> handleWebSocketMessagesForProtocol(upperCase, "echo", msg -> false)), + path( + "optional-protocol", + () -> handleWebSocketMessagesForOptionalProtocol(upperCase, Optional.of("echo"))), + path( + "optional-protocol-compress", + () -> + handleWebSocketMessagesForOptionalProtocol( + upperCase, Optional.empty(), msg -> true)), + path( + "upgrade-flow", + () -> + extractWebSocketUpgrade( + upgrade -> complete(upgrade.handleMessagesWith(upperCase)))), + path( + "upgrade-sink-source", + () -> + extractWebSocketUpgrade( + // Sink.ignore() is a Sink<Object, ...>: a supertype of Message on the input + // side + upgrade -> complete(upgrade.handleMessagesWith(Sink.ignore(), greeting)))), + path( + "core", + () -> + extractRequest( + request -> + complete(WebSocket.handleWebSocketRequestWith(request, upperCase))))); + + @Test + public void serverSideHandlersAcceptMessageSubtypes() { + String[] paths = { + "/directive", + "/compress", + "/protocol", + "/protocol-compress", + "/optional-protocol", + "/optional-protocol-compress", + "/upgrade-flow", + "/core" + }; + for (String path : paths) { + WSProbe probe = WSProbe.create(system(), materializer()); + testRoute(route) + .run(WS(Uri.create(path), probe.flow(), materializer(), List.of("echo"))) + .assertStatusCode(StatusCodes.SWITCHING_PROTOCOLS); + + probe.sendMessage("hi"); + probe.expectMessage("HI"); + probe.sendCompletion(); + probe.expectCompletion(); + } + } + + @Test + public void upgradeSinkAndSourceAcceptMessageSubtypeAndSupertype() { + WSProbe probe = WSProbe.create(system(), materializer()); + testRoute(route) + .run(WS(Uri.create("/upgrade-sink-source"), probe.flow(), materializer())) + .assertStatusCode(StatusCodes.SWITCHING_PROTOCOLS); + + probe.expectMessage("hello"); + } + + @Test + public void testkitClientHandlerAcceptsMessageSubtype() throws Exception { + CompletableFuture<Message> reply = new CompletableFuture<>(); + Flow<Message, TextMessage, NotUsed> client = + Flow.fromSinkAndSource(Sink.<Message>foreach(reply::complete), clientSource("hi")); + + testRoute(route) + .run(WS(Uri.create("/directive"), client, materializer())) + .assertStatusCode(StatusCodes.SWITCHING_PROTOCOLS); + + assertEquals("HI", reply.get(3, TimeUnit.SECONDS).asTextMessage().getStrictText()); + } + + @Test + public void singleWebSocketRequestAcceptsMessageSubtype() throws Exception { + Http http = Http.get(system()); + ServerBinding binding = + http.newServerAt("127.0.0.1", 0).bind(route).toCompletableFuture().get(3, TimeUnit.SECONDS); + try { + Flow<Message, TextMessage, CompletionStage<Message>> client = + Flow.fromSinkAndSourceMat(Sink.<Message>head(), clientSource("hi"), Keep.left()); Review Comment: The real WebSocket reply can be a streamed `TextMessage` when its payload spans TCP reads, so the later `getStrictText()` assertion can throw even when the reply is correct. `Sink.head()` also cancels before that streamed payload is consumed. Use `mapAsync` with `toStrict` before `Sink.head()` so the materialized result contains the complete reply. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
