Minor fixes to enabling http/websocket client configuration

This commit is contained in:
Pawel Kadluczka 2017-01-13 21:05:12 -08:00
Родитель 3bc6b067c8
Коммит 2021090834
14 изменённых файлов: 51 добавлений и 38 удалений

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

@ -21,7 +21,7 @@ namespace signalr
class hub_connection
{
public:
SIGNALRCLIENT_API explicit hub_connection(const utility::string_t& url, const utility::string_t& query_string = U(""),
SIGNALRCLIENT_API explicit hub_connection(const utility::string_t& url, const utility::string_t& query_string = _XPLATSTR(""),
trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr, bool use_default_url = true);
SIGNALRCLIENT_API ~hub_connection();

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

@ -236,8 +236,8 @@ namespace signalr
};
auto transport = connection->m_transport_factory->create_transport(
transport_type::websockets, connection->m_logger, process_response_callback,
error_callback, connection->m_signalr_client_config);
transport_type::websockets, connection->m_logger, connection->m_signalr_client_config,
process_response_callback, error_callback);
pplx::create_task([negotiation_response, connect_request_tce, disconnect_cts, weak_connection]()
{
@ -491,8 +491,8 @@ namespace signalr
}
// This is fire and forget because we don't really care about the result
request_sender::abort(*m_web_request_factory, m_base_url, m_transport->get_transport_type(), m_connection_token, m_connection_data, m_query_string,
m_signalr_client_config)
request_sender::abort(*m_web_request_factory, m_base_url, m_transport->get_transport_type(), m_connection_token,
m_connection_data, m_query_string, m_signalr_client_config)
.then([](pplx::task<utility::string_t> abort_task)
{
try
@ -774,7 +774,7 @@ namespace signalr
void connection_impl::set_client_config(const signalr_client_config& config)
{
ensure_disconnected(U("cannot set client config when the connection is not in the disconnected state. "));
ensure_disconnected(_XPLATSTR("cannot set client config when the connection is not in the disconnected state. "));
m_signalr_client_config = config;
}

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

@ -17,8 +17,9 @@ namespace signalr
request->set_method(web::http::methods::GET);
request->set_user_agent(USER_AGENT);
request->set_client_config(signalr_client_config);
return request->get_response(signalr_client_config).then([](web_response response)
return request->get_response().then([](web_response response)
{
if (response.status_code != 200)
{

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

@ -8,13 +8,14 @@
namespace signalr
{
std::shared_ptr<transport> transport_factory::create_transport(transport_type transport_type, const logger& logger,
const signalr_client_config& signalr_client_config,
std::function<void(const utility::string_t&)> process_response_callback,
std::function<void(const std::exception&)> error_callback,
const signalr_client_config& signalr_client_config)
std::function<void(const std::exception&)> error_callback)
{
if (transport_type == signalr::transport_type::websockets)
{
return websocket_transport::create([&signalr_client_config](){ return std::make_shared<default_websocket_client>(signalr_client_config); },
return websocket_transport::create(
[signalr_client_config](){ return std::make_shared<default_websocket_client>(signalr_client_config); },
logger, process_response_callback, error_callback);
}

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

@ -14,9 +14,9 @@ namespace signalr
{
public:
virtual std::shared_ptr<transport> create_transport(transport_type transport_type, const logger& logger,
const signalr_client_config& signalr_client_config,
std::function<void(const utility::string_t&)> process_response_callback,
std::function<void(const std::exception&)> error_callback,
const signalr_client_config& signalr_client_config = signalr_client_config{});
std::function<void(const std::exception&)> error_callback);
virtual ~transport_factory();
};

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

@ -21,11 +21,16 @@ namespace signalr
m_user_agent_string = user_agent_string;
}
pplx::task<web_response> web_request::get_response(const signalr_client_config& signalr_client_config)
void web_request::set_client_config(const signalr_client_config& signalr_client_config)
{
web::http::client::http_client client(m_url, signalr_client_config.get_http_client_config());
m_signalr_client_config = signalr_client_config;
}
m_request.headers() = signalr_client_config.get_http_headers();
pplx::task<web_response> web_request::get_response()
{
web::http::client::http_client client(m_url, m_signalr_client_config.get_http_client_config());
m_request.headers() = m_signalr_client_config.get_http_headers();
if (!m_user_agent_string.empty())
{
m_request.headers()[_XPLATSTR("User-Agent")] = m_user_agent_string;

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

@ -15,8 +15,9 @@ namespace signalr
virtual void set_method(const utility::string_t &method);
virtual void set_user_agent(const utility::string_t &user_agent_string);
virtual void set_client_config(const signalr_client_config& signalr_client_config);
virtual pplx::task<web_response> get_response(const signalr_client_config& signalr_client_config = signalr_client_config{});
virtual pplx::task<web_response> get_response();
web_request& operator=(const web_request&) = delete;
@ -26,5 +27,6 @@ namespace signalr
const web::uri m_url;
web::http::http_request m_request;
utility::string_t m_user_agent_string;
signalr_client_config m_signalr_client_config;
};
}

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

@ -488,25 +488,24 @@ TEST(hub_connection_tests, mirror_header)
hub_conn->set_client_config(signalr_client_config);
{
auto test = hub_conn->start().then([&hub_proxy]()
auto mirrored_header_value = hub_conn->start().then([&hub_proxy]()
{
return hub_proxy.invoke<web::json::value>(U("mirrorHeader"));
}).get();
ASSERT_EQ(U("MirrorThis"), test.as_string());
ASSERT_EQ(U("MirrorThis"), mirrored_header_value.as_string());
}
hub_conn->stop().wait();
headers[U("x-mirror")] = U("MirrorThat");
signalr_client_config.set_http_headers(headers);
ASSERT_THROW(hub_conn->set_client_config(signalr_client_config), signalr::signalr_exception);
hub_conn->stop().wait();
hub_conn->set_client_config(signalr_client_config);
{
auto test = hub_conn->start().then([&hub_proxy]()
auto mirrored_header_value = hub_conn->start().then([&hub_proxy]()
{
return hub_proxy.invoke<web::json::value>(U("mirrorHeader"));
}).get();
ASSERT_EQ(U("MirrorThat"), test.as_string());
ASSERT_EQ(U("MirrorThat"), mirrored_header_value.as_string());
}
}

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

@ -1084,8 +1084,9 @@ TEST(connection_impl_config, custom_headers_set_in_requests)
auto request = new web_request_stub((unsigned short)200, _XPLATSTR("OK"), response_body);
request->on_get_response = [](web_request_stub& request)
{
ASSERT_EQ(1, request.m_headers.size());
ASSERT_EQ(_XPLATSTR("42"), request.m_headers[_XPLATSTR("Answer")]);
auto http_headers = request.m_signalr_client_config.get_http_headers();
ASSERT_EQ(1, http_headers.size());
ASSERT_EQ(_XPLATSTR("42"), http_headers[_XPLATSTR("Answer")]);
};
return std::unique_ptr<web_request>(request);

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

@ -74,8 +74,9 @@ TEST(http_sender_get_response, headers_set)
auto request = new web_request_stub((unsigned short)200, _XPLATSTR("OK"), response_body);
request->on_get_response = [](web_request_stub& request)
{
ASSERT_EQ(1, request.m_headers.size());
ASSERT_EQ(_XPLATSTR("123"), request.m_headers[_XPLATSTR("abc")]);
auto http_headers = request.m_signalr_client_config.get_http_headers();
ASSERT_EQ(1, http_headers.size());
ASSERT_EQ(_XPLATSTR("123"), http_headers[_XPLATSTR("abc")]);
};
return std::unique_ptr<web_request>(request);

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

@ -10,9 +10,8 @@ test_transport_factory::test_transport_factory(const std::shared_ptr<websocket_c
{ }
std::shared_ptr<transport> test_transport_factory::create_transport(transport_type transport_type, const logger& logger,
std::function<void(const utility::string_t&)> process_message_callback,
std::function<void(const std::exception&)> error_callback,
const signalr_client_config&)
const signalr_client_config&, std::function<void(const utility::string_t&)> process_message_callback,
std::function<void(const std::exception&)> error_callback)
{
if (transport_type == signalr::transport_type::websockets)
{

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

@ -14,10 +14,9 @@ public:
test_transport_factory(const std::shared_ptr<websocket_client>& websocket_client);
std::shared_ptr<transport> create_transport(transport_type transport_type, const logger& logger,
const signalr_client_config& signalr_client_config,
std::function<void(const utility::string_t&)> process_message_callback,
std::function<void(const std::exception&)> error_callback,
const signalr_client_config& signalr_client_config = signalr_client_config{})
override;
std::function<void(const std::exception&)> error_callback) override;
private:
std::shared_ptr<websocket_client> m_websocket_client;

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

@ -18,9 +18,13 @@ void web_request_stub::set_user_agent(const utility::string_t &user_agent_string
m_user_agent_string = user_agent_string;
}
pplx::task<web_response> web_request_stub::get_response(const signalr::signalr_client_config& signalr_client_config)
void web_request_stub::set_client_config(const signalr_client_config& config)
{
m_signalr_client_config = config;
}
pplx::task<web_response> web_request_stub::get_response()
{
m_headers = signalr_client_config.get_http_headers();
on_get_response(*this);
return pplx::task_from_result<web_response>(

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

@ -15,13 +15,14 @@ struct web_request_stub : public web_request
utility::string_t m_response_body;
utility::string_t m_method;
utility::string_t m_user_agent_string;
web::http::http_headers m_headers;
signalr_client_config m_signalr_client_config;
std::function<void(web_request_stub&)> on_get_response = [](web_request_stub&){};
web_request_stub(unsigned short status_code, const utility::string_t& reason_phrase, const utility::string_t& response_body = _XPLATSTR(""));
virtual void set_method(const utility::string_t &method) override;
virtual void set_user_agent(const utility::string_t &user_agent_string) override;
virtual void set_client_config(const signalr_client_config& client_config) override;
virtual pplx::task<web_response> get_response(const signalr::signalr_client_config&) override;
virtual pplx::task<web_response> get_response() override;
};