|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#include <memory> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <olp/core/client/OlpClientSettingsFactory.h> |
| 24 | +#include <olp/core/http/Network.h> |
| 25 | +#include <olp/core/http/NetworkSettings.h> |
| 26 | + |
| 27 | +#include "NetworkTestBase.h" |
| 28 | +#include "ReadDefaultResponses.h" |
| 29 | + |
| 30 | +namespace { |
| 31 | +using NetworkRequest = olp::http::NetworkRequest; |
| 32 | +using NetworkResponse = olp::http::NetworkResponse; |
| 33 | +using NetworkSettings = olp::http::NetworkSettings; |
| 34 | + |
| 35 | +using DataCallbackTest = NetworkTestBase; |
| 36 | + |
| 37 | +TEST_F(DataCallbackTest, DataChunks) { |
| 38 | + const std::string kUrlBase = "https://some-url.com"; |
| 39 | + const std::string kApiBase = "/some-api"; |
| 40 | + |
| 41 | + // generate 1mb of data |
| 42 | + constexpr auto data_size = 1u * 1024u * 1024u; |
| 43 | + auto data = mockserver::ReadDefaultResponses::GenerateData(data_size); |
| 44 | + auto payload = std::make_shared<std::stringstream>(); |
| 45 | + std::vector<uint8_t> chunk_data(data_size); |
| 46 | + |
| 47 | + // we expect multiple data chunks are received in the test. |
| 48 | + int chunk_count = 0; |
| 49 | + |
| 50 | + mock_server_client_->MockResponse("GET", kApiBase, data); |
| 51 | + |
| 52 | + const auto url = kUrlBase + kApiBase; |
| 53 | + const auto request = NetworkRequest(url).WithSettings(settings_).WithVerb( |
| 54 | + olp::http::NetworkRequest::HttpVerb::GET); |
| 55 | + |
| 56 | + std::promise<NetworkResponse> promise; |
| 57 | + const auto outcome = network_->Send( |
| 58 | + request, payload, |
| 59 | + [&promise](NetworkResponse response) { |
| 60 | + promise.set_value(std::move(response)); |
| 61 | + }, |
| 62 | + nullptr, |
| 63 | + [&chunk_data, &chunk_count](const std::uint8_t* data, |
| 64 | + std::uint64_t offset, std::size_t length) { |
| 65 | + std::copy(data, data + length, &chunk_data[offset]); |
| 66 | + ++chunk_count; |
| 67 | + }); |
| 68 | + ASSERT_TRUE(outcome.IsSuccessful()); |
| 69 | + |
| 70 | + auto future = promise.get_future(); |
| 71 | + const auto response = future.get(); |
| 72 | + const auto& payload_data = payload->str(); |
| 73 | + |
| 74 | + ASSERT_EQ(response.GetStatus(), olp::http::HttpStatusCode::OK); |
| 75 | + ASSERT_GT(chunk_count, 2); |
| 76 | + EXPECT_TRUE(std::equal(data.begin(), data.end(), chunk_data.begin())); |
| 77 | + EXPECT_EQ(data, payload_data); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace |
0 commit comments