File: close_tests.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 (167 lines) | stat: -rw-r--r-- 5,283 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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * close_tests.cpp
 *
 * Tests cases for closing websocket_client objects.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

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

using namespace concurrency::streams;

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

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

namespace tests
{
namespace functional
{
namespace websocket
{
namespace client
{
SUITE(close_tests)
{
    // Test close websocket connection: client sends an empty close and server responds with close frame
    TEST_FIXTURE(uri_address, close_client_websocket)
    {
        test_websocket_server server;

        websocket_client client;

        client.connect(m_uri).wait();

        client.close().wait();
    }

    // Test close websocket connection: client sends a close with reason and server responds with close frame
    TEST_FIXTURE(uri_address, close_with_reason)
    {
        test_websocket_server server;

        websocket_client client;

        client.connect(m_uri).wait();

        client.close(websocket_close_status::going_away, U("Client disconnecting")).wait();
    }

    // Server sends a close frame (server initiated close)
    TEST_FIXTURE(uri_address, close_from_server)
    {
        std::string body("hello");
        test_websocket_server server;

        websocket_client client;

        client.connect(m_uri).wait();

        // Send close frame from server
        test_websocket_msg msg;
        msg.set_msg_type(test_websocket_message_type::WEB_SOCKET_CLOSE_TYPE);
        server.send_msg(msg);

        client.close().wait();
    }

    // Test close websocket connection with callback client: client sends an empty close and server responds with close
    // frame
    TEST_FIXTURE(uri_address, close_callback_client_websocket, "Ignore", "319")
    {
        test_websocket_server server;
        const utility::string_t close_reason = U("Too large");

        // verify it is ok not to set close handler
        websocket_callback_client client;

        client.connect(m_uri).wait();

        client.close().wait();

        websocket_callback_client client1;

        client1.set_close_handler([&close_reason](websocket_close_status status,
                                                  const utility::string_t& reason,
                                                  const std::error_code& code) {
            VERIFY_ARE_EQUAL(status, websocket_close_status::too_large);
            VERIFY_ARE_EQUAL(reason, close_reason);
            VERIFY_ARE_EQUAL(code.value(), 0);
        });

        client1.connect(m_uri).wait();

        client1.close(websocket_close_status::too_large, close_reason).wait();
    }

    // Test close websocket connection: client sends a close with reason and server responds with close frame
    TEST_FIXTURE(uri_address, close_callback_client_with_reason, "Ignore", "319")
    {
        const utility::string_t close_reason = U("Client disconnecting");
        test_websocket_server server;

        websocket_callback_client client;

        client.set_close_handler([close_reason](websocket_close_status status,
                                                const utility::string_t& reason,
                                                const std::error_code& code) {
            VERIFY_ARE_EQUAL(status, websocket_close_status::normal);
            VERIFY_ARE_EQUAL(reason, close_reason);
            VERIFY_ARE_EQUAL(code.value(), 0);
        });

        client.connect(m_uri).wait();

        client.close(websocket_close_status::normal, close_reason).wait();
    }

    // Server sends a close frame (server initiated close)
    TEST_FIXTURE(uri_address, close_callback_client_from_server, "Ignore", "319")
    {
        std::string body("hello");
        test_websocket_server server;

        websocket_callback_client client;

        int hitCount = 0;
        pplx::task_completion_event<void> closeEvent;
        client.set_close_handler([&hitCount, closeEvent](websocket_close_status status,
                                                         const utility::string_t& reason,
                                                         const std::error_code& code) {
            VERIFY_ARE_EQUAL(status, websocket_close_status::going_away);
            VERIFY_ARE_EQUAL(reason, U(""));
            VERIFY_ARE_EQUAL(code.value(), 0);

            hitCount++;
            closeEvent.set();
        });

        client.connect(m_uri).wait();

        // Send close frame from server
        test_websocket_msg msg;
        msg.set_msg_type(test_websocket_message_type::WEB_SOCKET_CLOSE_TYPE);
        server.send_msg(msg);

        // make sure it only called once.
        pplx::create_task(closeEvent).wait();
        VERIFY_ARE_EQUAL(hitCount, 1);
    }

} // SUITE(close_tests)

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

#endif