1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Tests cases manually building up HTTP requests with progress handlers.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#ifdef _WIN32
#include <WinError.h>
#endif
using namespace web::http;
using namespace web::http::client;
using namespace tests::functional::http::utilities;
namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
SUITE(progress_handler_tests)
{
TEST_FIXTURE(uri_address, set_progress_handler_no_bodies)
{
http_client_config config;
config.set_chunksize(512);
http_client client(m_uri, config);
const method mtd = methods::GET;
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
test_http_server::scoped_server scoped(m_uri);
scoped.server()->next_request().then([&](test_request* p_request) {
http_asserts::assert_test_request_equals(p_request, mtd, U("/"));
std::map<utility::string_t, utility::string_t> headers;
p_request->reply(200, utility::string_t(U("OK")), headers);
});
auto response = client.request(msg).get();
http_asserts::assert_response_equals(response, status_codes::OK);
VERIFY_ARE_EQUAL(0, upsize);
response.content_ready().wait();
VERIFY_ARE_EQUAL(0, downsize);
VERIFY_ARE_EQUAL(2, calls);
}
TEST_FIXTURE(uri_address, set_progress_handler_upload)
{
http_client_config config;
config.set_chunksize(512);
http_client client(m_uri, config);
const method mtd = methods::POST;
utility::string_t data;
utility::string_t content_type = U("text/plain; charset=utf-8");
const size_t repeats = 5500;
for (size_t i = 0; i < repeats; ++i)
data.append(U("abcdefghihklmnopqrstuvwxyz"));
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
msg.set_body(data);
test_http_server::scoped_server scoped(m_uri);
scoped.server()->next_request().then([&](test_request* p_request) {
http_asserts::assert_test_request_equals(p_request, mtd, U("/"), content_type, data);
std::map<utility::string_t, utility::string_t> headers;
p_request->reply(200, utility::string_t(U("OK")), headers);
});
auto response = client.request(msg).get();
http_asserts::assert_response_equals(response, status_codes::OK);
VERIFY_ARE_EQUAL(26u * repeats, upsize);
response.content_ready().wait();
VERIFY_ARE_EQUAL(0, downsize);
// We don't have very precise control over how much of a message is transferred
// in each chunk being sent or received, so we can't make an exact comparison here.
VERIFY_IS_TRUE(calls >= 3);
}
TEST_FIXTURE(uri_address, set_progress_handler_download)
{
http_client_config config;
config.set_chunksize(512);
http_client client(m_uri, config);
const method mtd = methods::GET;
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
const size_t repeats = 6000;
test_http_server::scoped_server scoped(m_uri);
scoped.server()->next_request().then([&](test_request* p_request) {
http_asserts::assert_test_request_equals(p_request, mtd, U("/"));
std::string resp_data;
for (size_t i = 0; i < repeats; ++i)
resp_data.append("abcdefghihklmnopqrstuvwxyz");
std::map<utility::string_t, utility::string_t> headers;
headers[U("Content-Type")] = U("text/plain");
p_request->reply(200, utility::string_t(U("OK")), headers, resp_data);
});
auto response = client.request(msg).get();
http_asserts::assert_response_equals(response, status_codes::OK);
VERIFY_ARE_EQUAL(0, upsize);
response.content_ready().wait();
VERIFY_ARE_EQUAL(26u * repeats, downsize);
// We don't have very precise control over how much of a message is transferred
// in each chunk being sent or received, so we can't make an exact comparison here.
VERIFY_IS_TRUE(calls > 4);
}
TEST_FIXTURE(uri_address, set_progress_handler_upload_and_download)
{
http_client_config config;
config.set_chunksize(512);
http_client client(m_uri, config);
const method mtd = methods::POST;
utility::string_t data;
utility::string_t content_type = U("text/plain; charset=utf-8");
const size_t repeats = 5500;
for (size_t i = 0; i < repeats; ++i)
data.append(U("abcdefghihklmnopqrstuvwxyz"));
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
msg.set_body(data);
test_http_server::scoped_server scoped(m_uri);
scoped.server()->next_request().then([&](test_request* p_request) {
http_asserts::assert_test_request_equals(p_request, mtd, U("/"), content_type, data);
std::string resp_data;
for (size_t i = 0; i < repeats * 2; ++i)
resp_data.append("abcdefghihklmnopqrstuvwxyz");
std::map<utility::string_t, utility::string_t> headers;
headers[U("Content-Type")] = U("text/plain");
p_request->reply(200, utility::string_t(U("OK")), headers, resp_data);
});
auto response = client.request(msg).get();
http_asserts::assert_response_equals(response, status_codes::OK);
VERIFY_ARE_EQUAL(26u * repeats, upsize);
response.content_ready().wait();
VERIFY_ARE_EQUAL(26u * repeats * 2, downsize);
// We don't have very precise control over how much of a message is transferred
// in each chunk being sent or received, so we can't make an exact comparison here.
VERIFY_IS_TRUE(calls > 4);
}
TEST_FIXTURE(uri_address, set_progress_handler_open_failure)
{
http_client client(U("http://localhost323:-1"));
const method mtd = methods::POST;
utility::string_t data;
utility::string_t content_type = U("text/plain; charset=utf-8");
const size_t repeats = 5500;
for (size_t i = 0; i < repeats; ++i)
data.append(U("abcdefghihklmnopqrstuvwxyz"));
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
// We should never see this handler called.
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
msg.set_body(data);
auto response = client.request(msg);
VERIFY_THROWS(response.get(), web::http::http_exception);
VERIFY_ARE_EQUAL(4711u, upsize);
VERIFY_ARE_EQUAL(4711u, downsize);
VERIFY_ARE_EQUAL(0, calls);
}
TEST_FIXTURE(uri_address, set_progress_handler_request_timeout)
{
test_http_server::scoped_server scoped(m_uri);
http_client_config config;
config.set_chunksize(512);
config.set_timeout(utility::seconds(1));
http_client client(m_uri, config);
const method mtd = methods::POST;
utility::string_t data;
utility::string_t content_type = U("text/plain; charset=utf-8");
const size_t repeats = 5500;
for (size_t i = 0; i < repeats; ++i)
data.append(U("abcdefghihklmnopqrstuvwxyz"));
utility::size64_t upsize = 4711u, downsize = 4711u;
int calls = 0;
http_request msg(mtd);
// We should never see this handler called for download, but for upload should still happen, since
// there's a server (just not a very responsive one) and we're sending data to it.
msg.set_progress_handler([&](message_direction::direction direction, utility::size64_t so_far) {
calls += 1;
if (direction == message_direction::upload)
upsize = so_far;
else
downsize = so_far;
});
msg.set_body(data);
auto t = scoped.server()->next_request();
auto response = client.request(msg);
#ifdef __APPLE__
// CodePlex 295
VERIFY_THROWS(response.get(), http_exception);
#else
VERIFY_THROWS_HTTP_ERROR_CODE(response.get(), std::errc::timed_out);
#endif
VERIFY_ARE_EQUAL(26u * repeats, upsize);
VERIFY_ARE_EQUAL(4711u, downsize);
// We don't have very precise control over how much of the message is transferred
// before the exception occurs, so we can't make an exact comparison here.
VERIFY_IS_TRUE(calls >= 2);
t.get();
}
TEST_FIXTURE(uri_address, upload_nobody_exception)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
http_request msg(methods::GET);
auto t = scoped.server()->next_request().then(
[&](test_request* p_request) { p_request->reply(200, utility::string_t(U("OK"))); });
msg.set_progress_handler([](message_direction::direction, utility::size64_t) {
// First all is for data upload completion
throw std::invalid_argument("fake error");
});
VERIFY_THROWS(client.request(msg).get(), std::invalid_argument);
t.get();
}
TEST_FIXTURE(uri_address, download_nobody_exception)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
http_request msg(methods::GET);
scoped.server()->next_request().then(
[&](test_request* p_request) { p_request->reply(200, utility::string_t(U("OK"))); });
int numCalls = 0;
msg.set_progress_handler([&](message_direction::direction, utility::size64_t) {
if (++numCalls == 2)
{
// second is for data download
throw std::invalid_argument("fake error");
}
});
VERIFY_THROWS(client.request(msg).get().content_ready().get(), std::invalid_argument);
}
TEST_FIXTURE(uri_address, data_upload_exception)
{
http_client client(m_uri);
http_request msg(methods::PUT);
msg.set_body(U("A"));
msg.set_progress_handler(
[&](message_direction::direction, utility::size64_t) { throw std::invalid_argument("fake error"); });
pplx::task<test_request*> t;
{
test_http_server::scoped_server scoped(m_uri);
t = scoped.server()->next_request();
VERIFY_THROWS(client.request(msg).get(), std::invalid_argument);
}
try
{
t.get();
}
catch (const std::runtime_error&)
{ /* It is ok if the request does not complete before the server is shutdown */
}
}
TEST_FIXTURE(uri_address, data_download_exception, "Ignore:Windows", "395")
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
http_request msg(methods::GET);
auto t = scoped.server()->next_request().then([&](test_request* p_request) {
std::string resp_data("abc");
std::map<utility::string_t, utility::string_t> headers;
headers[U("Content-Type")] = U("text/plain");
p_request->reply(200, utility::string_t(U("OK")), headers, resp_data);
});
int numCalls = 0;
msg.set_progress_handler([&](message_direction::direction, utility::size64_t) {
if (++numCalls == 2)
{
// 2rd is for data download
throw std::invalid_argument("fake error");
}
});
try
{
handle_timeout([&] { client.request(msg).get().content_ready().get(); });
}
catch (std::invalid_argument const&)
{
// Expected.
}
t.get();
}
}
} // namespace client
} // namespace http
} // namespace functional
} // namespace tests
|