1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_ECHO_HANDLER_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_ECHO_HANDLER_H_ 7 8 #include <memory> 9 #include <string_view> 10 11 #include "base/memory/scoped_refptr.h" 12 #include "net/test/embedded_test_server/embedded_test_server.h" 13 #include "net/test/embedded_test_server/websocket_connection.h" 14 #include "net/test/embedded_test_server/websocket_handler.h" 15 16 namespace net::test_server { 17 18 // WebSocketEchoHandler is a handler for WebSocket connections that echoes 19 // back any received text or binary messages to the sender. 20 class WebSocketEchoHandler : public WebSocketHandler { 21 public: 22 // Constructs the handler with a given WebSocket connection. 23 explicit WebSocketEchoHandler(scoped_refptr<WebSocketConnection> connection); 24 25 // Called during the WebSocket handshake; adds an "X-Custom-Header" with the 26 // value "WebSocketEcho" to the response. 27 void OnHandshake(const HttpRequest& request) override; 28 29 // Echoes back any received text message. 30 void OnTextMessage(std::string_view message) override; 31 32 // Echoes back any received binary message. 33 void OnBinaryMessage(base::span<const uint8_t> message) override; 34 }; 35 36 } // namespace net::test_server 37 38 #endif // NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_ECHO_HANDLER_H_ 39