Repository navigation
Expand file tree
/
Copy pathwebserver_ws_unavailable_test.cpp
More file actions
107 lines (95 loc) · 3.99 KB
/
Copy pathwebserver_ws_unavailable_test.cpp
File metadata and controls
107 lines (95 loc) · 3.99 KB
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
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
// On a HAVE_WEBSOCKET-off build,
// every public webserver WebSocket entry point must throw
// feature_unavailable whose what() names both "websocket" and
// "HAVE_WEBSOCKET".
//
// v2 replaced the v1 raw-pointer register_ws_resource overload
// with two smart-pointer overloads (unique_ptr, shared_ptr) and added
// unregister_ws_resource. All three are exercised here.
//
// On a HAVE_WEBSOCKET-on build there is no contract to pin from this TU
// (the integ-level ws_start_stop test already exercises the success path),
// so the suite is left empty there: the binary still exists, runs, exits
// 0, and contributes to the make-check totals in both configurations.
#include <memory>
#include <string>
#include "./httpserver.hpp"
#include "./littletest.hpp"
LT_BEGIN_SUITE(webserver_ws_unavailable_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(webserver_ws_unavailable_suite)
#ifndef HAVE_WEBSOCKET
LT_BEGIN_AUTO_TEST(webserver_ws_unavailable_suite,
register_ws_unique_throws_feature_unavailable)
httpserver::webserver ws{httpserver::create_webserver(8090)};
bool caught = false;
std::string msg;
try {
// The null unique_ptr is used for compile convenience only — the
// HAVE_WEBSOCKET-off branch throws feature_unavailable before
// reaching any null check. On a HAVE_WEBSOCKET-on build the same
// null argument would throw std::invalid_argument instead.
ws.register_ws_resource(
"/ws", std::unique_ptr<httpserver::websocket_handler>{});
} catch (const httpserver::feature_unavailable& e) {
caught = true;
msg = e.what();
}
LT_CHECK(caught);
LT_CHECK(msg.find("websocket") != std::string::npos);
LT_CHECK(msg.find("HAVE_WEBSOCKET") != std::string::npos);
LT_END_AUTO_TEST(register_ws_unique_throws_feature_unavailable)
LT_BEGIN_AUTO_TEST(webserver_ws_unavailable_suite,
register_ws_shared_throws_feature_unavailable)
httpserver::webserver ws{httpserver::create_webserver(8091)};
bool caught = false;
std::string msg;
try {
ws.register_ws_resource(
"/ws", std::shared_ptr<httpserver::websocket_handler>{});
} catch (const httpserver::feature_unavailable& e) {
caught = true;
msg = e.what();
}
LT_CHECK(caught);
LT_CHECK(msg.find("websocket") != std::string::npos);
LT_CHECK(msg.find("HAVE_WEBSOCKET") != std::string::npos);
LT_END_AUTO_TEST(register_ws_shared_throws_feature_unavailable)
LT_BEGIN_AUTO_TEST(webserver_ws_unavailable_suite,
unregister_ws_throws_feature_unavailable)
httpserver::webserver ws{httpserver::create_webserver(8092)};
bool caught = false;
std::string msg;
try {
ws.unregister_ws_resource("/ws");
} catch (const httpserver::feature_unavailable& e) {
caught = true;
msg = e.what();
}
LT_CHECK(caught);
LT_CHECK(msg.find("websocket") != std::string::npos);
LT_CHECK(msg.find("HAVE_WEBSOCKET") != std::string::npos);
LT_END_AUTO_TEST(unregister_ws_throws_feature_unavailable)
#endif // !HAVE_WEBSOCKET
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()