File: error_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 (172 lines) | stat: -rw-r--r-- 5,045 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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Tests cases error connection cases with websocket_client.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#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(error_tests)
{
    // Send before connecting
    TEST_FIXTURE(uri_address, send_before_connect)
    {
        websocket_client client;

        websocket_outgoing_message msg;
        msg.set_utf8_message("xyz");

        VERIFY_THROWS(client.send(msg).wait(), websocket_exception);
    }

    // Server does not exist
    TEST_FIXTURE(uri_address, server_doesnt_exist)
    {
        websocket_client client;
        VERIFY_THROWS(client.connect(m_uri).get(), websocket_exception);
    }

// Send after close
// CodePlex 319 fails on VS2013.
#if !defined(_MSC_VER) || _MSC_VER >= 1900
    TEST_FIXTURE(uri_address, send_after_close)
    {
        std::string body("hello");
        test_websocket_server server;

        server.next_message([&](test_websocket_msg msg) {
            websocket_asserts::assert_message_equals(
                msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        });
        websocket_client client;

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

        websocket_outgoing_message msg;
        msg.set_utf8_message(body);
        VERIFY_THROWS(client.send(msg).wait(), websocket_exception);
    }
#endif

    // Send after close for callback client
    TEST_FIXTURE(uri_address, send_after_close_callback_client, "Ignore", "319")
    {
        std::string body("hello");
        test_websocket_server server;

        server.next_message([&](test_websocket_msg msg) {
            websocket_asserts::assert_message_equals(
                msg, body, test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE);
        });
        websocket_callback_client client;

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

        websocket_outgoing_message msg;
        msg.set_utf8_message(body);
        VERIFY_THROWS(client.send(msg).wait(), websocket_exception);
    }

    // Receive after close
    TEST_FIXTURE(uri_address, receive_after_close)
    {
        test_websocket_server server;
        websocket_client client;
        client.connect(m_uri).wait();
        auto t = client.receive();
        client.close().wait();
        VERIFY_THROWS(t.wait(), websocket_exception);
    }

    // Start receive task after client has closed
    TEST_FIXTURE(uri_address, try_receive_after_close)
    {
        test_websocket_server server;
        websocket_client client;
        client.connect(m_uri).wait();
        client.close().wait();
        auto t = client.receive();
        VERIFY_THROWS(t.wait(), websocket_exception);
    }

    // Start the receive task after server has sent a close frame
    TEST_FIXTURE(uri_address, try_receive_after_server_initiated_close)
    {
        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);

        // 100 ms should be plenty for local loopback
        std::chrono::milliseconds dura(100);
        std::this_thread::sleep_for(dura);

        auto t = client.receive();
        VERIFY_THROWS(t.wait(), websocket_exception);

        client.close().wait();
    }

    // Destroy the client without closing it explicitly
    TEST_FIXTURE(uri_address, destroy_without_close)
    {
        test_websocket_server server;
        websocket_client client;
        client.connect(m_uri).wait();
    }

    // Destroy the callback client without closing it explicitly
    TEST_FIXTURE(uri_address, destroy_without_close_callback_client)
    {
        // test won't finish if we can't release client properly
        test_websocket_server server;
        websocket_callback_client client;
        client.connect(m_uri).wait();
    }

    // connect fails while user is waiting on receive
    TEST_FIXTURE(uri_address, connect_fail_with_receive)
    {
        websocket_client client;
        auto t = client.receive();

        VERIFY_THROWS(client.connect(U("ws://localhost:9981/ws")).get(), websocket_exception);
        VERIFY_THROWS(t.get(), websocket_exception);
    }

} // SUITE(error_tests)

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

#endif