Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/functional/http/client/response_extract_tests.cpp
f2a6ba12fc29
22.4 KiB
text/x-c++hdr
Slack frontend stub
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | /***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* response_extract_tests.cpp
*
* Tests cases covering extract functions on HTTP response.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#ifndef __cplusplus_winrt
#include "cpprest/http_listener.h"
#endif
using namespace web;
using namespace utility;
using namespace concurrency;
using namespace utility::conversions;
using namespace web::http;
using namespace web::http::client;
using namespace tests::functional::http::utilities;
namespace tests { namespace functional { namespace http { namespace client {
SUITE(response_extract_tests)
{
// Helper function to send a request and response with given values.
template<typename CharType>
static http_response send_request_response(
test_http_server *p_server,
http_client * p_client,
const utility::string_t&content_type,
const std::basic_string<CharType> &data)
{
const method method = methods::GET;
const ::http::status_code code = status_codes::OK;
std::map<utility::string_t, utility::string_t> headers;
if(!content_type.empty())
{
headers[U("Content-Type")] = content_type;
}
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"));
VERIFY_ARE_EQUAL(0u, p_request->reply(code, U(""), headers, data));
});
http_response rsp = p_client->request(method).get();
http_asserts::assert_response_equals(rsp, code, headers);
return rsp;
}
utf16string switch_endian_ness(const utf16string &src_str)
{
utf16string dest_str;
dest_str.resize(src_str.size());
unsigned char *src = (unsigned char *)&src_str[0];
unsigned char *dest = (unsigned char *)&dest_str[0];
for(size_t i = 0; i < dest_str.size() * 2; i+=2)
{
dest[i] = src[i + 1];
dest[i + 1] = src[i];
}
return dest_str;
}
TEST_FIXTURE(uri_address, extract_string)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// default encoding (Latin1)
std::string data("YOU KNOW ITITITITI");
http_response rsp = send_request_response(scoped.server(), &client, U("text/plain"), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// us-ascii
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset= us-AscIi"), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// Latin1
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=iso-8859-1"), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// utf-8
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset = UTF-8"), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// "utf-8" - quoted charset
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=\"utf-8\""), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// no content length
rsp = send_request_response(scoped.server(), &client, U(""), utility::string_t());
auto str = rsp.to_string();
// If there is no Content-Type in the response, make sure it won't throw when we ask for string
if(str.find(U("Content-Type")) == std::string::npos)
{
VERIFY_ARE_EQUAL(utility::string_t(U("")), rsp.extract_string().get());
}
// utf-16le
data = "YES NOW, HERHEHE****";
utf16string wdata(utf8_to_utf16(data));
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16le"), wdata);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// utf-16be
wdata = switch_endian_ness(wdata);
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16be"), wdata);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// utf-16 no BOM (utf-16be)
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// utf-16 big endian BOM.
wdata.insert(wdata.begin(), ('\0'));
unsigned char * start = (unsigned char *)&wdata[0];
start[0] = 0xFE;
start[1] = 0xFF;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
// utf-16 little endian BOM.
wdata = utf8_to_utf16("YOU KNOW THIS **********KICKS");
data = utf16_to_utf8(wdata);
wdata.insert(wdata.begin(), '\0');
start = (unsigned char *)&wdata[0];
start[0] = 0xFF;
start[1] = 0xFE;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string().get());
}
TEST_FIXTURE(uri_address, extract_utf8string)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// default encoding (Latin1)
std::string data("YOU KNOW ITITITITI");
http_response rsp = send_request_response(scoped.server(), &client, U("text/plain"), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// us-ascii
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset= us-AscIi"), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// Latin1
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=iso-8859-1"), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// utf-8
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset = UTF-8"), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// "utf-8" - quoted charset
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=\"utf-8\""), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// no content length
rsp = send_request_response(scoped.server(), &client, U(""), utility::string_t());
auto str = rsp.to_string();
// If there is no Content-Type in the response, make sure it won't throw when we ask for string
if (str.find(U("Content-Type")) == std::string::npos)
{
VERIFY_ARE_EQUAL("", rsp.extract_utf8string().get());
}
// utf-16le
data = "YES NOW, HERHEHE****";
utf16string wdata(utf8_to_utf16(data));
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16le"), wdata);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// utf-16be
wdata = switch_endian_ness(wdata);
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16be"), wdata);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// utf-16 no BOM (utf-16be)
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// utf-16 big endian BOM.
wdata.insert(wdata.begin(), ('\0'));
unsigned char * start = (unsigned char *) &wdata[0];
start[0] = 0xFE;
start[1] = 0xFF;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
// utf-16 little endian BOM.
wdata = utf8_to_utf16("YOU KNOW THIS **********KICKS");
data = utf16_to_utf8(wdata);
wdata.insert(wdata.begin(), '\0');
start = (unsigned char *) &wdata[0];
start[0] = 0xFF;
start[1] = 0xFE;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdata);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string().get());
}
TEST_FIXTURE(uri_address, extract_utf16string)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// default encoding (Latin1)
std::string data("YOU KNOW ITITITITI");
utf16string wdata(utf8_to_utf16(data));
http_response rsp = send_request_response(scoped.server(), &client, U("text/plain"), data);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// us-ascii
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset= us-AscIi"), data);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// Latin1
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=iso-8859-1"), data);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// utf-8
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset = UTF-8"), data);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// "utf-8" - quoted charset
rsp = send_request_response(scoped.server(), &client, U("text/plain;charset=\"utf-8\""), data);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// no content length
rsp = send_request_response(scoped.server(), &client, U(""), utility::string_t());
auto str = rsp.to_string();
// If there is no Content-Type in the response, make sure it won't throw when we ask for string
if (str.find(U("Content-Type")) == std::string::npos)
{
VERIFY_ARE_EQUAL(utf16string(), rsp.extract_utf16string().get());
}
// utf-16le
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16le"), wdata);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// utf-16be
auto wdatabe = switch_endian_ness(wdata);
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16be"), wdatabe);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// utf-16 no BOM (utf-16be)
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdatabe);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// utf-16 big endian BOM.
wdatabe.insert(wdatabe.begin(), ('\0'));
unsigned char * start = (unsigned char *) &wdatabe[0];
start[0] = 0xFE;
start[1] = 0xFF;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdatabe);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
// utf-16 little endian BOM.
auto wdatale = wdata;
wdatale.insert(wdatale.begin(), '\0');
start = (unsigned char *) &wdatale[0];
start[0] = 0xFF;
start[1] = 0xFE;
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), wdatale);
VERIFY_ARE_EQUAL(wdata, rsp.extract_utf16string().get());
}
TEST_FIXTURE(uri_address, extract_string_force)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
std::string data("YOU KNOW ITITITITI");
http_response rsp = send_request_response(scoped.server(), &client, U("bad unknown charset"), data);
VERIFY_ARE_EQUAL(to_string_t(data), rsp.extract_string(true).get());
rsp = send_request_response(scoped.server(), &client, U("bad unknown charset"), data);
VERIFY_ARE_EQUAL(data, rsp.extract_utf8string(true).get());
rsp = send_request_response(scoped.server(), &client, U("bad unknown charset"), data);
VERIFY_ARE_EQUAL(to_utf16string(data), rsp.extract_utf16string(true).get());
}
TEST_FIXTURE(uri_address, extract_string_incorrect)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// with non matching content type.
const std::string data("YOU KNOW ITITITITI");
http_response rsp = send_request_response(scoped.server(), &client, U("non_text"), data);
VERIFY_THROWS(rsp.extract_string().get(), http_exception);
// with unknown charset
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=uis-ascii"), data);
VERIFY_THROWS(rsp.extract_string().get(), http_exception);
}
#ifndef __cplusplus_winrt
TEST_FIXTURE(uri_address, extract_empty_string)
{
web::http::experimental::listener::http_listener listener(m_uri);
http_client client(m_uri);
listener.support([](http_request msg)
{
auto ResponseStreamBuf = streams::producer_consumer_buffer<uint8_t>();
ResponseStreamBuf.close(std::ios_base::out).wait();
http_response response(status_codes::OK);
response.set_body(ResponseStreamBuf.create_istream(), U("text/plain"));
response.headers().add(header_names::connection, U("close"));
msg.reply(response).wait();
});
listener.open().wait();
auto response = client.request(methods::GET).get();
auto data = response.extract_string().get();
VERIFY_ARE_EQUAL(0, data.size());
listener.close().wait();
}
#endif
TEST_FIXTURE(uri_address, extract_json)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// default encoding (Latin1)
json::value data = json::value::string(U("JSON string object"));
http_response rsp = send_request_response(scoped.server(), &client, U("application/json"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// us-ascii
rsp = send_request_response(scoped.server(), &client, U("application/json; charset= us-AscIi"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// Latin1
rsp = send_request_response(scoped.server(), &client, U("application/json;charset=iso-8859-1"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// utf-8
rsp = send_request_response(scoped.server(), &client, U("application/json; charset = UTF-8"), to_utf8string((data.serialize())));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U(""), utility::string_t());
auto str = rsp.to_string();
// If there is no Content-Type in the response, make sure it won't throw when we ask for json
if(str.find(U("Content-Type")) == std::string::npos)
{
VERIFY_ARE_EQUAL(utility::string_t(U("null")), rsp.extract_json().get().serialize());
}
#ifdef _WIN32
// utf-16le
auto utf16str = data.serialize();
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=utf-16le"), utf16str);
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// utf-16be
utf16string modified_data = data.serialize();
modified_data = switch_endian_ness(modified_data);
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=utf-16be"), modified_data);
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// utf-16 no BOM (utf-16be)
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=utf-16"), modified_data);
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// utf-16 big endian BOM.
modified_data.insert(modified_data.begin(), U('\0'));
unsigned char * start = (unsigned char *)&modified_data[0];
start[0] = 0xFE;
start[1] = 0xFF;
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=utf-16"), modified_data);
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
// utf-16 little endian BOM.
modified_data = data.serialize();
modified_data.insert(modified_data.begin(), U('\0'));
start = (unsigned char *)&modified_data[0];
start[0] = 0xFF;
start[1] = 0xFE;
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=utf-16"), modified_data);
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
#endif
// unofficial JSON MIME types
rsp = send_request_response(scoped.server(), &client, U("text/json"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U("text/x-json"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U("text/javascript"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U("text/x-javascript"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U("application/javascript"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
rsp = send_request_response(scoped.server(), &client, U("application/x-javascript"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json().get().serialize());
}
TEST_FIXTURE(uri_address, extract_json_force)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
json::value data = json::value::string(U("JSON string object"));
http_response rsp = send_request_response(scoped.server(), &client, U("bad charset"), to_utf8string(data.serialize()));
VERIFY_ARE_EQUAL(data.serialize(), rsp.extract_json(true).get().serialize());
}
TEST_FIXTURE(uri_address, extract_json_incorrect)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// with non matching content type.
json::value json_data = json::value::string(U("JSON string object"));
http_response rsp = send_request_response(scoped.server(), &client, U("bad guy"), json_data.serialize());
VERIFY_THROWS(rsp.extract_json().get(), http_exception);
// with unknown charset.
rsp = send_request_response(scoped.server(), &client, U("application/json; charset=us-askjhcii"), json_data.serialize());
VERIFY_THROWS(rsp.extract_json().get(), http_exception);
}
TEST_FIXTURE(uri_address, set_stream_try_extract_json)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
http_request request(methods::GET);
streams::ostream responseStream = streams::bytestream::open_ostream<std::vector<uint8_t>>();
request.set_response_stream(responseStream);
scoped.server()->next_request().then([](test_request *req)
{
std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = U("application/json");
req->reply(status_codes::OK, U("OK"), headers, U("{true}"));
});
http_response response = client.request(request).get();
VERIFY_THROWS(response.extract_json().get(), http_exception);
}
TEST_FIXTURE(uri_address, extract_vector)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
// textual content type - with unknown charset
std::string data("YOU KNOW ITITITITI");
std::vector<unsigned char> vector_data;
std::for_each(data.begin(), data.end(), [&](char ch)
{
vector_data.push_back((unsigned char)ch);
});
http_response rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=unknown"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with us-ascii
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset= us-AscIi"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with Latin1
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=iso-8859-1"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with utf-8
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-8"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with utf-16le
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16LE"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with utf-16be
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=UTF-16be"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// textual type with utf-16
rsp = send_request_response(scoped.server(), &client, U("text/plain; charset=utf-16"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
// non textual content type
rsp = send_request_response(scoped.server(), &client, U("blah; charset=utf-16"), data);
VERIFY_ARE_EQUAL(vector_data, rsp.extract_vector().get());
}
TEST_FIXTURE(uri_address, set_stream_try_extract_vector)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
http_request request(methods::GET);
streams::ostream responseStream = streams::bytestream::open_ostream<std::vector<uint8_t>>();
request.set_response_stream(responseStream);
scoped.server()->next_request().then([](test_request *req)
{
std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = U("text/plain");
req->reply(status_codes::OK, U("OK"), headers, U("data"));
});
http_response response = client.request(request).get();
VERIFY_THROWS(response.extract_vector().get(), http_exception);
}
TEST_FIXTURE(uri_address, head_response)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
const method method = methods::HEAD;
const ::http::status_code code = status_codes::OK;
std::map<utility::string_t, utility::string_t> headers;
headers[U("Content-Type")] = U("text/plain");
headers[U("Content-Length")] = U("100");
scoped.server()->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"));
VERIFY_ARE_EQUAL(0u, p_request->reply(code, U(""), headers));
});
http_response rsp = client.request(method).get();
VERIFY_ARE_EQUAL(0u, rsp.body().streambuf().in_avail());
}
} // SUITE(response_extract_tests)
}}}}
|