Skip to content

Commit 2321e84

Browse files
author
Thomas Schaub
committed
Add test to expose http_client_asio bug
1 parent 53293e4 commit 2321e84

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Release/tests/functional/http/client/connections_and_errors.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "cpprest/http_listener.h"
2828
#endif
2929

30+
#include <chrono>
31+
#include <thread>
32+
3033
using namespace web;
3134
using namespace utility;
3235
using namespace concurrency;
@@ -400,6 +403,36 @@ TEST_FIXTURE(uri_address, cancel_while_downloading_data)
400403
}
401404
#endif
402405

406+
// Try to connect to a server on a closed port and cancel the operation.
407+
TEST_FIXTURE(uri_address, cancel_bad_port)
408+
{
409+
// http_client_asio has a bug where, when canceled, it will cancel only the
410+
// current connection but then go and try the next address from the list of
411+
// resolved addresses, i.e., it won't actually cancel as long as there are
412+
// more addresses to try. Consequently, it will not report the task as being
413+
// canceled. This is easiest to observe when trying to connect to a server
414+
// that does not respond on a certain port, otherwise the timing might be
415+
// tricky.
416+
417+
// We need to connect to a URI for which there are multiple addresses
418+
// associated (i.e., multiple A records).
419+
web::http::uri uri(U("https://microsoft.com:442/"));
420+
421+
// Send request.
422+
http_client c(uri);
423+
web::http::http_request r;
424+
auto cts = pplx::cancellation_token_source();
425+
auto ct = cts.get_token();
426+
auto t = c.request(r, ct);
427+
428+
// Make sure that the client already finished resolving before canceling,
429+
// otherwise the bug might not be triggered.
430+
std::this_thread::sleep_for(std::chrono::seconds(1));
431+
cts.cancel();
432+
433+
VERIFY_THROWS_HTTP_ERROR_CODE(t.get(), std::errc::operation_canceled);
434+
}
435+
403436
} // SUITE(connections_and_errors)
404437

405438
}}}}

0 commit comments

Comments
 (0)