File: client_construction.cpp

package info (click to toggle)
cpprest 2.10.10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,812 kB
  • sloc: cpp: 69,785; sh: 254; makefile: 167
file content (255 lines) | stat: -rw-r--r-- 9,104 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * client_construction.cpp
 *
 * Tests cases for covering creating websocket_clients.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

#if defined(__cplusplus_winrt) || !defined(_M_ARM)

using namespace concurrency::streams;

using namespace web;
using namespace web::websockets;
using namespace web::websockets::client;

using namespace tests::functional::websocket::utilities;

namespace tests
{
namespace functional
{
namespace websocket
{
namespace client
{
SUITE(client_construction)
{
    // Helper function verifies that when constructing a websocket_client with invalid
    // URI std::invalid_argument is thrown.
    static void verify_client_invalid_argument(const uri& address)
    {
        try
        {
            websocket_client client;
            client.connect(address).wait();
            VERIFY_IS_TRUE(false);
        }
        catch (std::invalid_argument&)
        {
            // expected
        }
    }

    TEST_FIXTURE(uri_address, client_construction_error_cases)
    {
        uri address(U("notws://localhost:34567/"));

        // Invalid scheme.
        verify_client_invalid_argument(address);

        // empty host.
        address = uri(U("ws://:34567/"));
        verify_client_invalid_argument(address);
    }

    // Verify that we can read the config from the websocket_client
    TEST_FIXTURE(uri_address, get_client_config)
    {
        websocket_client_config config;

        web::credentials cred(U("username"), U("password"));
        config.set_credentials(cred);
        websocket_client client(config);

        const websocket_client_config& config2 = client.config();
        VERIFY_ARE_EQUAL(config2.credentials().username(), cred.username());
    }

    // Verify that we can read the config from the websocket_callback_client
    TEST_FIXTURE(uri_address, get_client_config_callback_client)
    {
        websocket_client_config config;

        web::credentials cred(U("username"), U("password"));
        config.set_credentials(cred);
        websocket_callback_client client(config);

        const websocket_client_config& config2 = client.config();
        VERIFY_ARE_EQUAL(config2.credentials().username(), cred.username());
    }

    // Verify that we can get the baseuri from websocket_client connect.
    TEST_FIXTURE(uri_address, uri_test)
    {
        websocket_client client1;
        VERIFY_ARE_EQUAL(client1.uri(), U("/"));

        test_websocket_server server;
        client1.connect(m_uri).wait();
        VERIFY_ARE_EQUAL(client1.uri(), m_uri);
        client1.close().wait();

        websocket_client_config config;
        websocket_client client2(config);
        VERIFY_ARE_EQUAL(client2.uri(), U("/"));

        client2.connect(m_uri).wait();
        VERIFY_ARE_EQUAL(client2.uri(), m_uri);
        client2.close().wait();
    }

    TEST_FIXTURE(uri_address, move_operations)
    {
        std::string body("hello");
        std::vector<unsigned char> body_vec(body.begin(), body.end());

        test_websocket_server server;
        websocket_client client;

        client.connect(m_uri).wait();

        // Move constructor
        websocket_client client2 = std::move(client);

        server.next_message([&](test_websocket_msg msg) // Handler to verify the message sent by the client.
                            {
                                websocket_asserts::assert_message_equals(
                                    msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
                            });

        websocket_outgoing_message msg;
        msg.set_utf8_message(body);
        client2.send(std::move(msg)).wait();

        auto t = client2.receive().then([&](websocket_incoming_message ret_msg) {
            VERIFY_ARE_EQUAL(ret_msg.length(), body.length());
            auto ret_str = ret_msg.extract_string().get();

            VERIFY_ARE_EQUAL(body.compare(ret_str), 0);
            VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
        });

        test_websocket_msg rmsg;
        rmsg.set_data(body_vec);
        rmsg.set_msg_type(test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        server.send_msg(rmsg);
        t.wait();

        // Move assignment
        client = std::move(client2);
        server.next_message([&](test_websocket_msg msg) // Handler to verify the message sent by the client.
                            {
                                websocket_asserts::assert_message_equals(
                                    msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
                            });

        websocket_outgoing_message msg1;
        msg1.set_utf8_message(body);
        client.send(std::move(msg1)).wait();

        test_websocket_msg rmsg1;
        rmsg1.set_data(body_vec);
        rmsg1.set_msg_type(test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        server.send_msg(rmsg1);
        auto t1 = client.receive().then([&](websocket_incoming_message ret_msg) {
            VERIFY_ARE_EQUAL(ret_msg.length(), body.length());
            auto ret_str = ret_msg.extract_string().get();

            VERIFY_ARE_EQUAL(body.compare(ret_str), 0);
            VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
        });
        t1.wait();
        client.close().wait();
    }

    void header_test_impl(const uri& address,
                          const utility::string_t& headerName,
                          const utility::string_t& headerValue,
                          const utility::string_t& expectedHeaderValue = U(""))
    {
        test_websocket_server server;
        websocket_client_config config;
        utility::string_t expectedValue = headerValue;
        if (!expectedHeaderValue.empty())
        {
            expectedValue = expectedHeaderValue;
        }
        config.headers().add(headerName, headerValue);
        websocket_client client(config);

        server.set_http_handler([&](test_http_request request) {
            test_http_response resp;
            if (request->get_header_val(utility::conversions::to_utf8string(headerName))
                    .compare(utility::conversions::to_utf8string(expectedValue)) == 0)
                resp.set_status_code(200); // Handshake request will be completed only if header match succeeds.
            else
                resp.set_status_code(400); // Else fail the handshake, websocket client connect will fail in this case.
            return resp;
        });
        client.connect(address).wait();
        client.close().wait();
    }

    TEST_FIXTURE(uri_address, connect_with_headers)
    {
        header_test_impl(m_uri, U("HeaderTest"), U("ConnectSuccessfully"));
    }

    TEST_FIXTURE(uri_address, manually_set_protocol_header)
    {
        utility::string_t headerName(U("Sec-WebSocket-Protocol"));
        header_test_impl(m_uri, headerName, U("myprotocol"));
        header_test_impl(m_uri, headerName, U("myprotocol2,"), U("myprotocol2"));
        header_test_impl(m_uri, headerName, U("myprotocol2,protocol3"), U("myprotocol2, protocol3"));
        header_test_impl(
            m_uri, headerName, U("myprotocol2, protocol3, protocol6,,"), U("myprotocol2, protocol3, protocol6"));
    }

    TEST_FIXTURE(uri_address, set_subprotocol)
    {
        test_websocket_server server;
        websocket_client_config config;

        utility::string_t expected1(U("pro1"));
        config.add_subprotocol(expected1);
        VERIFY_ARE_EQUAL(1, config.subprotocols().size());
        VERIFY_ARE_EQUAL(expected1, config.subprotocols()[0]);

        utility::string_t expected2(U("second"));
        config.add_subprotocol(expected2);
        VERIFY_ARE_EQUAL(2, config.subprotocols().size());
        VERIFY_ARE_EQUAL(expected1, config.subprotocols()[0]);
        VERIFY_ARE_EQUAL(expected2, config.subprotocols()[1]);

        websocket_client client(config);
        server.set_http_handler([&](test_http_request request) {
            test_http_response resp;
            if (request->get_header_val(utility::conversions::to_utf8string(U("Sec-WebSocket-Protocol")))
                    .compare(utility::conversions::to_utf8string(expected1 + U(", ") + expected2)) == 0)
                resp.set_status_code(200); // Handshake request will be completed only if header match succeeds.
            else
                resp.set_status_code(400); // Else fail the handshake, websocket client connect will fail in this case.
            return resp;
        });

        client.connect(m_uri).wait();
        client.close().wait();
    }

} // SUITE(client_construction)

} // namespace client
} // namespace websocket
} // namespace functional
} // namespace tests

#endif