• 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 "websocket/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 RESPONSE_HEADERS = "HTTP/1.1 101 Switching Protocols\r\n"
42          "Connection: Upgrade\r\n"
43          "Upgrade: websocket\r\n"
44          "Sec-WebSocket-Accept: AyuTxzyBTJJdViDskomT0Q==\r\n\r\n";
45      std::string responseHeaders = std::string(RESPONSE_HEADERS);
46  
47      static constexpr std::string_view EXPECTED_VERSION              = "HTTP/1.1";
48      static constexpr std::string_view EXPECTED_STATUS               = "101";
49      static constexpr std::string_view EXPECTED_CONNECTION           = "Upgrade";
50      static constexpr std::string_view EXPECTED_UPGRADE              = "websocket";
51      static constexpr std::string_view EXPECTED_SEC_WEBSOCKET_KEY    = "AyuTxzyBTJJdViDskomT0Q==";
52  };
53  
54  HWTEST_F(HttpDecoderTest, TestRequestDecode, testing::ext::TestSize.Level0)
55  {
56      HttpRequest parsed;
57      auto succeeded = HttpRequest::Decode(requestHeaders, parsed);
58  
59      ASSERT_TRUE(succeeded);
60      ASSERT_EQ(parsed.version, EXPECTED_VERSION);
61      ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
62      ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
63      ASSERT_EQ(parsed.secWebSocketKey, EXPECTED_SEC_WEBSOCKET_KEY);
64  }
65  
66  HWTEST_F(HttpDecoderTest, TestResponseDecode, testing::ext::TestSize.Level0)
67  {
68      HttpResponse parsed;
69      auto succeeded = HttpResponse::Decode(responseHeaders, parsed);
70  
71      ASSERT_TRUE(succeeded);
72      ASSERT_EQ(parsed.version, EXPECTED_VERSION);
73      ASSERT_EQ(parsed.status, EXPECTED_STATUS);
74      ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
75      ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
76      ASSERT_EQ(parsed.secWebSocketAccept, EXPECTED_SEC_WEBSOCKET_KEY);
77  }
78  }  // namespace panda::test
79