1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include <map>
18
19 #include "nweb_url_resource_response.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23 using namespace OHOS;
24
25 namespace OHOS::NWeb {
26 const std::string mime_type = "html";
27 const std::string encoding = "utf8";
28 const int status_code = 401;
29 const std::string reason_phrase = "Request requires authentication";
30 const std::map<std::string, std::string> response_headers = {{"key1", "value1"}, {"key2", "value2"}};
31 std::string input_stream = "input stream";
32
33 class NWebUrlResourceResponseTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void NWebUrlResourceResponseTest::SetUpTestCase(void)
42 {}
43
TearDownTestCase(void)44 void NWebUrlResourceResponseTest::TearDownTestCase(void)
45 {}
46
SetUp(void)47 void NWebUrlResourceResponseTest::SetUp(void)
48 {}
49
TearDown(void)50 void NWebUrlResourceResponseTest::TearDown(void)
51 {}
52
53 /**
54 * @tc.name: NWebUrlResourceResponse_Constructor_001
55 * @tc.desc: Test constructor with 5 params.
56 * @tc.type: FUNC
57 * @tc.require:
58 */
59 HWTEST_F(NWebUrlResourceResponseTest, NWebUrlResourceResponse_Constructor_001, TestSize.Level1)
60 {
61 std::shared_ptr<NWebUrlResourceResponse> webResonse = std::make_shared<NWebUrlResourceResponse>(mime_type,
62 encoding, status_code, reason_phrase, response_headers, input_stream);
63 std::string actual_stream = webResonse->ResponseData();
64 EXPECT_STREQ(input_stream.c_str(), actual_stream.c_str());
65
66 std::string actual_encoding = webResonse->ResponseEncoding();
67 EXPECT_STREQ(encoding.c_str(), actual_encoding.c_str());
68
69 std::string actual_mimtype = webResonse->ResponseMimeType();
70 EXPECT_STREQ(mime_type.c_str(), actual_mimtype.c_str());
71
72 std::map<std::string, std::string> actual_header = webResonse->ResponseHeaders();
73 EXPECT_EQ(response_headers, actual_header);
74
75 int actual_status_code = webResonse->ResponseStatusCode();
76 EXPECT_EQ(status_code, actual_status_code);
77
78 std::string actual_status = webResonse->ResponseStatus();
79 EXPECT_STREQ(reason_phrase.c_str(), actual_status.c_str());
80 }
81
82 /**
83 * @tc.name: NWebUrlResourceResponse_Constructor_002
84 * @tc.desc: Test constructor with 3 params.
85 * @tc.type: FUNC
86 * @tc.require:
87 */
88 HWTEST_F(NWebUrlResourceResponseTest, NWebUrlResourceResponse_Constructor_002, TestSize.Level1)
89 {
90 std::shared_ptr<NWebUrlResourceResponse> webResonse = std::make_shared<NWebUrlResourceResponse>(mime_type,
91 encoding, input_stream);
92 std::string actual_stream = webResonse->ResponseData();
93 EXPECT_STREQ(input_stream.c_str(), actual_stream.c_str());
94
95 std::string actual_encoding = webResonse->ResponseEncoding();
96 EXPECT_STREQ(encoding.c_str(), actual_encoding.c_str());
97
98 std::string actual_mimtype = webResonse->ResponseMimeType();
99 EXPECT_STREQ(mime_type.c_str(), actual_mimtype.c_str());
100 }
101
102 /**
103 * @tc.name: NWebUrlResourceResponse_Constructor_003
104 * @tc.desc: Test set and get functions.
105 * @tc.type: FUNC
106 * @tc.require:
107 */
108 HWTEST_F(NWebUrlResourceResponseTest, NWebUrlResourceResponse_Constructor_003, TestSize.Level1)
109 {
110 std::shared_ptr<NWebUrlResourceResponse> webResonse = std::make_shared<NWebUrlResourceResponse>(mime_type,
111 encoding, status_code, reason_phrase, response_headers, input_stream);
112 webResonse->PutResponseData(input_stream);
113 std::string actual_stream = webResonse->ResponseData();
114 EXPECT_STREQ(input_stream.c_str(), actual_stream.c_str());
115
116 webResonse->PutResponseEncoding(encoding);
117 std::string actual_encoding = webResonse->ResponseEncoding();
118 EXPECT_STREQ(encoding.c_str(), actual_encoding.c_str());
119
120 webResonse->PutResponseMimeType(mime_type);
121 std::string actual_mimtype = webResonse->ResponseMimeType();
122 EXPECT_STREQ(mime_type.c_str(), actual_mimtype.c_str());
123
124 webResonse->PutResponseHeaders(response_headers);
125 std::map<std::string, std::string> actual_header = webResonse->ResponseHeaders();
126 EXPECT_EQ(response_headers, actual_header);
127
128 webResonse->PutResponseStateAndStatuscode(status_code, reason_phrase);
129 int actual_status_code = webResonse->ResponseStatusCode();
130 EXPECT_EQ(status_code, actual_status_code);
131 std::string actual_reason_status = webResonse->ResponseStatus();
132 EXPECT_STREQ(reason_phrase.c_str(), actual_reason_status.c_str());
133 }
134 } // namespace OHOS::NWeb
135