diff --git a/src/signalrclient/default_http_client.cpp b/src/signalrclient/default_http_client.cpp index 49591ff..8df1fd6 100644 --- a/src/signalrclient/default_http_client.cpp +++ b/src/signalrclient/default_http_client.cpp @@ -86,35 +86,51 @@ namespace signalr cts.cancel(); }); - web::http::client::http_client client(utility::conversions::to_string_t(url), m_config.get_http_client_config()); - client.request(http_request, cts.get_token()) - .then([context, callback](pplx::task response_task) + try { - try - { - auto http_response = response_task.get(); - auto status_code = http_response.status_code(); - http_response.extract_utf8string() - .then([callback, status_code](std::string response_body) - { - signalr::http_response response; - response.content = response_body; - response.status_code = status_code; - callback(std::move(response), nullptr); - }); - } - catch (...) - { - callback(http_response(), std::current_exception()); - } + web::http::client::http_client client(utility::conversions::to_string_t(url), m_config.get_http_client_config()); + client.request(http_request, cts.get_token()) + .then([context, callback](pplx::task response_task) + { + try + { + auto http_response = response_task.get(); + auto status_code = http_response.status_code(); + http_response.extract_utf8string() + .then([callback, status_code](std::string response_body) + { + signalr::http_response response; + response.content = response_body; + response.status_code = status_code; + callback(std::move(response), nullptr); + }); + } + catch (...) + { + callback(http_response(), std::current_exception()); + } + if (context != nullptr) + { + std::unique_lock lck(context->mtx); + context->complete = true; + context->cv.notify_all(); + } + }); + } + // Url validation could throw from the client ctor + catch (...) + { + // unblock timeout task if it was created if (context != nullptr) { std::unique_lock lck(context->mtx); context->complete = true; context->cv.notify_all(); } - }); + callback({}, std::current_exception()); + return; + } } } #endif \ No newline at end of file diff --git a/test/signalrclienttests/CMakeLists.txt b/test/signalrclienttests/CMakeLists.txt index ecb5e0b..b8f8d1e 100644 --- a/test/signalrclienttests/CMakeLists.txt +++ b/test/signalrclienttests/CMakeLists.txt @@ -29,6 +29,12 @@ if(USE_MSGPACK) ) endif() +if(USE_CPPRESTSDK) + list (APPEND SOURCES + cpprestsdk_tests.cpp + ) +endif() + include_directories( ../../src/signalrclient ) @@ -85,6 +91,28 @@ list (APPEND libraries ${JSONCPP_LIB}) list (APPEND libraries gtest) +if(NOT USE_CPPRESTSDK) + target_link_libraries(signalrclienttests) +else() + set_target_properties(signalrclienttests PROPERTIES COMPILE_FLAGS "-DUSE_CPPRESTSDK" ) + + if(APPLE) + list (APPEND libraries + ${CPPREST_LIB} + OpenSSL::SSL Boost::boost Boost::system Boost::chrono Boost::thread + ) + elseif(NOT WIN32) + list (APPEND libraries + ${CPPREST_LIB} Boost::system + OpenSSL::SSL + ) + else() + list (APPEND libraries + ${CPPREST_LIB} + ) + endif() +endif() + target_link_libraries(signalrclienttests ${libraries}) add_test(NAME signalrclienttests COMMAND signalrclienttests) diff --git a/test/signalrclienttests/cpprestsdk_tests.cpp b/test/signalrclienttests/cpprestsdk_tests.cpp new file mode 100644 index 0000000..9ce7090 --- /dev/null +++ b/test/signalrclienttests/cpprestsdk_tests.cpp @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include "stdafx.h" +#include "connection_impl.h" +#include "test_utils.h" +#include "memory_log_writer.h" + +using namespace signalr; + +TEST(cpprestsdk, invalid_url_errors) +{ + auto connection = connection_impl::create("/path", trace_level::debug, std::make_shared(), nullptr, nullptr, false); + + manual_reset_event mre; + connection->start([&mre](std::exception_ptr ex) + { + mre.set(ex); + }); + + try + { + mre.get(); + ASSERT_TRUE(false); + } + catch (const std::exception& ex) + { + ASSERT_STREQ("URI must contain a hostname.", ex.what()); + } +} \ No newline at end of file