Connect - enabling building connect request url

This commit is contained in:
moozzyk 2014-10-16 14:49:25 -07:00
Родитель 54495c9136
Коммит 05101d3f68
3 изменённых файлов: 130 добавлений и 14 удалений

Просмотреть файл

@ -4,22 +4,79 @@
#include "stdafx.h"
#include "constants.h"
#include <cpprest\http_client.h>
#include "signalrclient\transport_type.h"
namespace signalr
{
namespace url_builder
{
web::uri_builder build_uri(const web::uri &base_url, const utility::string_t &command, const utility::string_t &query_string)
utility::string_t get_transport_name(transport_type transport)
{
return web::uri_builder(base_url)
.append_path(command)
.append_query(_XPLATSTR("clientProtocol"), PROTOCOL)
.append_query(query_string);
assert(transport == transport_type::websockets || transport == transport_type::long_polling);
return transport == transport_type::websockets
? _XPLATSTR("webSockets")
: _XPLATSTR("longPolling");
}
void append_transport(web::uri_builder &builder, transport_type transport)
{
if (transport > (transport_type)-1)
{
builder.append_query(_XPLATSTR("transport"), get_transport_name(transport));
}
}
void append_connection_token(web::uri_builder &builder, const utility::string_t &connection_token)
{
if (connection_token != _XPLATSTR(""))
{
builder.append_query(_XPLATSTR("connectionToken"), connection_token, /* do_encoding */ true);
}
}
web::uri_builder &convert_to_websocket_url(web::uri_builder &builder, transport_type transport)
{
if (transport == transport_type::websockets)
{
if (builder.scheme() == _XPLATSTR("https"))
{
builder.set_scheme(utility::string_t(_XPLATSTR("wss")));
}
else
{
builder.set_scheme(utility::string_t(_XPLATSTR("ws")));
}
}
return builder;
}
web::uri_builder build_uri(const web::uri &base_url, const utility::string_t &command, transport_type transport,
const utility::string_t &connection_token, const utility::string_t &query_string)
{
web::uri_builder builder(base_url);
builder.append_path(command);
append_transport(builder, transport);
builder.append_query(_XPLATSTR("clientProtocol"), PROTOCOL);
append_connection_token(builder, connection_token);
return builder.append_query(query_string);
}
web::uri build_negotiate(const web::uri &base_url, const utility::string_t &query_string)
{
return build_uri(base_url, _XPLATSTR("negotiate"), query_string).to_uri();
auto test = build_uri(base_url, _XPLATSTR("negotiate"), (transport_type)-1,
/*connection_token*/ _XPLATSTR(""), query_string).to_uri();
return build_uri(base_url, _XPLATSTR("negotiate"), (transport_type)-1,
/*connection_token*/ _XPLATSTR(""), query_string).to_uri();
}
web::uri build_connect(const web::uri &base_url, transport_type transport,
const utility::string_t &connection_token, const utility::string_t &query_string)
{
auto builder = build_uri(base_url, _XPLATSTR("connect"), transport, connection_token, query_string);
return convert_to_websocket_url(builder, transport).to_uri();
}
}
}

Просмотреть файл

@ -4,12 +4,14 @@
#pragma once
#include <cpprest\http_client.h>
#include "signalrclient\transport_type.h"
namespace signalr
{
namespace url_builder
{
web::uri_builder build_uri(const web::uri &base_url, const utility::string_t &command, const utility::string_t &query_string);
web::uri build_negotiate(const web::uri &base_url, const utility::string_t &query_string);
web::uri build_connect(const web::uri &base_url, transport_type transport,
const utility::string_t &connection_token, const utility::string_t &query_string);
}
}

Просмотреть файл

@ -9,17 +9,74 @@ using namespace signalr;
TEST(url_builder_negotiate, url_correct_if_query_string_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/sigalr/negotiate?clientProtocol=1.4")),
url_builder::build_negotiate(web::uri{_XPLATSTR("http://fake/sigalr/")}, _XPLATSTR("")));
web::uri(_XPLATSTR("http://fake/signalr/negotiate?clientProtocol=1.4")),
url_builder::build_negotiate(web::uri{_XPLATSTR("http://fake/signalr/")}, _XPLATSTR("")));
}
TEST(url_builder, url_correct_if_query_string_not_empty)
TEST(url_builder_negotiate, url_correct_if_query_string_not_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/sigalr/negotiate?clientProtocol=1.4&q1=1&q2=2")),
url_builder::build_negotiate(web::uri{ _XPLATSTR("http://fake/sigalr/") }, _XPLATSTR("q1=1&q2=2")));
web::uri(_XPLATSTR("http://fake/signalr/negotiate?clientProtocol=1.4&q1=1&q2=2")),
url_builder::build_negotiate(web::uri{ _XPLATSTR("http://fake/signalr/") }, _XPLATSTR("q1=1&q2=2")));
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/sigalr/negotiate?clientProtocol=1.4&q1=1&q2=2")),
url_builder::build_negotiate(web::uri{ _XPLATSTR("http://fake/sigalr/") }, _XPLATSTR("&q1=1&q2=2")));
web::uri(_XPLATSTR("http://fake/signalr/negotiate?clientProtocol=1.4&q1=1&q2=2")),
url_builder::build_negotiate(web::uri{ _XPLATSTR("http://fake/signalr/") }, _XPLATSTR("&q1=1&q2=2")));
}
TEST(url_builder_connect_longPolling, url_correct_if_query_string_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/signalr/connect?transport=longPolling&clientProtocol=1.4&connectionToken=connection%20token")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::long_polling, _XPLATSTR("connection token"), _XPLATSTR("")));
}
TEST(url_builder_connect_longPolling, url_correct_if_query_string_not_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/signalr/connect?transport=longPolling&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::long_polling, _XPLATSTR("connection-token"), _XPLATSTR("q1=1&q2=2")));
ASSERT_EQ(
web::uri(_XPLATSTR("http://fake/signalr/connect?transport=longPolling&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::long_polling, _XPLATSTR("connection-token"), _XPLATSTR("&q1=1&q2=2")));
}
TEST(url_builder_connect_webSockets, url_correct_if_query_string_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("ws://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection%20token")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection token"), _XPLATSTR("")));
ASSERT_EQ(
web::uri(_XPLATSTR("wss://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection%20token")),
url_builder::build_connect(web::uri{ _XPLATSTR("https://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection token"), _XPLATSTR("")));
}
TEST(url_builder_connect_webSockets, url_correct_if_query_string_not_empty)
{
ASSERT_EQ(
web::uri(_XPLATSTR("ws://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection-token"), _XPLATSTR("q1=1&q2=2")));
ASSERT_EQ(
web::uri(_XPLATSTR("ws://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("http://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection-token"), _XPLATSTR("&q1=1&q2=2")));
ASSERT_EQ(
web::uri(_XPLATSTR("wss://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("https://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection-token"), _XPLATSTR("q1=1&q2=2")));
ASSERT_EQ(
web::uri(_XPLATSTR("wss://fake/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=connection-token&q1=1&q2=2")),
url_builder::build_connect(web::uri{ _XPLATSTR("https://fake/signalr/") },
transport_type::websockets, _XPLATSTR("connection-token"), _XPLATSTR("&q1=1&q2=2")));
}