• 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 <string_view>
17 
18 #include "gtest/gtest.h"
19 #include "http.h"
20 
21 using namespace OHOS::ArkCompiler::Toolchain;
22 
23 namespace panda::test {
24 class HttpDecoderTest : public testing::Test {
25 public:
26     static constexpr std::string_view REQUEST_HEADERS = "GET / HTTP/1.1\r\n"
27         "Host: 127.0.0.1:19015\r\n"
28         "Connection: Upgrade\r\n"
29         "Pragma: no-cache\r\n"
30         "Cache-Control: no-cache\r\n"
31         "User-Agent: Mozilla/5.0 (X11; Linux x86_64) Chrome/117.0.0.0 Safari/537.36\r\n"
32         "Upgrade: websocket\r\n"
33         "Origin: dvtls://dvtls\r\n"
34         "Sec-WebSocket-Version: 13\r\n"
35         "Accept-Encoding: gzip, deflate, br\r\n"
36         "Accept-Language: en-US,en;q=0.9\r\n"
37         "Sec-WebSocket-Key: AyuTxzyBTJJdViDskomT0Q==\r\n"
38         "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n";
39     std::string requestHeaders = std::string(REQUEST_HEADERS);
40 
41     static constexpr std::string_view ERR_REQUEST_HEADERS = "GEY\r\n";
42     std::string errRequestHeaders = std::string(ERR_REQUEST_HEADERS);
43 
44     static constexpr std::string_view NO_INFO_REQUEST_HEADERS = "GET\r\n";
45     std::string noInfoRequestHeaders = std::string(NO_INFO_REQUEST_HEADERS);
46 
47     static constexpr std::string_view RESPONSE_HEADERS = "HTTP/1.1 101 Switching Protocols\r\n"
48         "Connection: Upgrade\r\n"
49         "Upgrade: websocket\r\n"
50         "Sec-WebSocket-Accept: AyuTxzyBTJJdViDskomT0Q==\r\n\r\n";
51     std::string responseHeaders = std::string(RESPONSE_HEADERS);
52 
53     static constexpr std::string_view ERR_RESPONSE_HEADERS = "HTTB\r\n";
54     std::string errResponseHeaders = std::string(ERR_RESPONSE_HEADERS);
55 
56     static constexpr std::string_view NO_INFO_RESPONSE_HEADERS = "HTTP\r\n";
57     std::string noInfoResponseHeaders = std::string(NO_INFO_RESPONSE_HEADERS);
58 
59     static constexpr std::string_view EXPECTED_VERSION              = "HTTP/1.1";
60     static constexpr std::string_view EXPECTED_STATUS               = "101";
61     static constexpr std::string_view EXPECTED_CONNECTION           = "Upgrade";
62     static constexpr std::string_view EXPECTED_UPGRADE              = "websocket";
63     static constexpr std::string_view EXPECTED_SEC_WEBSOCKET_KEY    = "AyuTxzyBTJJdViDskomT0Q==";
64 };
65 
66 HWTEST_F(HttpDecoderTest, TestRequestDecode, testing::ext::TestSize.Level0)
67 {
68     HttpRequest parsed;
69     auto succeeded = HttpRequest::Decode(requestHeaders, parsed);
70 
71     ASSERT_TRUE(succeeded);
72     ASSERT_EQ(parsed.version, EXPECTED_VERSION);
73     ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
74     ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
75     ASSERT_EQ(parsed.secWebSocketKey, EXPECTED_SEC_WEBSOCKET_KEY);
76 }
77 
78 HWTEST_F(HttpDecoderTest, TestAbnormalRequestDecode, testing::ext::TestSize.Level0)
79 {
80     HttpRequest parsed;
81     ASSERT_FALSE(HttpRequest::Decode(errRequestHeaders, parsed));
82     ASSERT_TRUE(HttpRequest::Decode(noInfoRequestHeaders, parsed));
83 
84     ASSERT_EQ(parsed.version, "");
85     ASSERT_EQ(parsed.connection, "");
86     ASSERT_EQ(parsed.upgrade, "");
87     ASSERT_EQ(parsed.secWebSocketKey, "");
88 }
89 
90 HWTEST_F(HttpDecoderTest, TestResponseDecode, testing::ext::TestSize.Level0)
91 {
92     HttpResponse parsed;
93     auto succeeded = HttpResponse::Decode(responseHeaders, parsed);
94 
95     ASSERT_TRUE(succeeded);
96     ASSERT_EQ(parsed.version, EXPECTED_VERSION);
97     ASSERT_EQ(parsed.status, EXPECTED_STATUS);
98     ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
99     ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
100     ASSERT_EQ(parsed.secWebSocketAccept, EXPECTED_SEC_WEBSOCKET_KEY);
101 }
102 
103 HWTEST_F(HttpDecoderTest, TestAbnormalResponseDecode, testing::ext::TestSize.Level0)
104 {
105     HttpResponse parsed;
106     ASSERT_FALSE(HttpResponse::Decode(errResponseHeaders, parsed));
107     ASSERT_TRUE(HttpResponse::Decode(noInfoResponseHeaders, parsed));
108 
109     ASSERT_EQ(parsed.version, "");
110     ASSERT_EQ(parsed.status, "");
111     ASSERT_EQ(parsed.connection, "");
112     ASSERT_EQ(parsed.upgrade, "");
113     ASSERT_EQ(parsed.secWebSocketAccept, "");
114 }
115 
116 }  // namespace panda::test
117