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 #include "http_client_time.h" 27 28 using namespace OHOS::NetStack::HttpClient; 29 30 class HttpClientTest : public testing::Test { 31 public: SetUpTestCase()32 static void SetUpTestCase() {} 33 TearDownTestCase()34 static void TearDownTestCase() {} 35 SetUp()36 virtual void SetUp() {} 37 TearDown()38 virtual void TearDown() {} 39 }; 40 41 namespace { 42 using namespace std; 43 using namespace testing::ext; 44 45 HWTEST_F(HttpClientTest, AddRequestInfoTest001, TestSize.Level1) 46 { 47 HttpClientRequest httpReq; 48 std::string url = "http://www.baidu.com"; 49 httpReq.SetURL(url); 50 51 HttpSession &session = HttpSession::GetInstance(); 52 auto task = session.CreateTask(httpReq); 53 task->Start(); 54 55 while (task->GetStatus() != TaskStatus::IDLE) { 56 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 57 } 58 EXPECT_EQ(task->GetStatus(), IDLE); 59 } 60 61 HWTEST_F(HttpClientTest, GetNowTimeSecondsTest001, TestSize.Level1) 62 { 63 HttpTime httpTime; 64 time_t currentTime = httpTime.GetNowTimeSeconds(); 65 EXPECT_NE(currentTime, 0); 66 } 67 68 HWTEST_F(HttpClientTest, StrTimeToTimestampTest001, TestSize.Level1) 69 { 70 std::string validTimeStr = "Sat, 01 Jan 2000 00:00:00 GMT"; 71 HttpTime httpTime; 72 time_t expectedTimestamp = 946656000; 73 time_t expectedTimestamp2 = 946720800; 74 auto ret = httpTime.StrTimeToTimestamp(validTimeStr); 75 EXPECT_TRUE(ret == expectedTimestamp || ret == expectedTimestamp2); 76 } 77 } // namespace