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

Commit

Permalink
SignalR - appending "/SignalR" to the hub_connection url by default
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzyk committed Feb 13, 2015
1 parent 8071bf6 commit 30e942c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/signalrclient/hub_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace signalr
{
public:
SIGNALRCLIENT_API explicit hub_connection(const utility::string_t& url, const utility::string_t& query_string = U(""),
trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr);
trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr, bool use_default_url = true);

SIGNALRCLIENT_API ~hub_connection();

Expand Down
4 changes: 2 additions & 2 deletions src/signalrclient/hub_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace signalr
{
hub_connection::hub_connection(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, std::shared_ptr<log_writer> log_writer)
: m_pImpl(hub_connection_impl::create(url, query_string, trace_level, std::move(log_writer)))
trace_level trace_level, std::shared_ptr<log_writer> log_writer, bool use_default_url)
: m_pImpl(hub_connection_impl::create(url, query_string, trace_level, std::move(log_writer), use_default_url))
{}

// Do NOT remove this destructor. Letting the compiler generate and inline the default dtor may lead to
Expand Down
37 changes: 29 additions & 8 deletions src/signalrclient/hub_connection_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,36 @@ namespace signalr
const std::function<void(const json::value&)>& set_result,
const std::function<void(const std::exception_ptr e)>& set_exception,
const std::function<void(const json::value&)>& on_progress);

static utility::string_t adapt_url(const utility::string_t& url, bool use_default_url);
}

std::shared_ptr<hub_connection_impl> hub_connection_impl::create(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer)
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, bool use_default_url)
{
return hub_connection_impl::create(url, query_string, trace_level, log_writer,
return hub_connection_impl::create(url, query_string, trace_level, log_writer, use_default_url,
std::make_unique<web_request_factory>(), std::make_unique<transport_factory>());
}

std::shared_ptr<hub_connection_impl> hub_connection_impl::create(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, std::unique_ptr<web_request_factory> web_request_factory,
std::unique_ptr<transport_factory> transport_factory)
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, bool use_default_url,
std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory)
{
auto connection = std::shared_ptr<hub_connection_impl>(new hub_connection_impl(url, query_string, trace_level,
log_writer ? log_writer : std::make_shared<trace_log_writer>(), std::move(web_request_factory), std::move(transport_factory)));
log_writer ? log_writer : std::make_shared<trace_log_writer>(), use_default_url,
std::move(web_request_factory), std::move(transport_factory)));

connection->initialize();

return connection;
}

hub_connection_impl::hub_connection_impl(const utility::string_t& url, const utility::string_t& query_string, trace_level trace_level,
const std::shared_ptr<log_writer>& log_writer, std::unique_ptr<web_request_factory> web_request_factory,
const std::shared_ptr<log_writer>& log_writer, bool use_default_url, std::unique_ptr<web_request_factory> web_request_factory,
std::unique_ptr<transport_factory> transport_factory)
: m_connection(connection_impl::create(url, query_string, trace_level, log_writer, std::move(web_request_factory), std::move(transport_factory))),
m_logger(log_writer, trace_level), m_callback_manager(json::value::parse(_XPLATSTR("{ \"E\" : \"connection went out of scope before invocation result was received\"}")))
: m_connection(connection_impl::create(adapt_url(url, use_default_url), query_string, trace_level, log_writer,
std::move(web_request_factory), std::move(transport_factory))),m_logger(log_writer, trace_level),
m_callback_manager(json::value::parse(_XPLATSTR("{ \"E\" : \"connection went out of scope before invocation result was received\"}")))
{ }

void hub_connection_impl::initialize()
Expand Down Expand Up @@ -300,5 +304,22 @@ namespace signalr
set_result(json::value::null());
};
}

static utility::string_t adapt_url(const utility::string_t& url, bool use_default_url)
{
if (use_default_url)
{
auto new_url = url;
if (new_url.back() != _XPLATSTR('/'))
{
new_url.append(_XPLATSTR("/"));
}
new_url.append(_XPLATSTR("signalr"));

return new_url;
}

return url;
}
}
}
10 changes: 5 additions & 5 deletions src/signalrclient/hub_connection_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace signalr
{
public:
static std::shared_ptr<hub_connection_impl> create(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer);
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, bool use_default_url);

static std::shared_ptr<hub_connection_impl> create(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, std::unique_ptr<web_request_factory> web_request_factory,
std::unique_ptr<transport_factory> transport_factory);
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, bool use_default_url,
std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory);

hub_connection_impl(const hub_connection_impl&) = delete;
hub_connection_impl& operator=(const hub_connection_impl&) = delete;
Expand All @@ -44,8 +44,8 @@ namespace signalr

private:
hub_connection_impl(const utility::string_t& url, const utility::string_t& query_string, trace_level trace_level,
const std::shared_ptr<log_writer>& log_writer, std::unique_ptr<web_request_factory> web_request_factory,
std::unique_ptr<transport_factory> transport_factory);
const std::shared_ptr<log_writer>& log_writer, bool use_default_url,
std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory);

//TODO: keep a copy or take from the connection?
logger m_logger;
Expand Down
63 changes: 59 additions & 4 deletions test/signalrclienttests/hub_connection_impl_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,64 @@ using namespace signalr;
std::shared_ptr<hub_connection_impl> create_hub_connection(std::shared_ptr<websocket_client> websocket_client = create_test_websocket_client(),
std::shared_ptr<log_writer> log_writer = std::make_shared<trace_log_writer>(), trace_level trace_level = trace_level::all)
{
return hub_connection_impl::create( _XPLATSTR("http://fakeuri"), _XPLATSTR(""), trace_level, log_writer,
return hub_connection_impl::create( _XPLATSTR("http://fakeuri"), _XPLATSTR(""), trace_level, log_writer, /*use_default_url*/true,
create_test_web_request_factory(), std::make_unique<test_transport_factory>(websocket_client));
}

TEST(url, signalr_appended_to_url_if_use_default_url_true)
{
utility::string_t base_urls[] = { _XPLATSTR("http://fakeuri"), _XPLATSTR("http://fakeuri/") };

for (const auto& base_url : base_urls)
{
web::uri requested_url;
auto web_request_factory = std::make_unique<test_web_request_factory>([&requested_url](const web::uri &url)
{
requested_url = url;
return std::unique_ptr<web_request>(new web_request_stub((unsigned short)404, _XPLATSTR("Bad request"), _XPLATSTR("")));
});

auto hub_connection = hub_connection_impl::create(base_url, _XPLATSTR(""), trace_level::none,
std::make_shared<trace_log_writer>(), /*use_default_url:*/ true, std::move(web_request_factory),
std::make_unique<test_transport_factory>(create_test_websocket_client()));

try
{
hub_connection->start().get();
}
catch (const std::exception&) { }

ASSERT_EQ(web::uri(_XPLATSTR("http://fakeuri/signalr/negotiate?clientProtocol=1.4")), requested_url);
}
}

TEST(url, signalr_not_appended_to_url_if_use_default_url_false)
{
utility::string_t base_urls[] = { _XPLATSTR("http://fakeuri"), _XPLATSTR("http://fakeuri/") };

for (const auto& base_url : base_urls)
{
web::uri requested_url;
auto web_request_factory = std::make_unique<test_web_request_factory>([&requested_url](const web::uri &url)
{
requested_url = url;
return std::unique_ptr<web_request>(new web_request_stub((unsigned short)404, _XPLATSTR("Bad request"), _XPLATSTR("")));
});

auto hub_connection = hub_connection_impl::create(base_url, _XPLATSTR(""), trace_level::none,
std::make_shared<trace_log_writer>(), /*use_default_url:*/ false, std::move(web_request_factory),
std::make_unique<test_transport_factory>(create_test_websocket_client()));

try
{
hub_connection->start().get();
}
catch (const std::exception&) {}

ASSERT_EQ(web::uri(_XPLATSTR("http://fakeuri/negotiate?clientProtocol=1.4")), requested_url);
}
}

TEST(create_hub_proxy, create_hub_proxy_creates_proxy_with_correct_name)
{
auto hub_proxy = create_hub_connection()->create_hub_proxy(_XPLATSTR("my_hub_proxy"));
Expand Down Expand Up @@ -89,8 +143,9 @@ TEST(start, start_sets_connection_data)
return std::unique_ptr<web_request>(new web_request_stub((unsigned short)404, _XPLATSTR("Bad request"), _XPLATSTR("")));
});

auto hub_connection = hub_connection_impl::create(_XPLATSTR("http://fakeuri"), _XPLATSTR(""), trace_level::none, std::make_shared<trace_log_writer>(),
std::move(web_request_factory), std::make_unique<test_transport_factory>(create_test_websocket_client()));
auto hub_connection = hub_connection_impl::create(_XPLATSTR("http://fakeuri"), _XPLATSTR(""), trace_level::none,
std::make_shared<trace_log_writer>(), /*use_default_url:*/ true, std::move(web_request_factory),
std::make_unique<test_transport_factory>(create_test_websocket_client()));
hub_connection->create_hub_proxy(_XPLATSTR("my_hub"));
hub_connection->create_hub_proxy(_XPLATSTR("your_hub"));

Expand All @@ -102,7 +157,7 @@ TEST(start, start_sets_connection_data)
{
}

ASSERT_EQ(web::uri(_XPLATSTR("http://fakeuri/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22Name%22:%22my_hub%22%7D,%7B%22Name%22:%22your_hub%22%7D%5D")),
ASSERT_EQ(web::uri(_XPLATSTR("http://fakeuri/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22Name%22:%22my_hub%22%7D,%7B%22Name%22:%22your_hub%22%7D%5D")),
requested_url);
}

Expand Down
8 changes: 4 additions & 4 deletions test/signalrclienttests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ std::unique_ptr<web_request_factory> create_test_web_request_factory()
return std::make_unique<test_web_request_factory>([](const web::uri& url)
{
auto response_body =
url.path() == _XPLATSTR("/negotiate")
url.path() == _XPLATSTR("/negotiate") || url.path() == _XPLATSTR("/signalr/negotiate")
? _XPLATSTR("{\"Url\":\"/signalr\", \"ConnectionToken\" : \"A==\", \"ConnectionId\" : \"f7707523-307d-4cba-9abf-3eef701241e8\", ")
_XPLATSTR("\"KeepAliveTimeout\" : 20.0, \"DisconnectTimeout\" : 30.0, \"ConnectionTimeout\" : 110.0, \"TryWebSockets\" : true, ")
_XPLATSTR("\"ProtocolVersion\" : \"1.4\", \"TransportConnectTimeout\" : 5.0, \"LongPollDelay\" : 0.0}")
: url.path() == _XPLATSTR("/start")
? _XPLATSTR("{\"Response\":\"started\" }")
: _XPLATSTR("");
: url.path() == _XPLATSTR("/start") || url.path() == _XPLATSTR("/signalr/start")
? _XPLATSTR("{\"Response\":\"started\" }")
: _XPLATSTR("");

return std::unique_ptr<web_request>(new web_request_stub((unsigned short)200, _XPLATSTR("OK"), response_body));
});
Expand Down

0 comments on commit 30e942c

Please sign in to comment.