11e934351Sopenharmony_ci/* 21e934351Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 31e934351Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41e934351Sopenharmony_ci * you may not use this file except in compliance with the License. 51e934351Sopenharmony_ci * You may obtain a copy of the License at 61e934351Sopenharmony_ci * 71e934351Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81e934351Sopenharmony_ci * 91e934351Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101e934351Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111e934351Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121e934351Sopenharmony_ci * See the License for the specific language governing permissions and 131e934351Sopenharmony_ci * limitations under the License. 141e934351Sopenharmony_ci */ 151e934351Sopenharmony_ci 161e934351Sopenharmony_ci#include <iostream> 171e934351Sopenharmony_ci#include <cstring> 181e934351Sopenharmony_ci#include "gtest/gtest.h" 191e934351Sopenharmony_ci#include "gmock/gmock.h" 201e934351Sopenharmony_ci#include "http_client_constant.h" 211e934351Sopenharmony_ci#include "netstack_log.h" 221e934351Sopenharmony_ci#include "netstack_common_utils.h" 231e934351Sopenharmony_ci 241e934351Sopenharmony_ci#define private public 251e934351Sopenharmony_ci#include "http_client_task.h" 261e934351Sopenharmony_ci#include "http_client.h" 271e934351Sopenharmony_ci#include "http_client_error.h" 281e934351Sopenharmony_ci#include <curl/curl.h> 291e934351Sopenharmony_ci#include "http_client_request.h" 301e934351Sopenharmony_ci#include "http_client_response.h" 311e934351Sopenharmony_ci#if HAS_NETMANAGER_BASE 321e934351Sopenharmony_ci#include "net_conn_client.h" 331e934351Sopenharmony_ci#include "network_security_config.h" 341e934351Sopenharmony_ci#endif 351e934351Sopenharmony_ci 361e934351Sopenharmony_ciusing namespace OHOS::NetStack::HttpClient; 371e934351Sopenharmony_ciusing namespace testing; 381e934351Sopenharmony_ciusing namespace testing::ext; 391e934351Sopenharmony_ci 401e934351Sopenharmony_ciclass HttpClientTaskTest : public testing::Test { 411e934351Sopenharmony_cipublic: 421e934351Sopenharmony_ci static void SetUpTestCase() {} 431e934351Sopenharmony_ci 441e934351Sopenharmony_ci static void TearDownTestCase() {} 451e934351Sopenharmony_ci 461e934351Sopenharmony_ci virtual void SetUp() {} 471e934351Sopenharmony_ci 481e934351Sopenharmony_ci virtual void TearDown() {} 491e934351Sopenharmony_ci 501e934351Sopenharmony_ci void ProcesslTaskCb(std::shared_ptr<HttpClientTask> task); 511e934351Sopenharmony_ci}; 521e934351Sopenharmony_ci 531e934351Sopenharmony_cinamespace { 541e934351Sopenharmony_ciusing namespace std; 551e934351Sopenharmony_ciusing namespace testing::ext; 561e934351Sopenharmony_ci 571e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetHttpVersionTest001, TestSize.Level1) 581e934351Sopenharmony_ci{ 591e934351Sopenharmony_ci HttpClientRequest httpReq; 601e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 611e934351Sopenharmony_ci httpReq.SetURL(url); 621e934351Sopenharmony_ci 631e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 641e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 651e934351Sopenharmony_ci 661e934351Sopenharmony_ci uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP_NONE); 671e934351Sopenharmony_ci EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_NONE); 681e934351Sopenharmony_ci} 691e934351Sopenharmony_ci 701e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetHttpVersionTest002, TestSize.Level1) 711e934351Sopenharmony_ci{ 721e934351Sopenharmony_ci HttpClientRequest httpReq; 731e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 741e934351Sopenharmony_ci httpReq.SetURL(url); 751e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 761e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 771e934351Sopenharmony_ci 781e934351Sopenharmony_ci uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP1_1); 791e934351Sopenharmony_ci EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_1_1); 801e934351Sopenharmony_ci} 811e934351Sopenharmony_ci 821e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetHttpVersionTest003, TestSize.Level1) 831e934351Sopenharmony_ci{ 841e934351Sopenharmony_ci HttpClientRequest httpReq; 851e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 861e934351Sopenharmony_ci httpReq.SetURL(url); 871e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 881e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 891e934351Sopenharmony_ci 901e934351Sopenharmony_ci uint32_t httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP2); 911e934351Sopenharmony_ci EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_2_0); 921e934351Sopenharmony_ci httpVersionTest = task->GetHttpVersion(HttpProtocol::HTTP3); 931e934351Sopenharmony_ci EXPECT_EQ(httpVersionTest, CURL_HTTP_VERSION_3); 941e934351Sopenharmony_ci} 951e934351Sopenharmony_ci 961e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest001, TestSize.Level1) 971e934351Sopenharmony_ci{ 981e934351Sopenharmony_ci HttpClientRequest httpReq; 991e934351Sopenharmony_ci 1001e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 1011e934351Sopenharmony_ci httpReq.SetURL(url); 1021e934351Sopenharmony_ci httpReq.SetHttpProxyType(NOT_USE); 1031e934351Sopenharmony_ci HttpProxy proxy; 1041e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 1051e934351Sopenharmony_ci proxy.port = 8888; 1061e934351Sopenharmony_ci proxy.exclusions = "www.httpbin.org"; 1071e934351Sopenharmony_ci proxy.tunnel = false; 1081e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 1091e934351Sopenharmony_ci 1101e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 1111e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 1121e934351Sopenharmony_ci 1131e934351Sopenharmony_ci bool result = task->SetOtherCurlOption(task->curlHandle_); 1141e934351Sopenharmony_ci EXPECT_TRUE(result); 1151e934351Sopenharmony_ci} 1161e934351Sopenharmony_ci 1171e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest002, TestSize.Level1) 1181e934351Sopenharmony_ci{ 1191e934351Sopenharmony_ci HttpClientRequest httpReq; 1201e934351Sopenharmony_ci 1211e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 1221e934351Sopenharmony_ci httpReq.SetURL(url); 1231e934351Sopenharmony_ci httpReq.SetHttpProxyType(NOT_USE); 1241e934351Sopenharmony_ci HttpProxy proxy; 1251e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 1261e934351Sopenharmony_ci proxy.port = 8888; 1271e934351Sopenharmony_ci proxy.tunnel = false; 1281e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 1291e934351Sopenharmony_ci 1301e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 1311e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 1321e934351Sopenharmony_ci 1331e934351Sopenharmony_ci EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_)); 1341e934351Sopenharmony_ci} 1351e934351Sopenharmony_ci 1361e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest003, TestSize.Level1) 1371e934351Sopenharmony_ci{ 1381e934351Sopenharmony_ci HttpClientRequest httpReq; 1391e934351Sopenharmony_ci 1401e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 1411e934351Sopenharmony_ci httpReq.SetURL(url); 1421e934351Sopenharmony_ci httpReq.SetHttpProxyType(NOT_USE); 1431e934351Sopenharmony_ci HttpProxy proxy; 1441e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 1451e934351Sopenharmony_ci proxy.port = 8888; 1461e934351Sopenharmony_ci proxy.tunnel = false; 1471e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 1481e934351Sopenharmony_ci 1491e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 1501e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 1511e934351Sopenharmony_ci 1521e934351Sopenharmony_ci EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_)); 1531e934351Sopenharmony_ci} 1541e934351Sopenharmony_ci 1551e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest004, TestSize.Level1) 1561e934351Sopenharmony_ci{ 1571e934351Sopenharmony_ci HttpClientRequest httpReq; 1581e934351Sopenharmony_ci 1591e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 1601e934351Sopenharmony_ci httpReq.SetURL(url); 1611e934351Sopenharmony_ci httpReq.SetHttpProxyType(NOT_USE); 1621e934351Sopenharmony_ci HttpProxy proxy; 1631e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 1641e934351Sopenharmony_ci proxy.port = 8888; 1651e934351Sopenharmony_ci proxy.exclusions = "www.httpbin.org"; 1661e934351Sopenharmony_ci proxy.tunnel = true; 1671e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 1681e934351Sopenharmony_ci 1691e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 1701e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 1711e934351Sopenharmony_ci 1721e934351Sopenharmony_ci EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_)); 1731e934351Sopenharmony_ci curl_easy_cleanup(task->curlHandle_); 1741e934351Sopenharmony_ci task->curlHandle_ = nullptr; 1751e934351Sopenharmony_ci} 1761e934351Sopenharmony_ci 1771e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetOtherCurlOptionTest005, TestSize.Level1) 1781e934351Sopenharmony_ci{ 1791e934351Sopenharmony_ci HttpClientRequest httpReq; 1801e934351Sopenharmony_ci 1811e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 1821e934351Sopenharmony_ci httpReq.SetURL(url); 1831e934351Sopenharmony_ci httpReq.SetHttpProxyType(USE_SPECIFIED); 1841e934351Sopenharmony_ci HttpProxy proxy; 1851e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 1861e934351Sopenharmony_ci proxy.port = 8888; 1871e934351Sopenharmony_ci proxy.exclusions = "www.test.org"; 1881e934351Sopenharmony_ci proxy.tunnel = true; 1891e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 1901e934351Sopenharmony_ci 1911e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 1921e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 1931e934351Sopenharmony_ci EXPECT_TRUE(task->SetOtherCurlOption(task->curlHandle_)); 1941e934351Sopenharmony_ci curl_easy_cleanup(task->curlHandle_); 1951e934351Sopenharmony_ci task->curlHandle_ = nullptr; 1961e934351Sopenharmony_ci} 1971e934351Sopenharmony_ci 1981e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetUploadOptionsTest001, TestSize.Level1) 1991e934351Sopenharmony_ci{ 2001e934351Sopenharmony_ci HttpClientRequest httpReq; 2011e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/put"; 2021e934351Sopenharmony_ci httpReq.SetURL(url); 2031e934351Sopenharmony_ci std::string method = "PUT"; 2041e934351Sopenharmony_ci httpReq.SetMethod(method); 2051e934351Sopenharmony_ci 2061e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2071e934351Sopenharmony_ci std::string filePath = "/bin/who"; 2081e934351Sopenharmony_ci auto task = session.CreateTask(httpReq, UPLOAD, filePath); 2091e934351Sopenharmony_ci 2101e934351Sopenharmony_ci EXPECT_TRUE(task->SetUploadOptions(task->curlHandle_)); 2111e934351Sopenharmony_ci} 2121e934351Sopenharmony_ci 2131e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetUploadOptionsTest002, TestSize.Level1) 2141e934351Sopenharmony_ci{ 2151e934351Sopenharmony_ci HttpClientRequest httpReq; 2161e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/put"; 2171e934351Sopenharmony_ci httpReq.SetURL(url); 2181e934351Sopenharmony_ci std::string method = "PUT"; 2191e934351Sopenharmony_ci httpReq.SetMethod(method); 2201e934351Sopenharmony_ci 2211e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2221e934351Sopenharmony_ci std::string filePath = ""; 2231e934351Sopenharmony_ci auto task = session.CreateTask(httpReq, UPLOAD, filePath); 2241e934351Sopenharmony_ci 2251e934351Sopenharmony_ci EXPECT_FALSE(task->SetUploadOptions(task->curlHandle_)); 2261e934351Sopenharmony_ci} 2271e934351Sopenharmony_ci 2281e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetUploadOptionsTest003, TestSize.Level1) 2291e934351Sopenharmony_ci{ 2301e934351Sopenharmony_ci HttpClientRequest httpReq; 2311e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/put"; 2321e934351Sopenharmony_ci httpReq.SetURL(url); 2331e934351Sopenharmony_ci std::string method = "PUT"; 2341e934351Sopenharmony_ci httpReq.SetMethod(method); 2351e934351Sopenharmony_ci 2361e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2371e934351Sopenharmony_ci std::string filePath = "unavailable"; 2381e934351Sopenharmony_ci auto task = session.CreateTask(httpReq, UPLOAD, filePath); 2391e934351Sopenharmony_ci 2401e934351Sopenharmony_ci EXPECT_FALSE(task->SetUploadOptions(task->curlHandle_)); 2411e934351Sopenharmony_ci} 2421e934351Sopenharmony_ci 2431e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest001, TestSize.Level1) 2441e934351Sopenharmony_ci{ 2451e934351Sopenharmony_ci HttpClientRequest httpReq; 2461e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 2471e934351Sopenharmony_ci httpReq.SetURL(url); 2481e934351Sopenharmony_ci 2491e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2501e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 2511e934351Sopenharmony_ci 2521e934351Sopenharmony_ci EXPECT_TRUE(task->SetCurlOptions()); 2531e934351Sopenharmony_ci} 2541e934351Sopenharmony_ci 2551e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest002, TestSize.Level1) 2561e934351Sopenharmony_ci{ 2571e934351Sopenharmony_ci HttpClientRequest httpReq; 2581e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 2591e934351Sopenharmony_ci httpReq.SetURL(url); 2601e934351Sopenharmony_ci 2611e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2621e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 2631e934351Sopenharmony_ci 2641e934351Sopenharmony_ci task->request_.SetMethod(HttpConstant::HTTP_METHOD_HEAD); 2651e934351Sopenharmony_ci 2661e934351Sopenharmony_ci EXPECT_TRUE(task->SetCurlOptions()); 2671e934351Sopenharmony_ci} 2681e934351Sopenharmony_ci 2691e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest003, TestSize.Level1) 2701e934351Sopenharmony_ci{ 2711e934351Sopenharmony_ci HttpClientRequest httpReq; 2721e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/put"; 2731e934351Sopenharmony_ci httpReq.SetURL(url); 2741e934351Sopenharmony_ci std::string method = "PUT"; 2751e934351Sopenharmony_ci httpReq.SetMethod(method); 2761e934351Sopenharmony_ci 2771e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2781e934351Sopenharmony_ci std::string filePath = "/bin/who"; 2791e934351Sopenharmony_ci auto task = session.CreateTask(httpReq, UPLOAD, filePath); 2801e934351Sopenharmony_ci 2811e934351Sopenharmony_ci task->curlHandle_ = nullptr; 2821e934351Sopenharmony_ci EXPECT_FALSE(task->SetCurlOptions()); 2831e934351Sopenharmony_ci} 2841e934351Sopenharmony_ci 2851e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest004, TestSize.Level1) 2861e934351Sopenharmony_ci{ 2871e934351Sopenharmony_ci HttpClientRequest httpReq; 2881e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 2891e934351Sopenharmony_ci httpReq.SetURL(url); 2901e934351Sopenharmony_ci 2911e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 2921e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 2931e934351Sopenharmony_ci 2941e934351Sopenharmony_ci task->request_.SetMethod(HttpConstant::HTTP_METHOD_POST); 2951e934351Sopenharmony_ci 2961e934351Sopenharmony_ci EXPECT_TRUE(task->SetCurlOptions()); 2971e934351Sopenharmony_ci} 2981e934351Sopenharmony_ci 2991e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest005, TestSize.Level1) 3001e934351Sopenharmony_ci{ 3011e934351Sopenharmony_ci HttpClientRequest httpReq; 3021e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 3031e934351Sopenharmony_ci httpReq.SetURL(url); 3041e934351Sopenharmony_ci 3051e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3061e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3071e934351Sopenharmony_ci 3081e934351Sopenharmony_ci task->curlHandle_ = nullptr; 3091e934351Sopenharmony_ci 3101e934351Sopenharmony_ci EXPECT_FALSE(task->SetCurlOptions()); 3111e934351Sopenharmony_ci} 3121e934351Sopenharmony_ci 3131e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetCurlOptionsTest006, TestSize.Level1) 3141e934351Sopenharmony_ci{ 3151e934351Sopenharmony_ci HttpClientRequest httpReq; 3161e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 3171e934351Sopenharmony_ci httpReq.SetURL(url); 3181e934351Sopenharmony_ci 3191e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3201e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3211e934351Sopenharmony_ci 3221e934351Sopenharmony_ci task->curlHandle_ = nullptr; 3231e934351Sopenharmony_ci std::string headerStr = "Connection:keep-alive"; 3241e934351Sopenharmony_ci task->curlHeaderList_ = curl_slist_append(task->curlHeaderList_, headerStr.c_str()); 3251e934351Sopenharmony_ci 3261e934351Sopenharmony_ci EXPECT_FALSE(task->SetCurlOptions()); 3271e934351Sopenharmony_ci} 3281e934351Sopenharmony_ci 3291e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetType001, TestSize.Level1) 3301e934351Sopenharmony_ci{ 3311e934351Sopenharmony_ci HttpClientRequest httpReq; 3321e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 3331e934351Sopenharmony_ci httpReq.SetURL(url); 3341e934351Sopenharmony_ci 3351e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3361e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3371e934351Sopenharmony_ci 3381e934351Sopenharmony_ci EXPECT_EQ(TaskType::DEFAULT, task->GetType()); 3391e934351Sopenharmony_ci} 3401e934351Sopenharmony_ci 3411e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetFilePathTest001, TestSize.Level1) 3421e934351Sopenharmony_ci{ 3431e934351Sopenharmony_ci HttpClientRequest httpReq; 3441e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/put"; 3451e934351Sopenharmony_ci httpReq.SetURL(url); 3461e934351Sopenharmony_ci std::string method = "PUT"; 3471e934351Sopenharmony_ci httpReq.SetMethod(method); 3481e934351Sopenharmony_ci 3491e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3501e934351Sopenharmony_ci std::string filePath = "/bin/who"; 3511e934351Sopenharmony_ci auto task = session.CreateTask(httpReq, UPLOAD, filePath); 3521e934351Sopenharmony_ci 3531e934351Sopenharmony_ci EXPECT_EQ(task->GetFilePath(), "/bin/who"); 3541e934351Sopenharmony_ci} 3551e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetTaskIdTest001, TestSize.Level1) 3561e934351Sopenharmony_ci{ 3571e934351Sopenharmony_ci HttpClientRequest httpReq; 3581e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 3591e934351Sopenharmony_ci httpReq.SetURL(url); 3601e934351Sopenharmony_ci 3611e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3621e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3631e934351Sopenharmony_ci 3641e934351Sopenharmony_ci unsigned int taskId = task->GetTaskId(); 3651e934351Sopenharmony_ci EXPECT_TRUE(taskId >= 0); 3661e934351Sopenharmony_ci} 3671e934351Sopenharmony_ci 3681e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, OnSuccessTest001, TestSize.Level1) 3691e934351Sopenharmony_ci{ 3701e934351Sopenharmony_ci HttpClientRequest httpReq; 3711e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 3721e934351Sopenharmony_ci httpReq.SetURL(url); 3731e934351Sopenharmony_ci 3741e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3751e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3761e934351Sopenharmony_ci task->OnSuccess([task](const HttpClientRequest &request, const HttpClientResponse &response) {}); 3771e934351Sopenharmony_ci 3781e934351Sopenharmony_ci EXPECT_TRUE(task->onSucceeded_ != nullptr); 3791e934351Sopenharmony_ci} 3801e934351Sopenharmony_ci 3811e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, OnCancelTest001, TestSize.Level1) 3821e934351Sopenharmony_ci{ 3831e934351Sopenharmony_ci HttpClientRequest httpReq; 3841e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 3851e934351Sopenharmony_ci httpReq.SetURL(url); 3861e934351Sopenharmony_ci 3871e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 3881e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 3891e934351Sopenharmony_ci 3901e934351Sopenharmony_ci task->OnCancel([](const HttpClientRequest &request, const HttpClientResponse &response) {}); 3911e934351Sopenharmony_ci 3921e934351Sopenharmony_ci EXPECT_TRUE(task->onCanceled_ != nullptr); 3931e934351Sopenharmony_ci} 3941e934351Sopenharmony_ci 3951e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, OnFailTest001, TestSize.Level1) 3961e934351Sopenharmony_ci{ 3971e934351Sopenharmony_ci HttpClientRequest httpReq; 3981e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 3991e934351Sopenharmony_ci httpReq.SetURL(url); 4001e934351Sopenharmony_ci 4011e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4021e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4031e934351Sopenharmony_ci 4041e934351Sopenharmony_ci task->OnFail( 4051e934351Sopenharmony_ci [](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {}); 4061e934351Sopenharmony_ci 4071e934351Sopenharmony_ci EXPECT_TRUE(task->onFailed_ != nullptr); 4081e934351Sopenharmony_ci} 4091e934351Sopenharmony_ci 4101e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, OnDataReceiveTest001, TestSize.Level1) 4111e934351Sopenharmony_ci{ 4121e934351Sopenharmony_ci HttpClientRequest httpReq; 4131e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 4141e934351Sopenharmony_ci httpReq.SetURL(url); 4151e934351Sopenharmony_ci 4161e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4171e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4181e934351Sopenharmony_ci 4191e934351Sopenharmony_ci task->OnDataReceive([](const HttpClientRequest &request, const uint8_t *data, size_t length) {}); 4201e934351Sopenharmony_ci 4211e934351Sopenharmony_ci EXPECT_TRUE(task->onDataReceive_ != nullptr); 4221e934351Sopenharmony_ci} 4231e934351Sopenharmony_ci 4241e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, OnProgressTest001, TestSize.Level1) 4251e934351Sopenharmony_ci{ 4261e934351Sopenharmony_ci HttpClientRequest httpReq; 4271e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 4281e934351Sopenharmony_ci httpReq.SetURL(url); 4291e934351Sopenharmony_ci 4301e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4311e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4321e934351Sopenharmony_ci 4331e934351Sopenharmony_ci task->OnProgress( 4341e934351Sopenharmony_ci [](const HttpClientRequest &request, u_long dltotal, u_long dlnow, u_long ultotal, u_long ulnow) {}); 4351e934351Sopenharmony_ci 4361e934351Sopenharmony_ci EXPECT_TRUE(task->onProgress_ != nullptr); 4371e934351Sopenharmony_ci} 4381e934351Sopenharmony_ci 4391e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest001, TestSize.Level1) 4401e934351Sopenharmony_ci{ 4411e934351Sopenharmony_ci HttpClientRequest httpReq; 4421e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 4431e934351Sopenharmony_ci const char *data = "https://www.baidu.com"; 4441e934351Sopenharmony_ci httpReq.SetURL(url); 4451e934351Sopenharmony_ci 4461e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4471e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4481e934351Sopenharmony_ci 4491e934351Sopenharmony_ci auto *userData = task.get(); 4501e934351Sopenharmony_ci size_t size = 10; 4511e934351Sopenharmony_ci size_t memBytes = 1; 4521e934351Sopenharmony_ci 4531e934351Sopenharmony_ci task->OnDataReceive([](const HttpClientRequest &request, const uint8_t *data, size_t length) {}); 4541e934351Sopenharmony_ci size_t result = task->DataReceiveCallback(data, size, memBytes, userData); 4551e934351Sopenharmony_ci 4561e934351Sopenharmony_ci EXPECT_EQ(result, size * memBytes); 4571e934351Sopenharmony_ci} 4581e934351Sopenharmony_ci 4591e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest002, TestSize.Level1) 4601e934351Sopenharmony_ci{ 4611e934351Sopenharmony_ci HttpClientRequest httpReq; 4621e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 4631e934351Sopenharmony_ci const char *data = "https://www.baidu.com"; 4641e934351Sopenharmony_ci httpReq.SetURL(url); 4651e934351Sopenharmony_ci 4661e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4671e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4681e934351Sopenharmony_ci 4691e934351Sopenharmony_ci auto *userData = task.get(); 4701e934351Sopenharmony_ci size_t size = 10; 4711e934351Sopenharmony_ci size_t memBytes = 1; 4721e934351Sopenharmony_ci size_t result = task->DataReceiveCallback(data, size, memBytes, userData); 4731e934351Sopenharmony_ci 4741e934351Sopenharmony_ci EXPECT_EQ(result, size * memBytes); 4751e934351Sopenharmony_ci} 4761e934351Sopenharmony_ci 4771e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, DataReceiveCallbackTest003, TestSize.Level1) 4781e934351Sopenharmony_ci{ 4791e934351Sopenharmony_ci HttpClientRequest httpReq; 4801e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 4811e934351Sopenharmony_ci const char *data = "http://www.httpbin.org/get"; 4821e934351Sopenharmony_ci httpReq.SetURL(url); 4831e934351Sopenharmony_ci 4841e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 4851e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 4861e934351Sopenharmony_ci 4871e934351Sopenharmony_ci auto *userData = task.get(); 4881e934351Sopenharmony_ci size_t size = 10; 4891e934351Sopenharmony_ci size_t memBytes = 1; 4901e934351Sopenharmony_ci task->canceled_ = true; 4911e934351Sopenharmony_ci size_t result = task->DataReceiveCallback(data, size, memBytes, userData); 4921e934351Sopenharmony_ci 4931e934351Sopenharmony_ci EXPECT_EQ(result, 0); 4941e934351Sopenharmony_ci task->canceled_ = false; 4951e934351Sopenharmony_ci} 4961e934351Sopenharmony_ci 4971e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProgressCallbackTest001, TestSize.Level1) 4981e934351Sopenharmony_ci{ 4991e934351Sopenharmony_ci HttpClientRequest httpReq; 5001e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 5011e934351Sopenharmony_ci httpReq.SetURL(url); 5021e934351Sopenharmony_ci 5031e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5041e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 5051e934351Sopenharmony_ci 5061e934351Sopenharmony_ci auto *userData = task.get(); 5071e934351Sopenharmony_ci curl_off_t dltotal = 100; 5081e934351Sopenharmony_ci curl_off_t dlnow = 50; 5091e934351Sopenharmony_ci curl_off_t ultotal = 200; 5101e934351Sopenharmony_ci curl_off_t ulnow = 100; 5111e934351Sopenharmony_ci int result; 5121e934351Sopenharmony_ci 5131e934351Sopenharmony_ci result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow); 5141e934351Sopenharmony_ci EXPECT_EQ(result, 0); 5151e934351Sopenharmony_ci} 5161e934351Sopenharmony_ci 5171e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProgressCallbackTest002, TestSize.Level1) 5181e934351Sopenharmony_ci{ 5191e934351Sopenharmony_ci HttpClientRequest httpReq; 5201e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 5211e934351Sopenharmony_ci httpReq.SetURL(url); 5221e934351Sopenharmony_ci 5231e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5241e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 5251e934351Sopenharmony_ci 5261e934351Sopenharmony_ci auto *userData = task.get(); 5271e934351Sopenharmony_ci curl_off_t dltotal = 100; 5281e934351Sopenharmony_ci curl_off_t dlnow = 50; 5291e934351Sopenharmony_ci curl_off_t ultotal = 200; 5301e934351Sopenharmony_ci curl_off_t ulnow = 100; 5311e934351Sopenharmony_ci int result; 5321e934351Sopenharmony_ci 5331e934351Sopenharmony_ci task->Cancel(); 5341e934351Sopenharmony_ci result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow); 5351e934351Sopenharmony_ci EXPECT_EQ(result, CURLE_ABORTED_BY_CALLBACK); 5361e934351Sopenharmony_ci} 5371e934351Sopenharmony_ci 5381e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProgressCallbackTest003, TestSize.Level1) 5391e934351Sopenharmony_ci{ 5401e934351Sopenharmony_ci HttpClientRequest httpReq; 5411e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 5421e934351Sopenharmony_ci httpReq.SetURL(url); 5431e934351Sopenharmony_ci 5441e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5451e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 5461e934351Sopenharmony_ci 5471e934351Sopenharmony_ci auto *userData = task.get(); 5481e934351Sopenharmony_ci curl_off_t dltotal = 100; 5491e934351Sopenharmony_ci curl_off_t dlnow = 50; 5501e934351Sopenharmony_ci curl_off_t ultotal = 200; 5511e934351Sopenharmony_ci curl_off_t ulnow = 100; 5521e934351Sopenharmony_ci 5531e934351Sopenharmony_ci int result = task->ProgressCallback(userData, dltotal, dlnow, ultotal, ulnow); 5541e934351Sopenharmony_ci EXPECT_EQ(result, 0); 5551e934351Sopenharmony_ci} 5561e934351Sopenharmony_ci 5571e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest001, TestSize.Level1) 5581e934351Sopenharmony_ci{ 5591e934351Sopenharmony_ci HttpClientRequest httpReq; 5601e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 5611e934351Sopenharmony_ci httpReq.SetURL(url); 5621e934351Sopenharmony_ci 5631e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5641e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 5651e934351Sopenharmony_ci 5661e934351Sopenharmony_ci auto *userData = task.get(); 5671e934351Sopenharmony_ci const char *data = "Test Header"; 5681e934351Sopenharmony_ci size_t size = 5; 5691e934351Sopenharmony_ci size_t memBytes = 2; 5701e934351Sopenharmony_ci size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData); 5711e934351Sopenharmony_ci EXPECT_EQ(result, size * memBytes); 5721e934351Sopenharmony_ci} 5731e934351Sopenharmony_ci 5741e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest002, TestSize.Level1) 5751e934351Sopenharmony_ci{ 5761e934351Sopenharmony_ci HttpClientRequest httpReq; 5771e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 5781e934351Sopenharmony_ci httpReq.SetURL(url); 5791e934351Sopenharmony_ci 5801e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5811e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 5821e934351Sopenharmony_ci 5831e934351Sopenharmony_ci auto *userData = task.get(); 5841e934351Sopenharmony_ci 5851e934351Sopenharmony_ci const char *data = "Test Header"; 5861e934351Sopenharmony_ci size_t size = HttpConstant::MAX_DATA_LIMIT + 1; 5871e934351Sopenharmony_ci size_t memBytes = 1; 5881e934351Sopenharmony_ci size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData); 5891e934351Sopenharmony_ci EXPECT_EQ(result, 0); 5901e934351Sopenharmony_ci} 5911e934351Sopenharmony_ci 5921e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, HeaderReceiveCallbackTest003, TestSize.Level1) 5931e934351Sopenharmony_ci{ 5941e934351Sopenharmony_ci HttpClientRequest httpReq; 5951e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 5961e934351Sopenharmony_ci httpReq.SetURL(url); 5971e934351Sopenharmony_ci 5981e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 5991e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 6001e934351Sopenharmony_ci 6011e934351Sopenharmony_ci auto *userData = task.get(); 6021e934351Sopenharmony_ci 6031e934351Sopenharmony_ci const char *data = "Test Header"; 6041e934351Sopenharmony_ci size_t size = 5; 6051e934351Sopenharmony_ci size_t memBytes = 2; 6061e934351Sopenharmony_ci size_t result = task->HeaderReceiveCallback(data, size, memBytes, userData); 6071e934351Sopenharmony_ci EXPECT_EQ(result, size * memBytes); 6081e934351Sopenharmony_ci} 6091e934351Sopenharmony_ci 6101e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProcessResponseCodeTest001, TestSize.Level1) 6111e934351Sopenharmony_ci{ 6121e934351Sopenharmony_ci HttpClientRequest httpReq; 6131e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 6141e934351Sopenharmony_ci httpReq.SetURL(url); 6151e934351Sopenharmony_ci 6161e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 6171e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 6181e934351Sopenharmony_ci task->Start(); 6191e934351Sopenharmony_ci 6201e934351Sopenharmony_ci while (task->GetStatus() != TaskStatus::IDLE) { 6211e934351Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(100)); 6221e934351Sopenharmony_ci } 6231e934351Sopenharmony_ci EXPECT_TRUE(task->ProcessResponseCode()); 6241e934351Sopenharmony_ci} 6251e934351Sopenharmony_ci 6261e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProcessResponseTest001, TestSize.Level1) 6271e934351Sopenharmony_ci{ 6281e934351Sopenharmony_ci HttpClientRequest httpReq; 6291e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 6301e934351Sopenharmony_ci httpReq.SetURL(url); 6311e934351Sopenharmony_ci 6321e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 6331e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 6341e934351Sopenharmony_ci 6351e934351Sopenharmony_ci CURLMsg msg; 6361e934351Sopenharmony_ci msg.data.result = CURLE_ABORTED_BY_CALLBACK; 6371e934351Sopenharmony_ci task->OnCancel([](const HttpClientRequest &request, const HttpClientResponse &response) {}); 6381e934351Sopenharmony_ci task->ProcessResponse(&msg); 6391e934351Sopenharmony_ci EXPECT_TRUE(task->onCanceled_); 6401e934351Sopenharmony_ci 6411e934351Sopenharmony_ci msg.data.result = CURLE_FAILED_INIT; 6421e934351Sopenharmony_ci task->OnFail( 6431e934351Sopenharmony_ci [](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {}); 6441e934351Sopenharmony_ci task->ProcessResponse(&msg); 6451e934351Sopenharmony_ci EXPECT_TRUE(task->onFailed_); 6461e934351Sopenharmony_ci 6471e934351Sopenharmony_ci msg.data.result = CURLE_OK; 6481e934351Sopenharmony_ci task->response_.SetResponseCode(ResponseCode::NOT_MODIFIED); 6491e934351Sopenharmony_ci task->OnSuccess([task](const HttpClientRequest &request, const HttpClientResponse &response) {}); 6501e934351Sopenharmony_ci task->ProcessResponse(&msg); 6511e934351Sopenharmony_ci EXPECT_TRUE(task->onSucceeded_); 6521e934351Sopenharmony_ci 6531e934351Sopenharmony_ci task->curlHandle_ = nullptr; 6541e934351Sopenharmony_ci task->OnFail( 6551e934351Sopenharmony_ci [](const HttpClientRequest &request, const HttpClientResponse &response, const HttpClientError &error) {}); 6561e934351Sopenharmony_ci task->ProcessResponse(&msg); 6571e934351Sopenharmony_ci EXPECT_TRUE(task->onFailed_); 6581e934351Sopenharmony_ci} 6591e934351Sopenharmony_ci 6601e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetResponseTest001, TestSize.Level1) 6611e934351Sopenharmony_ci{ 6621e934351Sopenharmony_ci HttpClientRequest httpReq; 6631e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 6641e934351Sopenharmony_ci httpReq.SetURL(url); 6651e934351Sopenharmony_ci 6661e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 6671e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 6681e934351Sopenharmony_ci 6691e934351Sopenharmony_ci HttpClientResponse resp; 6701e934351Sopenharmony_ci resp.result_ = "result1"; 6711e934351Sopenharmony_ci task->SetResponse(resp); 6721e934351Sopenharmony_ci 6731e934351Sopenharmony_ci EXPECT_EQ(task->response_.result_, "result1"); 6741e934351Sopenharmony_ci} 6751e934351Sopenharmony_ci 6761e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetHttpProxyInfoTest001, TestSize.Level1) 6771e934351Sopenharmony_ci{ 6781e934351Sopenharmony_ci HttpClientRequest httpReq; 6791e934351Sopenharmony_ci 6801e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 6811e934351Sopenharmony_ci httpReq.SetURL(url); 6821e934351Sopenharmony_ci httpReq.SetHttpProxyType(USE_SPECIFIED); 6831e934351Sopenharmony_ci HttpProxy proxy; 6841e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 6851e934351Sopenharmony_ci proxy.port = 8888; 6861e934351Sopenharmony_ci proxy.exclusions = "www.httpbin.org"; 6871e934351Sopenharmony_ci proxy.tunnel = false; 6881e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 6891e934351Sopenharmony_ci 6901e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 6911e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 6921e934351Sopenharmony_ci 6931e934351Sopenharmony_ci std::string host; 6941e934351Sopenharmony_ci std::string exclusions; 6951e934351Sopenharmony_ci int32_t port = 0; 6961e934351Sopenharmony_ci bool tunnel = false; 6971e934351Sopenharmony_ci task->GetHttpProxyInfo(host, port, exclusions, tunnel); 6981e934351Sopenharmony_ci 6991e934351Sopenharmony_ci EXPECT_EQ(host, "192.168.147.60"); 7001e934351Sopenharmony_ci EXPECT_EQ(port, 8888); 7011e934351Sopenharmony_ci EXPECT_EQ(exclusions, "www.httpbin.org"); 7021e934351Sopenharmony_ci EXPECT_FALSE(tunnel); 7031e934351Sopenharmony_ci} 7041e934351Sopenharmony_ci 7051e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetHttpProxyInfoTest002, TestSize.Level1) 7061e934351Sopenharmony_ci{ 7071e934351Sopenharmony_ci HttpClientRequest httpReq; 7081e934351Sopenharmony_ci 7091e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 7101e934351Sopenharmony_ci httpReq.SetURL(url); 7111e934351Sopenharmony_ci httpReq.SetHttpProxyType(NOT_USE); 7121e934351Sopenharmony_ci HttpProxy proxy; 7131e934351Sopenharmony_ci proxy.host = "192.168.147.60"; 7141e934351Sopenharmony_ci proxy.port = 8888; 7151e934351Sopenharmony_ci proxy.exclusions = "www.httpbin.org"; 7161e934351Sopenharmony_ci proxy.tunnel = false; 7171e934351Sopenharmony_ci httpReq.SetHttpProxy(proxy); 7181e934351Sopenharmony_ci 7191e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7201e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7211e934351Sopenharmony_ci 7221e934351Sopenharmony_ci std::string host; 7231e934351Sopenharmony_ci std::string exclusions; 7241e934351Sopenharmony_ci int32_t port = 0; 7251e934351Sopenharmony_ci bool tunnel = false; 7261e934351Sopenharmony_ci task->GetHttpProxyInfo(host, port, exclusions, tunnel); 7271e934351Sopenharmony_ci 7281e934351Sopenharmony_ci EXPECT_EQ(host, ""); 7291e934351Sopenharmony_ci EXPECT_EQ(port, 0); 7301e934351Sopenharmony_ci EXPECT_EQ(exclusions, ""); 7311e934351Sopenharmony_ci EXPECT_FALSE(tunnel); 7321e934351Sopenharmony_ci} 7331e934351Sopenharmony_ci 7341e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetStatus001, TestSize.Level1) 7351e934351Sopenharmony_ci{ 7361e934351Sopenharmony_ci HttpClientRequest httpReq; 7371e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 7381e934351Sopenharmony_ci httpReq.SetURL(url); 7391e934351Sopenharmony_ci 7401e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7411e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7421e934351Sopenharmony_ci 7431e934351Sopenharmony_ci task->SetStatus(RUNNING); 7441e934351Sopenharmony_ci EXPECT_EQ(RUNNING, task->GetStatus()); 7451e934351Sopenharmony_ci} 7461e934351Sopenharmony_ci 7471e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetStatusTest001, TestSize.Level1) 7481e934351Sopenharmony_ci{ 7491e934351Sopenharmony_ci HttpClientRequest httpReq; 7501e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/get"; 7511e934351Sopenharmony_ci httpReq.SetURL(url); 7521e934351Sopenharmony_ci 7531e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7541e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7551e934351Sopenharmony_ci 7561e934351Sopenharmony_ci EXPECT_EQ(IDLE, task->GetStatus()); 7571e934351Sopenharmony_ci} 7581e934351Sopenharmony_ci 7591e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, GetStatusTest002, TestSize.Level1) 7601e934351Sopenharmony_ci{ 7611e934351Sopenharmony_ci HttpClientRequest httpReq; 7621e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 7631e934351Sopenharmony_ci httpReq.SetURL(url); 7641e934351Sopenharmony_ci 7651e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7661e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7671e934351Sopenharmony_ci 7681e934351Sopenharmony_ci task->SetStatus(TaskStatus::RUNNING); 7691e934351Sopenharmony_ci 7701e934351Sopenharmony_ci EXPECT_EQ(task->GetStatus(), RUNNING); 7711e934351Sopenharmony_ci} 7721e934351Sopenharmony_ci 7731e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, StartTest001, TestSize.Level1) 7741e934351Sopenharmony_ci{ 7751e934351Sopenharmony_ci HttpClientRequest httpReq; 7761e934351Sopenharmony_ci std::string url = "http://www.baidu.com"; 7771e934351Sopenharmony_ci httpReq.SetURL(url); 7781e934351Sopenharmony_ci 7791e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7801e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7811e934351Sopenharmony_ci 7821e934351Sopenharmony_ci task->SetStatus(TaskStatus::RUNNING); 7831e934351Sopenharmony_ci bool result = task->Start(); 7841e934351Sopenharmony_ci EXPECT_FALSE(result); 7851e934351Sopenharmony_ci} 7861e934351Sopenharmony_ci 7871e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, StartTest002, TestSize.Level1) 7881e934351Sopenharmony_ci{ 7891e934351Sopenharmony_ci HttpClientRequest httpReq; 7901e934351Sopenharmony_ci std::string url = "http://www.baidu.com"; 7911e934351Sopenharmony_ci httpReq.SetURL(url); 7921e934351Sopenharmony_ci 7931e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 7941e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 7951e934351Sopenharmony_ci 7961e934351Sopenharmony_ci task->error_.SetErrorCode(HttpErrorCode::HTTP_UNSUPPORTED_PROTOCOL); 7971e934351Sopenharmony_ci bool result = task->Start(); 7981e934351Sopenharmony_ci EXPECT_FALSE(result); 7991e934351Sopenharmony_ci} 8001e934351Sopenharmony_ci 8011e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, ProcessCookieTest001, TestSize.Level1) 8021e934351Sopenharmony_ci{ 8031e934351Sopenharmony_ci HttpClientRequest httpReq; 8041e934351Sopenharmony_ci std::string url = "http://www.httpbin.org/cookies/set/name1/value1"; 8051e934351Sopenharmony_ci httpReq.SetURL(url); 8061e934351Sopenharmony_ci httpReq.SetHeader("content-type", "text/plain"); 8071e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 8081e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 8091e934351Sopenharmony_ci task->Start(); 8101e934351Sopenharmony_ci 8111e934351Sopenharmony_ci while (task->GetStatus() != TaskStatus::IDLE) { 8121e934351Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(5)); 8131e934351Sopenharmony_ci } 8141e934351Sopenharmony_ci EXPECT_EQ(task->GetResponse().GetResponseCode(), ResponseCode::OK); 8151e934351Sopenharmony_ci} 8161e934351Sopenharmony_ci 8171e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, CancelTest001, TestSize.Level1) 8181e934351Sopenharmony_ci{ 8191e934351Sopenharmony_ci HttpClientRequest httpReq; 8201e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 8211e934351Sopenharmony_ci httpReq.SetURL(url); 8221e934351Sopenharmony_ci 8231e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 8241e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 8251e934351Sopenharmony_ci 8261e934351Sopenharmony_ci task->Cancel(); 8271e934351Sopenharmony_ci EXPECT_TRUE(task->canceled_); 8281e934351Sopenharmony_ci} 8291e934351Sopenharmony_ci 8301e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetServerSSLCertOption001, TestSize.Level1) 8311e934351Sopenharmony_ci{ 8321e934351Sopenharmony_ci HttpClientRequest httpReq; 8331e934351Sopenharmony_ci std::string url = "https://www.baidu.com"; 8341e934351Sopenharmony_ci httpReq.SetURL(url); 8351e934351Sopenharmony_ci 8361e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 8371e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 8381e934351Sopenharmony_ci 8391e934351Sopenharmony_ci bool result = task->SetServerSSLCertOption(task->curlHandle_); 8401e934351Sopenharmony_ci EXPECT_TRUE(result); 8411e934351Sopenharmony_ci} 8421e934351Sopenharmony_ci 8431e934351Sopenharmony_ci#if HAS_NETMANAGER_BASE 8441e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, SetServerSSLCertOption_ShouldReturnTrue_WhenGetPinSetForHostNameReturnsZeroAndPinsIsEmpty, 8451e934351Sopenharmony_ci TestSize.Level2) 8461e934351Sopenharmony_ci{ 8471e934351Sopenharmony_ci auto configInstance = OHOS::NetManagerStandard::NetworkSecurityConfig::GetInstance(); 8481e934351Sopenharmony_ci OHOS::NetManagerStandard::DomainConfig config = {}; 8491e934351Sopenharmony_ci OHOS::NetManagerStandard::Domain domain; 8501e934351Sopenharmony_ci domain.domainName_ = "https://www.example.com"; 8511e934351Sopenharmony_ci domain.includeSubDomains_ = false; 8521e934351Sopenharmony_ci config.domains_.push_back(domain); 8531e934351Sopenharmony_ci OHOS::NetManagerStandard::Pin pin; 8541e934351Sopenharmony_ci pin.digestAlgorithm_ = "TEST"; 8551e934351Sopenharmony_ci pin.digest_ = "TEST"; 8561e934351Sopenharmony_ci config.pinSet_.pins_.push_back(pin); 8571e934351Sopenharmony_ci configInstance.domainConfigs_.push_back(config); 8581e934351Sopenharmony_ci HttpClientRequest httpReq; 8591e934351Sopenharmony_ci std::string url = "https://www.example.com"; 8601e934351Sopenharmony_ci httpReq.SetURL(url); 8611e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 8621e934351Sopenharmony_ci auto task = session.CreateTask(httpReq); 8631e934351Sopenharmony_ci EXPECT_TRUE(task->SetServerSSLCertOption(task->curlHandle_)); 8641e934351Sopenharmony_ci} 8651e934351Sopenharmony_ci#endif 8661e934351Sopenharmony_ci 8671e934351Sopenharmony_ciclass MockCurl { 8681e934351Sopenharmony_cipublic: 8691e934351Sopenharmony_ci MOCK_METHOD0(easy_init, CURL *()); 8701e934351Sopenharmony_ci}; 8711e934351Sopenharmony_ci 8721e934351Sopenharmony_ciHWTEST_F(HttpClientTaskTest, HttpClientTask_ShouldNotCreate_WhenCurlInitFails, TestSize.Level0) 8731e934351Sopenharmony_ci{ 8741e934351Sopenharmony_ci MockCurl mockCurl; 8751e934351Sopenharmony_ci ON_CALL(mockCurl, easy_init).WillByDefault(Return(nullptr)); 8761e934351Sopenharmony_ci 8771e934351Sopenharmony_ci HttpClientRequest request; 8781e934351Sopenharmony_ci HttpClientTask httpClientTask(request); 8791e934351Sopenharmony_ci ASSERT_EQ(httpClientTask.GetStatus(), IDLE); 8801e934351Sopenharmony_ci ASSERT_EQ(httpClientTask.GetType(), DEFAULT); 8811e934351Sopenharmony_ci ASSERT_EQ(httpClientTask.canceled_, false); 8821e934351Sopenharmony_ci 8831e934351Sopenharmony_ci HttpSession &session = HttpSession::GetInstance(); 8841e934351Sopenharmony_ci auto httpClientTask2 = session.CreateTask(request, UPLOAD, "testFakePath"); 8851e934351Sopenharmony_ci ASSERT_EQ(httpClientTask2->GetType(), UPLOAD); 8861e934351Sopenharmony_ci} 8871e934351Sopenharmony_ci} // namespace 888