• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "netstack_log.h"
17 #include "gtest/gtest.h"
18 #include <csignal>
19 #include <cstring>
20 #include <functional>
21 #include <iostream>
22 
23 #include "websocket_client_innerapi.h"
24 
25 class WebSocketTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase() {}
28 
TearDownTestCase()29     static void TearDownTestCase() {}
30 
SetUp()31     virtual void SetUp() {}
32 
TearDown()33     virtual void TearDown() {}
34 };
35 
36 namespace {
37 using namespace testing::ext;
38 using namespace OHOS::NetStack::WebSocketClient;
39 
40 OpenOptions openOptions;
41 
42 CloseOption closeOptions;
43 
OnMessage(WebSocketClient * client,const std::string & data,size_t length)44 static void OnMessage(WebSocketClient *client, const std::string &data, size_t length) {}
45 
OnOpen(WebSocketClient * client,OpenResult openResult)46 static void OnOpen(WebSocketClient *client, OpenResult openResult) {}
47 
OnError(WebSocketClient * client,ErrorResult error)48 static void OnError(WebSocketClient *client, ErrorResult error) {}
49 
OnClose(WebSocketClient * client,CloseResult closeResult)50 static void OnClose(WebSocketClient *client, CloseResult closeResult) {}
51 
52 WebSocketClient *client = new WebSocketClient();
53 
54 HWTEST_F(WebSocketTest, WebSocketRegistcallback001, TestSize.Level1)
55 {
56     openOptions.headers["Content-Type"] = "application/json";
57     openOptions.headers["Authorization"] = "Bearer your_token_here";
58     closeOptions.code = LWS_CLOSE_STATUS_NORMAL;
59     closeOptions.reason = "";
60     client->Registcallback(OnOpen, OnMessage, OnError, OnClose);
61     int32_t ret = client->Connect("www.baidu.com", openOptions);
62     EXPECT_EQ(ret, 0);
63 }
64 
65 HWTEST_F(WebSocketTest, WebSocketConnect002, TestSize.Level1)
66 {
67     int32_t ret = 0;
68     openOptions.headers["Content-Type"] = "application/json";
69     openOptions.headers["Authorization"] = "Bearer your_token_here";
70     client->Registcallback(OnOpen, OnMessage, OnError, OnClose);
71     ret = client->Connect("www.baidu.com", openOptions);
72     EXPECT_EQ(ret, 0);
73 }
74 
75 HWTEST_F(WebSocketTest, WebSocketSend003, TestSize.Level1)
76 {
77     int32_t ret;
78     const char *data = "Hello, world!";
79     int32_t length = std::strlen(data);
80     client->Connect("www.baidu.com", openOptions);
81     ret = client->Send(const_cast<char *>(data), length);
82     EXPECT_EQ(ret, 0);
83 }
84 
85 HWTEST_F(WebSocketTest, WebSocketClose004, TestSize.Level1)
86 {
87     const int32_t WEBSOCKET_NO_CONNECTION = 1017;
88     CloseOption CloseOptions;
89     CloseOptions.code = LWS_CLOSE_STATUS_NORMAL;
90     CloseOptions.reason = "";
91     int32_t ret = client->Close(CloseOptions);
92     EXPECT_EQ(ret, WEBSOCKET_NO_CONNECTION);
93 }
94 
95 HWTEST_F(WebSocketTest, WebSocketDestroy005, TestSize.Level1)
96 {
97     int32_t ret;
98     ret = client->Destroy();
99     delete client;
100     EXPECT_EQ(ret, 0);
101 }
102 
103 } // namespace