void container_test_base::check_public_access(azure::storage::blob_container_public_access_type access) { auto blob = m_container.get_block_blob_reference(_XPLATSTR("blob")); blob.upload_text(_XPLATSTR("test"), azure::storage::access_condition(), azure::storage::blob_request_options(), m_context); azure::storage::cloud_blob_container public_container(m_container.uri()); auto public_blob = public_container.get_blob_reference(blob.name()); if (access != azure::storage::blob_container_public_access_type::off) { public_blob.download_attributes(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context); } else { CHECK_THROW(public_blob.download_attributes(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context), azure::storage::storage_exception); } if (access == azure::storage::blob_container_public_access_type::container) { public_container.list_blobs_segmented(utility::string_t(), true, azure::storage::blob_listing_details::all, 0, azure::storage::continuation_token(), azure::storage::blob_request_options(), m_context); public_container.download_attributes(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context); } else { CHECK_THROW(public_container.list_blobs_segmented(utility::string_t(), true, azure::storage::blob_listing_details::all, 0, azure::storage::continuation_token(), azure::storage::blob_request_options(), m_context), azure::storage::storage_exception); CHECK_THROW(public_container.download_attributes(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context), azure::storage::storage_exception); } auto perms = m_container.download_permissions(azure::storage::access_condition(), azure::storage::blob_request_options(), m_context); CHECK(access == perms.public_access()); }
utility::string_t details::_http_request::to_string() const { utility::ostringstream_t buffer; buffer << m_method << _XPLATSTR(" ") << (this->m_uri.is_empty() ? _XPLATSTR("/") : this->m_uri.to_string()) << _XPLATSTR(" HTTP/1.1\r\n"); buffer << http_msg_base::to_string(); return buffer.str(); }
uri_builder &uri_builder::append_query(const utility::string_t &query, bool is_encode) { if(query.empty()) { return *this; } auto encoded_query = is_encode ? uri::encode_uri(query, uri::components::path) : query; auto thisQuery = this->query(); if (thisQuery.empty()) { this->set_query(encoded_query); } else if(thisQuery.back() == _XPLATSTR('&') && encoded_query.front() == _XPLATSTR('&')) { thisQuery.pop_back(); this->set_query(thisQuery + encoded_query); } else if(thisQuery.back() != _XPLATSTR('&') && encoded_query.front() != _XPLATSTR('&')) { this->set_query(thisQuery + _XPLATSTR("&") + encoded_query); } else { // Only one ampersand. this->set_query(thisQuery + encoded_query); } return *this; }
uri details::_http_request::relative_uri() const { // If the listener path is empty, then just return the request URI. if(m_listener_path.empty() || m_listener_path == _XPLATSTR("/")) { return m_uri.resource(); } utility::string_t prefix = uri::decode(m_listener_path); utility::string_t path = uri::decode(m_uri.resource().to_string()); if(path.empty()) { path = _XPLATSTR("/"); } auto pos = path.find(prefix); if (pos == 0) { return uri(uri::encode_uri(path.erase(0, prefix.length()))); } else { throw http_exception(_XPLATSTR("Error: request was not prefixed with listener uri")); } }
TEST(http_sender_get_response, headers_set) { std::string response_body{ "response body" }; auto web_request_factory = std::make_unique<test_web_request_factory>([response_body](const std::string &) -> std::unique_ptr<web_request> { auto request = new web_request_stub((unsigned short)200, "OK", response_body); request->on_get_response = [](web_request_stub& request) { auto http_headers = request.m_signalr_client_config.get_http_headers(); ASSERT_EQ(1U, http_headers.size()); ASSERT_EQ(_XPLATSTR("123"), http_headers[_XPLATSTR("abc")]); }; return std::unique_ptr<web_request>(request); }); signalr::signalr_client_config signalr_client_config; auto http_headers = signalr_client_config.get_http_headers(); http_headers[_XPLATSTR("abc")] = _XPLATSTR("123"); signalr_client_config.set_http_headers(http_headers); // ensures that web_request.get_response() was invoked ASSERT_EQ(response_body, http_sender::get(*web_request_factory, "url", signalr_client_config).get()); }
void shared_key_authentication_handler::sign_request(web::http::http_request& request, operation_context context) const { web::http::http_headers& headers = request.headers(); headers.add(ms_header_date, utility::datetime::utc_now().to_string()); if (m_credentials.is_shared_key()) { utility::string_t string_to_sign = m_canonicalizer->canonicalize(request, context); if (core::logger::instance().should_log(context, client_log_level::log_level_verbose)) { utility::string_t with_dots(string_to_sign); std::replace(with_dots.begin(), with_dots.end(), _XPLATSTR('\n'), _XPLATSTR('.')); core::logger::instance().log(context, client_log_level::log_level_verbose, _XPLATSTR("StringToSign: ") + with_dots); } utility::string_t header_value; header_value.reserve(256); header_value.append(m_canonicalizer->authentication_scheme()); header_value.append(_XPLATSTR(" ")); header_value.append(m_credentials.account_name()); header_value.append(_XPLATSTR(":")); header_value.append(calculate_hmac_sha256_hash(string_to_sign, m_credentials)); headers.add(web::http::header_names::authorization, header_value); } }
utility::string_t details::_http_response::to_string() const { // If the user didn't explicitly set a reason phrase then we should have it default // if they used one of the standard known status codes. auto reason_phrase = m_reason_phrase; if(reason_phrase.empty()) { static http_status_to_phrase idToPhraseMap[] = { #define _PHRASES #define DAT(a,b,c) {status_codes::a, c}, #include "cpprest/http_constants.dat" #undef _PHRASES #undef DAT }; for( auto iter = std::begin(idToPhraseMap); iter != std::end(idToPhraseMap); ++iter) { if( iter->id == status_code() ) { reason_phrase = iter->phrase; break; } } } utility::ostringstream_t buffer; buffer << _XPLATSTR("HTTP/1.1 ") << m_status_code << _XPLATSTR(" ") << reason_phrase << _XPLATSTR("\r\n"); buffer << http_msg_base::to_string(); return buffer.str(); }
TEST_FIXTURE(blob_test_base, blob_name_unicode) { utility::string_t blob_name(_XPLATSTR("文件1")); auto blob = m_container.get_block_blob_reference(blob_name); blob.upload_text(_XPLATSTR("test2")); CHECK(blob.exists()); CHECK(blob.name() == blob_name); }
utility::string_t get_transport_name(transport_type transport) { _ASSERTE(transport == transport_type::websockets || transport == transport_type::long_polling); return transport == transport_type::websockets ? _XPLATSTR("webSockets") : _XPLATSTR("longPolling"); }
TEST_FIXTURE(blob_test_base, directory_name_unicode) { utility::string_t dir_name(_XPLATSTR("目录1")); utility::string_t blob_name(_XPLATSTR("block_blob")); auto dir = m_container.get_directory_reference(dir_name); auto blob = dir.get_block_blob_reference(blob_name); blob.upload_text(_XPLATSTR("test")); CHECK(blob.exists()); CHECK(blob.name() == dir_name + _XPLATSTR("/") + blob_name); }
void add_cors_rule_2(std::vector<azure::storage::service_properties::cors_rule>& cors_rules) { azure::storage::service_properties::cors_rule rule; rule.allowed_headers().push_back(_XPLATSTR("x-ms-meta-ab*")); rule.allowed_origins().push_back(_XPLATSTR("*")); rule.allowed_methods().push_back(web::http::methods::HEAD); rule.exposed_headers().push_back(_XPLATSTR("x-ms-meta-abc*")); rule.set_max_age(std::chrono::seconds(25)); cors_rules.push_back(rule); }
TEST_FIXTURE(file_directory_test_base, directory_get_parent_directory_ref) { m_directory.create_if_not_exists(azure::storage::file_access_condition(), azure::storage::file_request_options(), m_context); auto directory_name = _XPLATSTR("directory"); auto directory = m_directory.get_subdirectory_reference(directory_name); auto parent_directory = directory.get_parent_directory_reference(); CHECK_UTF8_EQUAL(directory_name, directory.name()); CHECK(directory.get_parent_share_reference().is_valid()); check_equal(m_share, directory.get_parent_share_reference()); check_equal(m_directory, parent_directory); CHECK(!directory.uri().primary_uri().is_empty()); CHECK(directory.metadata().empty()); CHECK(directory.properties().etag().empty()); CHECK(!directory.properties().last_modified().is_initialized()); CHECK(!parent_directory.uri().primary_uri().is_empty()); CHECK(parent_directory.metadata().empty()); CHECK(parent_directory.properties().etag().empty()); CHECK(!parent_directory.properties().last_modified().is_initialized()); // Check if get_root_directory_refence works for root directory's sub-directory. auto root_direcotry = m_share.get_root_directory_reference(); directory_name = _XPLATSTR("directory"); directory = root_direcotry.get_subdirectory_reference(directory_name); parent_directory = directory.get_parent_directory_reference(); CHECK_UTF8_EQUAL(directory_name, directory.name()); CHECK(directory.get_parent_share_reference().is_valid()); check_equal(m_share, directory.get_parent_share_reference()); check_equal(root_direcotry, parent_directory); CHECK(!directory.uri().primary_uri().is_empty()); CHECK(directory.metadata().empty()); CHECK(directory.properties().etag().empty()); CHECK(!directory.properties().last_modified().is_initialized()); CHECK(!parent_directory.uri().primary_uri().is_empty()); CHECK(parent_directory.metadata().empty()); CHECK(parent_directory.properties().etag().empty()); CHECK(!parent_directory.properties().last_modified().is_initialized()); // Check if get_root_directory_refence works for root directory. root_direcotry = m_share.get_root_directory_reference(); directory = root_direcotry.get_parent_directory_reference(); check_equal(root_direcotry, parent_directory); }
TEST(http_sender_get_response, request_sent_using_get_method) { utility::string_t response_body{ _XPLATSTR("response body") }; auto web_request_factory = std::make_unique<test_web_request_factory>([response_body](const web::uri &) -> std::unique_ptr<web_request> { auto request = new web_request_stub((unsigned short)200, _XPLATSTR("OK"), response_body); request->on_get_response = [](web_request_stub& request) { ASSERT_EQ(_XPLATSTR("GET"), request.m_method); }; return std::unique_ptr<web_request>(request); }); ASSERT_EQ(response_body, http_sender::get(*web_request_factory, _XPLATSTR("url")).get()); }
void append_connection_data(web::uri_builder& builder, const utility::string_t& connection_data) { if (connection_data.length() > 0) { builder.append_query(_XPLATSTR("connectionData"), connection_data, /* do_encoding */ true); } }
web::uri build_connect(const web::uri& base_url, transport_type transport, const utility::string_t& query_string) { auto builder = build_uri(base_url, _XPLATSTR(""), query_string); return convert_to_websocket_url(builder, transport).to_uri(); //auto builder = build_uri(base_url, _XPLATSTR("connect"), transport, connection_data, query_string); //return convert_to_websocket_url(builder, transport).to_uri(); }
web::http::http_request update_message(const cloud_queue_message& message, std::chrono::seconds visibility_timeout, bool update_contents, web::http::uri_builder& uri_builder, const std::chrono::seconds& timeout, operation_context context) { uri_builder.append_query(core::make_query_parameter(_XPLATSTR("popreceipt"), message.pop_receipt())); uri_builder.append_query(core::make_query_parameter(_XPLATSTR("visibilitytimeout"), visibility_timeout.count(), /* do_encoding */ false)); web::http::http_request request = queue_base_request(web::http::methods::PUT, uri_builder, timeout, context); if (update_contents) { protocol::message_writer writer; std::string content = writer.write(message); request.set_body(content); } return request; }
web::http::http_request delete_message(const cloud_queue_message& message, web::http::uri_builder& uri_builder, const std::chrono::seconds& timeout, operation_context context) { uri_builder.append_query(core::make_query_parameter(_XPLATSTR("popreceipt"), message.pop_receipt())); web::http::http_request request = queue_base_request(web::http::methods::DEL, uri_builder, timeout, context); return request; }
TEST_FIXTURE(file_directory_test_base, directory_get_directory_ref) { m_directory.create_if_not_exists(azure::storage::file_access_condition(), azure::storage::file_request_options(), m_context); auto directory_name = _XPLATSTR("directory"); auto directory = m_directory.get_subdirectory_reference(directory_name); CHECK(directory.name() == directory_name); CHECK(directory.get_parent_share_reference().is_valid()); check_equal(m_share, directory.get_parent_share_reference()); check_equal(m_directory, directory.get_parent_directory_reference()); CHECK(!directory.uri().primary_uri().is_empty()); CHECK(directory.metadata().empty()); CHECK(directory.properties().etag().empty()); CHECK(!directory.properties().last_modified().is_initialized()); // In root directory directory = m_share.get_root_directory_reference().get_subdirectory_reference(directory_name); CHECK(directory.name() == directory_name); CHECK(directory.get_parent_share_reference().is_valid()); check_equal(m_share, directory.get_parent_share_reference()); check_equal(m_share.get_root_directory_reference(), directory.get_parent_directory_reference()); CHECK(!directory.uri().primary_uri().is_empty()); CHECK(directory.metadata().empty()); CHECK(directory.properties().etag().empty()); CHECK(!directory.properties().last_modified().is_initialized()); }
void append_transport(web::uri_builder &builder, transport_type transport) { if (transport > static_cast<transport_type>(-1)) { builder.append_query(_XPLATSTR("transport"), get_transport_name(transport)); } }
web::uri_builder &convert_to_websocket_url(web::uri_builder &builder, transport_type transport) { if (transport == transport_type::websockets) { if (builder.scheme() == _XPLATSTR("https")) { builder.set_scheme(utility::string_t(_XPLATSTR("wss"))); } else { builder.set_scheme(utility::string_t(_XPLATSTR("ws"))); } } return builder; }
void append_groups_token(web::uri_builder& builder, const utility::string_t& groups_token) { if (groups_token.length() > 0) { builder.append_query(_XPLATSTR("groupsToken"), groups_token, /* do_encoding */ true); } }
void append_connection_token(web::uri_builder &builder, const utility::string_t &connection_token) { if (connection_token.length() > 0) { builder.append_query(_XPLATSTR("connectionToken"), connection_token, /* do_encoding */ true); } }
void append_message_id(web::uri_builder& builder, const utility::string_t& message_id) { if (message_id.length() > 0) { builder.append_query(_XPLATSTR("messageId"), message_id, /* do_encoding */ true); } }
web::uri_builder build_uri(const web::uri& base_url, const utility::string_t& command, transport_type transport, const utility::string_t& connection_data, const utility::string_t& query_string, const utility::string_t& last_message_id = _XPLATSTR(""), const utility::string_t& groups_token = _XPLATSTR("")) { _ASSERTE(command == _XPLATSTR("reconnect") || (last_message_id.length() == 0 && groups_token.length() == 0)); web::uri_builder builder(base_url); builder.append_path(command); append_transport(builder, transport); builder.append_query(_XPLATSTR("clientProtocol"), PROTOCOL); //append_connection_token(builder, connection_token); append_connection_data(builder, connection_data); append_message_id(builder, last_message_id); append_groups_token(builder, groups_token); return builder.append_query(query_string); }
/// Test the blob name with corner characters. TEST_FIXTURE(blob_test_base, corner_blob_name) { // Initialize the chareset to generate random blob name. std::vector<utility::char_t> charset; utility::string_t characters = _XPLATSTR("`~!@#$%^&*()_+[{]}|;:\'\",<>?"); for (size_t i = 0; i < characters.size(); ++i) { charset.push_back(characters[i]); } for (int i = 0; i < 16; ++i) { utility::string_t blob_name = get_random_string(charset, 20); azure::storage::cloud_block_blob blob = m_container.get_block_blob_reference(blob_name); auto content = get_random_string(charset, 20); blob.upload_text(content); // list the container to get the blob just created. auto listing = list_all_blobs(blob_name, azure::storage::blob_listing_details::all, 0, azure::storage::blob_request_options()); CHECK(listing.size() == 1); // check the consistance of blob content. auto download_content = blob.download_text(); CHECK(content == download_content); blob.delete_blob(); } }
void add_metrics_4(azure::storage::service_properties::metrics_properties& metrics) { metrics = azure::storage::service_properties::metrics_properties(); metrics.set_version(_XPLATSTR("1.0")); metrics.set_enabled(false); metrics.set_retention_policy_enabled(false); }
void add_logging_2(azure::storage::service_properties::logging_properties& logging) { logging = azure::storage::service_properties::logging_properties(); logging.set_version(_XPLATSTR("1.0")); logging.set_write_enabled(true); logging.set_delete_enabled(true); logging.set_retention_policy_enabled(false); }
TEST_FIXTURE(queue_service_test_base, queue_name_unicode) { utility::string_t queue_name(_XPLATSTR("队列1")); azure::storage::cloud_queue_client client = get_queue_client(); azure::storage::cloud_queue queue = client.get_queue_reference(queue_name); CHECK_STORAGE_EXCEPTION(queue.exists(), error_invalid_characters_in_resource_name); CHECK_STORAGE_EXCEPTION(queue.create(), error_invalid_characters_in_resource_name); }
TEST_FIXTURE(table_service_test_base, table_name_unicode) { utility::string_t table_name(_XPLATSTR("表格1")); azure::storage::cloud_table_client client = get_table_client(); azure::storage::cloud_table table = client.get_table_reference(table_name); CHECK(false == table.exists()); CHECK_STORAGE_EXCEPTION(table.create(), error_bad_request); }
void add_logging_1(azure::storage::service_properties::logging_properties& logging) { logging = azure::storage::service_properties::logging_properties(); logging.set_version(_XPLATSTR("1.0")); logging.set_read_enabled(true); logging.set_retention_policy_enabled(true); logging.set_retention_days(20); }