Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Commit ff8922c

Browse files
moozzykmoozzyk
authored andcommitted
WrongVersion - throwing if client and server protocol versions don't match
1 parent 146bd82 commit ff8922c

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/signalrclient/connection_impl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <thread>
66
#include <algorithm>
77
#include "cpprest\asyncrt_utils.h"
8+
#include "constants.h"
89
#include "connection_impl.h"
910
#include "request_sender.h"
1011
#include "url_builder.h"
@@ -100,6 +101,15 @@ namespace signalr
100101
}, m_disconnect_cts.get_token())
101102
.then([connection](negotiation_response negotiation_response)
102103
{
104+
if (negotiation_response.protocol_version != PROTOCOL)
105+
{
106+
return pplx::task_from_exception<void>(
107+
std::runtime_error(std::string{ "incompatible protocol version. client protocol version: " }
108+
.append(utility::conversions::to_utf8string(PROTOCOL)
109+
.append(", server protocol version: ")
110+
.append(utility::conversions::to_utf8string(negotiation_response.protocol_version)))));
111+
}
112+
103113
return connection->start_transport(negotiation_response)
104114
.then([connection, negotiation_response](std::shared_ptr<transport> transport)
105115
{

test/signalrclienttests/connection_impl_tests.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ TEST(connection_impl_start, start_fails_if_connect_request_times_out)
302302
auto websocket_client = std::make_shared<test_websocket_client>();
303303
websocket_client->set_receive_function([]()->pplx::task<std::string>
304304
{
305-
return pplx::task_from_result(std::string(""));
305+
return pplx::task_from_result(std::string("{}"));
306306
});
307307

308308
auto connection =
@@ -320,6 +320,36 @@ TEST(connection_impl_start, start_fails_if_connect_request_times_out)
320320
}
321321
}
322322

323+
TEST(connection_impl_start, start_fails_if_protocol_versions_not_compatible)
324+
{
325+
auto web_request_factory = std::make_unique<test_web_request_factory>([](const web::uri& url)
326+
{
327+
auto response_body =
328+
url.path() == _XPLATSTR("/negotiate")
329+
? _XPLATSTR("{\"Url\":\"/signalr\", \"ConnectionToken\" : \"A==\", \"ConnectionId\" : \"f7707523-307d-4cba-9abf-3eef701241e8\", ")
330+
_XPLATSTR("\"KeepAliveTimeout\" : 20.0, \"DisconnectTimeout\" : 30.0, \"ConnectionTimeout\" : 110.0, \"TryWebSockets\" : true, ")
331+
_XPLATSTR("\"ProtocolVersion\" : \"1.2\", \"TransportConnectTimeout\" : 0.1, \"LongPollDelay\" : 0.0}")
332+
: _XPLATSTR("{ }");
333+
334+
return std::unique_ptr<web_request>(new web_request_stub((unsigned short)200, _XPLATSTR("OK"), response_body));
335+
});
336+
337+
auto websocket_client = std::make_shared<test_websocket_client>();
338+
auto connection =
339+
connection_impl::create(create_uri(), _XPLATSTR(""), trace_level::all, std::make_shared<trace_log_writer>(),
340+
std::move(web_request_factory), std::make_unique<test_transport_factory>(websocket_client));
341+
342+
try
343+
{
344+
connection->start().get();
345+
ASSERT_TRUE(false); // exception not thrown
346+
}
347+
catch (const std::runtime_error &e)
348+
{
349+
ASSERT_STREQ("incompatible protocol version. client protocol version: 1.4, server protocol version: 1.2", e.what());
350+
}
351+
}
352+
323353
TEST(connection_impl_process_response, process_response_logs_messages)
324354
{
325355
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());

0 commit comments

Comments
 (0)