]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | /* |
2 | * Copyright 2021 ScyllaDB | |
3 | */ | |
4 | ||
5 | #include <seastar/websocket/server.hh> | |
6 | #include <seastar/testing/test_case.hh> | |
7 | #include <seastar/testing/thread_test_case.hh> | |
8 | #include <seastar/http/response_parser.hh> | |
9 | #include <seastar/util/defer.hh> | |
10 | #include "loopback_socket.hh" | |
11 | ||
12 | using namespace seastar; | |
13 | using namespace seastar::experimental; | |
14 | ||
15 | SEASTAR_TEST_CASE(test_websocket_handshake) { | |
16 | return seastar::async([] { | |
17 | const std::string request = | |
18 | "GET / HTTP/1.1\r\n" | |
19 | "Upgrade: websocket\r\n" | |
20 | "Connection: Upgrade\r\n" | |
21 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" | |
22 | "Sec-WebSocket-Version: 13\r\n" | |
23 | "\r\n"; | |
24 | loopback_connection_factory factory; | |
25 | loopback_socket_impl lsi(factory); | |
26 | ||
27 | auto acceptor = factory.get_server_socket().accept(); | |
28 | auto connector = lsi.connect(socket_address(), socket_address()); | |
29 | connected_socket sock = connector.get0(); | |
30 | auto input = sock.input(); | |
31 | auto output = sock.output(); | |
32 | ||
33 | websocket::server dummy; | |
34 | websocket::connection conn(dummy, acceptor.get0().connection); | |
35 | future<> serve = conn.process(); | |
36 | ||
37 | auto close = defer([&conn, &input, &output, &serve] () noexcept { | |
38 | conn.shutdown(); | |
39 | input.close().get(); | |
40 | output.close().get(); | |
41 | serve.get(); | |
42 | }); | |
43 | ||
44 | // Send the handshake | |
45 | output.write(request).get(); | |
46 | output.flush().get(); | |
47 | ||
48 | // Check that the server correctly computed the response | |
49 | // according to WebSocket handshake specification | |
50 | http_response_parser parser; | |
51 | parser.init(); | |
52 | input.consume(parser).get(); | |
53 | std::unique_ptr<http_response> resp = parser.get_parsed_response(); | |
54 | BOOST_ASSERT(resp); | |
55 | sstring websocket_accept = resp->_headers["Sec-WebSocket-Accept"]; | |
56 | // Trim possible whitespace prefix | |
57 | auto it = std::find_if(websocket_accept.begin(), websocket_accept.end(), ::isalnum); | |
58 | if (it != websocket_accept.end()) { | |
59 | websocket_accept.erase(websocket_accept.begin(), it); | |
60 | } | |
61 | BOOST_REQUIRE_EQUAL(websocket_accept, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="); | |
62 | for (auto& header : resp->_headers) { | |
63 | std::cout << header.first << ':' << header.second << std::endl; | |
64 | } | |
65 | }); | |
66 | } |