1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <iostream> 17 #include <cstring> 18 #include "gtest/gtest.h" 19 #include "http_client_constant.h" 20 #include "netstack_log.h" 21 22 #define private public 23 #include "http_client.h" 24 #include "http_client_task.h" 25 #include <curl/curl.h> 26 27 using namespace OHOS::NetStack::HttpClient; 28 29 class HttpClientTest : public testing::Test { 30 public: SetUpTestCase()31 static void SetUpTestCase() {} 32 TearDownTestCase()33 static void TearDownTestCase() {} 34 SetUp()35 virtual void SetUp() {} 36 TearDown()37 virtual void TearDown() {} 38 }; 39 40 namespace { 41 using namespace std; 42 using namespace testing::ext; 43 44 HWTEST_F(HttpClientTest, AddRequestInfoTest001, TestSize.Level1) 45 { 46 HttpClientRequest httpReq; 47 std::string url = "http://www.baidu.com"; 48 httpReq.SetURL(url); 49 50 HttpSession &session = HttpSession::GetInstance(); 51 auto task = session.CreateTask(httpReq); 52 task->Start(); 53 54 while (task->GetStatus() != TaskStatus::IDLE) { 55 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 56 } 57 EXPECT_EQ(task->GetStatus(), IDLE); 58 } 59 60 HWTEST_F(HttpClientTest, IsInitedTest001, TestSize.Level1) 61 { 62 HttpSession req; 63 64 EXPECT_TRUE(req.IsInited()); 65 } 66 67 HWTEST_F(HttpClientTest, InitTest001, TestSize.Level1) 68 { 69 HttpSession req; 70 71 EXPECT_TRUE(req.Init()); 72 EXPECT_TRUE(req.initialized_); 73 } 74 75 HWTEST_F(HttpClientTest, DeinitTest001, TestSize.Level1) 76 { 77 HttpSession req; 78 79 req.Deinit(); 80 81 EXPECT_FALSE(req.initialized_); 82 EXPECT_TRUE(req.Init()); 83 } 84 85 HWTEST_F(HttpClientTest, GetTaskByIdTest001, TestSize.Level1) 86 { 87 HttpClientRequest httpReq; 88 std::string url = "https://www.baidu.com"; 89 httpReq.SetURL(url); 90 91 HttpSession &session = HttpSession::GetInstance(); 92 auto task = session.CreateTask(httpReq); 93 int32_t taskId = task->GetTaskId(); 94 session.taskIdMap_[taskId] = task; 95 96 EXPECT_NE(nullptr, session.GetTaskById(taskId)); 97 session.taskIdMap_.erase(taskId); 98 } 99 100 } // namespace