Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/functional/http/client/building_request_tests.cpp
f2a6ba12fc29
11.3 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 | /***
* ==++==
*
* 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.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* building_request_tests.cpp
*
* Tests cases manually building up HTTP requests.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#ifdef _WIN32
#include <WinError.h>
#endif
#include <locale_guard.h>
using namespace web::http;
using namespace web::http::client;
using namespace tests::functional::http::utilities;
namespace tests { namespace functional { namespace http { namespace client {
SUITE(building_request_tests)
{
TEST_FIXTURE(uri_address, simple_values)
{
test_http_server::scoped_server scoped(m_uri);
test_http_server * p_server = scoped.server();
http_client client(m_uri);
// Set a method.
const method method = methods::OPTIONS;
http_request msg(method);
VERIFY_ARE_EQUAL(method, msg.method());
// Set a path once.
const utility::string_t custom_path1 = U("/hey/custom/path");
msg.set_request_uri(custom_path1);
VERIFY_ARE_EQUAL(custom_path1, msg.relative_uri().to_string());
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, custom_path1);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// Set the path twice.
msg = http_request(method);
msg.set_request_uri(custom_path1);
VERIFY_ARE_EQUAL(custom_path1, msg.relative_uri().to_string());
const utility::string_t custom_path2 = U("/yes/you/there");
msg.set_request_uri(custom_path2);
VERIFY_ARE_EQUAL(custom_path2, msg.relative_uri().to_string());
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, custom_path2);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
TEST_FIXTURE(uri_address, body_types)
{
test_http_server::scoped_server scoped(m_uri);
test_http_server * p_server = scoped.server();
http_client client(m_uri);
// Body data types.
const method method(U("CUSTOMmethod"));
utility::string_t str_body(U("YES_BASIC_STRING BODY"));
utility::string_t str_move_body(str_body);
std::vector<unsigned char> vector_body;
vector_body.resize(str_body.size()*sizeof(utility::char_t));
memcpy(&vector_body[0], &str_body[0], str_body.size()*sizeof(utility::char_t));
std::vector<unsigned char> vector_move_body(vector_body);
utility::string_t custom_content = U("YESNOW!");
// vector - no content type.
http_request msg(method);
msg.set_body(std::move(vector_move_body));
VERIFY_ARE_EQUAL(U("application/octet-stream"), msg.headers()[U("Content-Type")]);
p_server->next_request().then([&](test_request *p_request)
{
auto received = p_request->m_body;
auto sent = vector_body;
VERIFY_IS_TRUE(received == sent);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// vector - with content type.
msg = http_request(method);
vector_move_body = vector_body;
msg.headers().add(U("Content-Type"), custom_content);
msg.set_body(std::move(vector_move_body));
VERIFY_ARE_EQUAL(custom_content, msg.headers()[U("Content-Type")]);
p_server->next_request().then([&](test_request *p_request)
{
auto received = p_request->m_body;
auto sent = vector_body;
VERIFY_IS_TRUE(received == sent);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// string - no content type.
msg = http_request(method);
msg.set_body(std::move(str_move_body));
VERIFY_ARE_EQUAL(U("text/plain; charset=utf-8"), msg.headers()[U("Content-Type")]);
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"), U("text/plain; charset=utf-8"), str_body);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// string - with content type.
msg = http_request(method);
str_move_body = str_body;
msg.headers().add(U("Content-Type"), custom_content);
msg.set_body(std::move(str_move_body));
VERIFY_ARE_EQUAL(custom_content, msg.headers()[U("Content-Type")]);
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"), custom_content, str_body);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
TEST(set_body_string_with_charset)
{
http_request request;
VERIFY_THROWS(request.set_body(
::utility::conversions::to_utf16string("body_data"),
::utility::conversions::to_utf16string("text/plain;charset=utf-16")), std::invalid_argument);
}
TEST_FIXTURE(uri_address, empty_bodies)
{
test_http_server::scoped_server scoped(m_uri);
test_http_server * p_server = scoped.server();
http_client client(m_uri);
// Body data.
std::string empty_str;
std::vector<unsigned char> vector_body;
utility::string_t str_body;
utility::string_t wstr_body;
// empty vector.
const method method(methods::PUT);
http_request msg(method);
msg.set_body(std::move(vector_body));
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"), U("application/octet-stream"));
VERIFY_ARE_EQUAL(0u, p_request->m_body.size());
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// empty string.
msg = http_request(method);
msg.set_body(std::move(str_body));
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"), U("text/plain; charset=utf-8"));
VERIFY_ARE_EQUAL(0u, p_request->m_body.size());
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// empty wstring.
msg = http_request(method);
msg.set_body(std::move(wstr_body));
p_server->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, method, U("/"), U("text/plain; charset=utf-8"));
VERIFY_ARE_EQUAL(0u, p_request->m_body.size());
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
TEST_FIXTURE(uri_address, set_body)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
const method mtd = methods::POST;
utility::string_t data(U("YOU KNOW~!!!!!"));
utility::string_t content_type = U("text/plain; charset=utf-8");
// without content type
http_request msg(mtd);
msg.set_body(data);
VERIFY_ARE_EQUAL(content_type, msg.headers()[U("Content-Type")]);
scoped.server()->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, mtd, U("/"), content_type, data);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
// with content type
content_type = U("YESYES");
#ifdef _UTF16_STRINGS
const utility::string_t expected_content_type = U("YESYES; charset=utf-8");
#else
const utility::string_t expected_content_type = U("YESYES");
#endif
msg = http_request(mtd);
msg.set_body(data, content_type);
VERIFY_ARE_EQUAL(expected_content_type, msg.headers()[U("Content-Type")]);
scoped.server()->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, mtd, U("/"), expected_content_type, data);
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
TEST_FIXTURE(uri_address, set_body_with_charset)
{
http_request msg(methods::PUT);
msg.set_body("datadatadata", "text/plain;charset=us-ascii");
VERIFY_THROWS(msg.set_body(
::utility::conversions::to_utf16string("datadatadata"),
::utility::conversions::to_utf16string("text/plain;charset=us-ascii")), std::invalid_argument);
}
TEST_FIXTURE(uri_address, set_content_length_locale, "Ignore:Android", "Locale unsupported on Android")
{
std::locale changedLocale;
try
{
#ifdef _WIN32
changedLocale = std::locale("fr-FR");
#else
changedLocale = std::locale("fr_FR.UTF-8");
#endif
}
catch (const std::exception &)
{
// Silently pass if locale isn't installed on the machine.
return;
}
tests::common::utilities::locale_guard loc(changedLocale);
http_request req(methods::PUT);
req.headers().set_content_length(1000);
VERIFY_ARE_EQUAL(U("1000"), req.headers()[web::http::header_names::content_length]); // fr_RF would have 1 000
}
TEST_FIXTURE(uri_address, set_port_locale, "Ignore:Android", "Locale unsupported on Android")
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);
utility::string_t data(U("STRING data 1000"));
scoped.server()->next_request().then([&](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, methods::PUT, U("/"), U("text/plain; charset=utf-8"), data);
p_request->reply(200);
});
{
std::locale changedLocale;
try
{
#ifdef _WIN32
changedLocale = std::locale("fr-FR");
#else
changedLocale = std::locale("fr_FR.UTF-8");
#endif
}
catch (const std::exception &)
{
// Silently pass if locale isn't installed on machine.
return;
}
tests::common::utilities::locale_guard loc(changedLocale);
http_request msg(methods::PUT);
msg.set_body(data);
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
}
TEST_FIXTURE(uri_address, reuse_request)
{
test_http_server::scoped_server scoped(m_uri);
test_http_server * p_server = scoped.server();
http_client client(m_uri);
http_request msg(methods::GET);
for (int i = 0; i < 3; ++i)
{
p_server->next_request().then([](test_request *p_request)
{
http_asserts::assert_test_request_equals(p_request, methods::GET, U("/"));
p_request->reply(200);
});
http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
}
}
}
}}}}
|