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 "test.h"
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 #include "tools/log.h"
20 #include "js_uri.h"
21 #include "native_module_uri.h"
22
23 #define ASSERT_CHECK_CALL(call) \
24 { \
25 ASSERT_EQ(call, napi_ok); \
26 }
27
28 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
29 { \
30 napi_valuetype valueType = napi_undefined; \
31 ASSERT_TRUE(value != nullptr); \
32 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
33 ASSERT_EQ(valueType, type); \
34 }
35
36 HWTEST_F(NativeEngineTest, ConstructorTest001, testing::ext::TestSize.Level0)
37 {
38 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
39 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
40 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@www.baidu.com:99");
41 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@www.baidu.com:99/path/path?query");
42 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
43 ASSERT_STREQ(uri.GetHost().c_str(), "www.baidu.com");
44 ASSERT_STREQ(uri.GetPort().c_str(), "99");
45 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
46 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
47 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
48 }
49
50 HWTEST_F(NativeEngineTest, ConstructorTest002, testing::ext::TestSize.Level0)
51 {
52 OHOS::Uri::Uri uri("http://username:password@[1080::8:800:200C:417A]:99/path/66path1?query#fagment");
53 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
54 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[1080::8:800:200C:417A]:99");
55 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[1080::8:800:200C:417A]:99/path/66path1?query");
56 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
57 ASSERT_STREQ(uri.GetHost().c_str(), "[1080::8:800:200C:417A]");
58 ASSERT_STREQ(uri.GetPort().c_str(), "99");
59 ASSERT_STREQ(uri.GetPath().c_str(), "/path/66path1");
60 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
61 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
62 }
63
64 HWTEST_F(NativeEngineTest, ConstructorTest003, testing::ext::TestSize.Level0)
65 {
66 OHOS::Uri::Uri uri("http://username:password@[::]:88/path/path66?foooo#gaogao");
67 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
68 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[::]:88");
69 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[::]:88/path/path66?foooo");
70 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
71 ASSERT_STREQ(uri.GetHost().c_str(), "[::]");
72 ASSERT_STREQ(uri.GetPort().c_str(), "88");
73 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path66");
74 ASSERT_STREQ(uri.GetQuery().c_str(), "foooo");
75 ASSERT_STREQ(uri.GetFragment().c_str(), "gaogao");
76 }
77
78 HWTEST_F(NativeEngineTest, ConstructorTest004, testing::ext::TestSize.Level0)
79 {
80 OHOS::Uri::Uri uri("http://username:password@[1:0:0:1:2:1:2:1]:99/path/66path1?query#fagment");
81 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
82 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[1:0:0:1:2:1:2:1]:99");
83 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[1:0:0:1:2:1:2:1]:99/path/66path1?query");
84 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
85 ASSERT_STREQ(uri.GetHost().c_str(), "[1:0:0:1:2:1:2:1]");
86 ASSERT_STREQ(uri.GetPort().c_str(), "99");
87 ASSERT_STREQ(uri.GetPath().c_str(), "/path/66path1");
88 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
89 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
90 }
91
92 HWTEST_F(NativeEngineTest, ConstructorTest005, testing::ext::TestSize.Level0)
93 {
94 OHOS::Uri::Uri uri("http://username:password@[::FFFF:129.144.52.38]:99/path/path?query#fagment");
95 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
96 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[::FFFF:129.144.52.38]:99");
97 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[::FFFF:129.144.52.38]:99/path/path?query");
98 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
99 ASSERT_STREQ(uri.GetHost().c_str(), "[::FFFF:129.144.52.38]");
100 ASSERT_STREQ(uri.GetPort().c_str(), "99");
101 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
102 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
103 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
104 }
105
106 HWTEST_F(NativeEngineTest, ConstructorTest006, testing::ext::TestSize.Level0)
107 {
108 OHOS::Uri::Uri uri("http://username:password@[::192.9.5.5]:99/path/path?query#fagment");
109 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
110 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[::192.9.5.5]:99");
111 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[::192.9.5.5]:99/path/path?query");
112 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
113 ASSERT_STREQ(uri.GetHost().c_str(), "[::192.9.5.5]");
114 ASSERT_STREQ(uri.GetPort().c_str(), "99");
115 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
116 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
117 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
118 }
119
120 HWTEST_F(NativeEngineTest, ConstructorTest007, testing::ext::TestSize.Level0)
121 {
122 OHOS::Uri::Uri uri("http://username:password@[22::22:2:2%ss]:99/path/path?query#fagment");
123 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
124 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[22::22:2:2%ss]:99");
125 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[22::22:2:2%ss]:99/path/path?query");
126 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
127 ASSERT_STREQ(uri.GetHost().c_str(), "[22::22:2:2%ss]");
128 ASSERT_STREQ(uri.GetPort().c_str(), "99");
129 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
130 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
131 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
132 }
133
134 HWTEST_F(NativeEngineTest, ConstructorTest008, testing::ext::TestSize.Level0)
135 {
136 OHOS::Uri::Uri uri("http://username:password@[fe80:0000:0001:0000:0440:44ff:1233:5678]"
137 ":99/path/path?query#fagment");
138 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
139 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[fe80:0000:0001:0000:0440:44ff:1233:5678]:99");
140 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[fe80:0000:0001:0000:0440:44ff:1233:5678]"
141 ":99/path/path?query");
142 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
143 ASSERT_STREQ(uri.GetHost().c_str(), "[fe80:0000:0001:0000:0440:44ff:1233:5678]");
144 ASSERT_STREQ(uri.GetPort().c_str(), "99");
145 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
146 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
147 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
148 }
149
150 HWTEST_F(NativeEngineTest, ConstructorTest009, testing::ext::TestSize.Level0)
151 {
152 OHOS::Uri::Uri uri("http://username:password@[fe80::0001:0000]:99/path/path?query#fagment");
153 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
154 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@[fe80::0001:0000]:99");
155 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@[fe80::0001:0000]:99/path/path?query");
156 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
157 ASSERT_STREQ(uri.GetHost().c_str(), "[fe80::0001:0000]");
158 ASSERT_STREQ(uri.GetPort().c_str(), "99");
159 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
160 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
161 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
162 }
163
164 HWTEST_F(NativeEngineTest, ConstructorTest010, testing::ext::TestSize.Level0)
165 {
166 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
167 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
168 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@www.baidu.com:99");
169 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@www.baidu.com:99/path/path?query");
170 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
171 ASSERT_STREQ(uri.GetHost().c_str(), "www.baidu.com");
172 ASSERT_STREQ(uri.GetPort().c_str(), "99");
173 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
174 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
175 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
176 }
177
178 HWTEST_F(NativeEngineTest, ConstructorTest011, testing::ext::TestSize.Level0)
179 {
180 OHOS::Uri::Uri uri("http://username:password@199.98.55.44:99/path/path?query#fagment");
181 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
182 ASSERT_STREQ(uri.GetAuthority().c_str(), "username:password@199.98.55.44:99");
183 ASSERT_STREQ(uri.GetSsp().c_str(), "//username:password@199.98.55.44:99/path/path?query");
184 ASSERT_STREQ(uri.GetUserinfo().c_str(), "username:password");
185 ASSERT_STREQ(uri.GetHost().c_str(), "199.98.55.44");
186 ASSERT_STREQ(uri.GetPort().c_str(), "99");
187 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
188 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
189 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
190 }
191
192 HWTEST_F(NativeEngineTest, ConstructorTest012, testing::ext::TestSize.Level0)
193 {
194 OHOS::Uri::Uri uri("http://16.9.5.4:99/path/path?query#fagment");
195 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
196 ASSERT_STREQ(uri.GetAuthority().c_str(), "16.9.5.4:99");
197 ASSERT_STREQ(uri.GetSsp().c_str(), "//16.9.5.4:99/path/path?query");
198 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
199 ASSERT_STREQ(uri.GetHost().c_str(), "16.9.5.4");
200 ASSERT_STREQ(uri.GetPort().c_str(), "99");
201 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
202 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
203 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
204 }
205
206 HWTEST_F(NativeEngineTest, ConstructorTest013, testing::ext::TestSize.Level0)
207 {
208 OHOS::Uri::Uri uri("http://[::168:169:333]:99/path/path?query#fagment");
209 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
210 ASSERT_STREQ(uri.GetAuthority().c_str(), "[::168:169:333]:99");
211 ASSERT_STREQ(uri.GetSsp().c_str(), "//[::168:169:333]:99/path/path?query");
212 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
213 ASSERT_STREQ(uri.GetHost().c_str(), "[::168:169:333]");
214 ASSERT_STREQ(uri.GetPort().c_str(), "99");
215 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
216 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
217 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
218 }
219
220 HWTEST_F(NativeEngineTest, ConstructorTest014, testing::ext::TestSize.Level0)
221 {
222 OHOS::Uri::Uri uri("http://user@49.10hh8.54.12:80/path/path?query#qwer");
223 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
224 ASSERT_STREQ(uri.GetAuthority().c_str(), "user@49.10hh8.54.12:80");
225 ASSERT_STREQ(uri.GetSsp().c_str(), "//user@49.10hh8.54.12:80/path/path?query");
226 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
227 ASSERT_STREQ(uri.GetHost().c_str(), "");
228 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
229 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
230 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
231 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
232 }
233
234 HWTEST_F(NativeEngineTest, ConstructorTest015, testing::ext::TestSize.Level0)
235 {
236 OHOS::Uri::Uri uri("http://user@www.baidu.com/path/path?query#qwer");
237 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
238 ASSERT_STREQ(uri.GetAuthority().c_str(), "user@www.baidu.com");
239 ASSERT_STREQ(uri.GetSsp().c_str(), "//user@www.baidu.com/path/path?query");
240 ASSERT_STREQ(uri.GetUserinfo().c_str(), "user");
241 ASSERT_STREQ(uri.GetHost().c_str(), "www.baidu.com");
242 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
243 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
244 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
245 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
246 }
247
248 HWTEST_F(NativeEngineTest, ConstructorTest016, testing::ext::TestSize.Level0)
249 {
250 OHOS::Uri::Uri uri("ftp://user@www.1hw.1com:77/path/path?query#qwer");
251 ASSERT_STREQ(uri.GetScheme().c_str(), "ftp");
252 ASSERT_STREQ(uri.GetAuthority().c_str(), "user@www.1hw.1com:77");
253 ASSERT_STREQ(uri.GetSsp().c_str(), "//user@www.1hw.1com:77/path/path?query");
254 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
255 ASSERT_STREQ(uri.GetHost().c_str(), "");
256 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
257 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
258 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
259 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
260 }
261
262 HWTEST_F(NativeEngineTest, ConstructorTest017, testing::ext::TestSize.Level0)
263 {
264 OHOS::Uri::Uri uri("http://user@hosthost/path/path?query#qwer");
265 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
266 ASSERT_STREQ(uri.GetAuthority().c_str(), "user@hosthost");
267 ASSERT_STREQ(uri.GetSsp().c_str(), "//user@hosthost/path/path?query");
268 ASSERT_STREQ(uri.GetUserinfo().c_str(), "user");
269 ASSERT_STREQ(uri.GetHost().c_str(), "hosthost");
270 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
271 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
272 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
273 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
274 }
275
276 HWTEST_F(NativeEngineTest, ConstructorTest018, testing::ext::TestSize.Level0)
277 {
278 OHOS::Uri::Uri uri("http://user@[::]/path/path?query#qwer");
279 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
280 ASSERT_STREQ(uri.GetAuthority().c_str(), "user@[::]");
281 ASSERT_STREQ(uri.GetSsp().c_str(), "//user@[::]/path/path?query");
282 ASSERT_STREQ(uri.GetUserinfo().c_str(), "user");
283 ASSERT_STREQ(uri.GetHost().c_str(), "[::]");
284 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
285 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
286 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
287 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
288 }
289
290 HWTEST_F(NativeEngineTest, ConstructorTest019, testing::ext::TestSize.Level0)
291 {
292 OHOS::Uri::Uri uri("http://[::192:0:5]/path/path?query#qwer");
293 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
294 ASSERT_STREQ(uri.GetAuthority().c_str(), "[::192:0:5]");
295 ASSERT_STREQ(uri.GetSsp().c_str(), "//[::192:0:5]/path/path?query");
296 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
297 ASSERT_STREQ(uri.GetHost().c_str(), "[::192:0:5]");
298 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
299 ASSERT_STREQ(uri.GetPath().c_str(), "/path/path");
300 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
301 ASSERT_STREQ(uri.GetFragment().c_str(), "qwer");
302 }
303
304 HWTEST_F(NativeEngineTest, ConstructorTest020, testing::ext::TestSize.Level0)
305 {
306 OHOS::Uri::Uri uri("http:/username:password@www.baidu.com:99/path/path?query#fagment");
307 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
308 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
309 ASSERT_STREQ(uri.GetSsp().c_str(), "/username:password@www.baidu.com:99/path/path?query");
310 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
311 ASSERT_STREQ(uri.GetHost().c_str(), "");
312 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
313 ASSERT_STREQ(uri.GetPath().c_str(), "/username:password@www.baidu.com:99/path/path");
314 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
315 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
316 }
317
318 HWTEST_F(NativeEngineTest, ConstructorTest021, testing::ext::TestSize.Level0)
319 {
320 OHOS::Uri::Uri uri("http:/&username:password@[1080::8:800:200C:417A]:99/path/66path1?query#fagment");
321 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
322 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
323 ASSERT_STREQ(uri.GetSsp().c_str(), "/&username:password@[1080::8:800:200C:417A]:99/path/66path1?query");
324 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
325 ASSERT_STREQ(uri.GetHost().c_str(), "");
326 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
327 ASSERT_STREQ(uri.GetPath().c_str(), "/&username:password@[1080::8:800:200C:417A]:99/path/66path1");
328 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
329 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
330 }
331
332 HWTEST_F(NativeEngineTest, ConstructorTest022, testing::ext::TestSize.Level0)
333 {
334 OHOS::Uri::Uri uri("http:/[username:password@[::FFFF:129.144.52.38]:99/path/path?query#fagment");
335 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
336 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
337 ASSERT_STREQ(uri.GetSsp().c_str(), "/[username:password@[::FFFF:129.144.52.38]:99/path/path?query");
338 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
339 ASSERT_STREQ(uri.GetHost().c_str(), "");
340 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
341 ASSERT_STREQ(uri.GetPath().c_str(), "/[username:password@[::FFFF:129.144.52.38]:99/path/path");
342 ASSERT_STREQ(uri.GetQuery().c_str(), "query");
343 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
344 }
345
346 HWTEST_F(NativeEngineTest, ConstructorTest023, testing::ext::TestSize.Level0)
347 {
348 OHOS::Uri::Uri uri("http:username:password@[1080::8:800:200C:417A]:99/path/66path1?query#fagment");
349 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
350 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
351 ASSERT_STREQ(uri.GetSsp().c_str(), "username:password@[1080::8:800:200C:417A]:99/path/66path1?query");
352 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
353 ASSERT_STREQ(uri.GetHost().c_str(), "");
354 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
355 ASSERT_STREQ(uri.GetPath().c_str(), "");
356 ASSERT_STREQ(uri.GetQuery().c_str(), "");
357 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
358 }
359
360 HWTEST_F(NativeEngineTest, ConstructorTest024, testing::ext::TestSize.Level0)
361 {
362 OHOS::Uri::Uri uri("http:^$username:password@[::192.9.5.5]:99/path/path?query#fagment");
363 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
364 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
365 ASSERT_STREQ(uri.GetSsp().c_str(), "^$username:password@[::192.9.5.5]:99/path/path?query");
366 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
367 ASSERT_STREQ(uri.GetHost().c_str(), "");
368 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
369 ASSERT_STREQ(uri.GetPath().c_str(), "");
370 ASSERT_STREQ(uri.GetQuery().c_str(), "");
371 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
372 }
373
374 HWTEST_F(NativeEngineTest, ConstructorTest025, testing::ext::TestSize.Level0)
375 {
376 OHOS::Uri::Uri uri("http:[?]username:password@[fe80::0001:0000]:99/path/path?query#fagment");
377 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
378 ASSERT_STREQ(uri.GetAuthority().c_str(), "");
379 ASSERT_STREQ(uri.GetSsp().c_str(), "[?]username:password@[fe80::0001:0000]:99/path/path?query");
380 ASSERT_STREQ(uri.GetUserinfo().c_str(), "");
381 ASSERT_STREQ(uri.GetHost().c_str(), "");
382 ASSERT_STREQ(uri.GetPort().c_str(), "-1");
383 ASSERT_STREQ(uri.GetPath().c_str(), "");
384 ASSERT_STREQ(uri.GetQuery().c_str(), "");
385 ASSERT_STREQ(uri.GetFragment().c_str(), "fagment");
386 }
387
388 HWTEST_F(NativeEngineTest, ConstructorTest026, testing::ext::TestSize.Level0)
389 {
390 OHOS::Uri::Uri uri("");
391 ASSERT_STREQ(uri.GetScheme().c_str(), "");
392 ASSERT_STREQ(uri.GetSsp().c_str(), "");
393 ASSERT_STREQ(uri.GetFragment().c_str(), "");
394 ASSERT_STREQ(uri.IsFailed().c_str(), "uri is empty");
395 }
396
397 HWTEST_F(NativeEngineTest, ConstructorTest027, testing::ext::TestSize.Level0)
398 {
399 OHOS::Uri::Uri uri("#asd;");
400 ASSERT_STREQ(uri.IsFailed().c_str(), "#It can't be the first");
401 }
402
403 HWTEST_F(NativeEngineTest, ConstructorTest028, testing::ext::TestSize.Level0)
404 {
405 OHOS::Uri::Uri uri("?sa^d:s#asd;");
406 ASSERT_STREQ(uri.IsFailed().c_str(), "Query does not conform to the rule");
407 }
408
409 HWTEST_F(NativeEngineTest, ConstructorTest029, testing::ext::TestSize.Level0)
410 {
411 OHOS::Uri::Uri uri("?sad:s#a^sd;");
412 ASSERT_STREQ(uri.IsFailed().c_str(), "Fragment does not conform to the rule");
413 }
414
415 HWTEST_F(NativeEngineTest, ConstructorTest030, testing::ext::TestSize.Level0)
416 {
417 OHOS::Uri::Uri uri("4http:/username:password@www.baidu.com:99/path/path?query#fagment");
418 ASSERT_STREQ(uri.IsFailed().c_str(), "Scheme the first character must be a letter");
419 }
420
421 HWTEST_F(NativeEngineTest, ConstructorTest031, testing::ext::TestSize.Level0)
422 {
423 OHOS::Uri::Uri uri("ht*tp:/username:password@www.baidu.com:99/path/path?query#fagment");
424 ASSERT_STREQ(uri.GetScheme().c_str(), "ht*tp");
425 }
426
427 HWTEST_F(NativeEngineTest, ConstructorTest032, testing::ext::TestSize.Level0)
428 {
429 OHOS::Uri::Uri uri("/usern]ame/path/path?query#fagment");
430 ASSERT_STREQ(uri.IsFailed().c_str(), "SpecialPath does not conform to the rule");
431 }
432
433 HWTEST_F(NativeEngineTest, ConstructorTest033, testing::ext::TestSize.Level0)
434 {
435 OHOS::Uri::Uri uri("/username/path/path?query#fagment");
436 ASSERT_STREQ(uri.IsFailed().c_str(), "");
437 }
438
439 HWTEST_F(NativeEngineTest, ConstructorTest034, testing::ext::TestSize.Level0)
440 {
441 OHOS::Uri::Uri uri("http:/userna^me:password@www.baidu.com:99/path/path?query#fagment");
442 ASSERT_STREQ(uri.IsFailed().c_str(), "");
443 }
444
445 HWTEST_F(NativeEngineTest, ConstructorTest035, testing::ext::TestSize.Level0)
446 {
447 OHOS::Uri::Uri uri("http://?query#fagment");
448 ASSERT_STREQ(uri.GetScheme().c_str(), "http");
449 }
450
451 HWTEST_F(NativeEngineTest, ConstructorTest036, testing::ext::TestSize.Level0)
452 {
453 OHOS::Uri::Uri uri("http:/username:password@www.baidu.com:99/pa^th/path?query#fagment");
454 ASSERT_STREQ(uri.IsFailed().c_str(), "");
455 }
456
457 HWTEST_F(NativeEngineTest, ConstructorTest037, testing::ext::TestSize.Level0)
458 {
459 OHOS::Uri::Uri uri("http:/username:password@www.baidu.com:9^9/path/path?query#fagment");
460 ASSERT_STREQ(uri.IsFailed().c_str(), "");
461 }
462
463 HWTEST_F(NativeEngineTest, ConstructorTest038, testing::ext::TestSize.Level0)
464 {
465 OHOS::Uri::Uri uri("http:/username:password@[1:0:0:1:2:1:2:1]:9^9/path/path?query#fagment");
466 ASSERT_STREQ(uri.IsFailed().c_str(), "");
467 }
468
469 HWTEST_F(NativeEngineTest, ConstructorTest039, testing::ext::TestSize.Level0)
470 {
471 OHOS::Uri::Uri uri("http:/username:password@[1:0:0:1:2:1:2:1/path/path?query#fagment");
472 ASSERT_STREQ(uri.IsFailed().c_str(), "");
473 }
474
475 HWTEST_F(NativeEngineTest, ConstructorTest040, testing::ext::TestSize.Level0)
476 {
477 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:sfvs1:2:1:2:1]:99/path1?query#fagment");
478 ASSERT_STREQ(uri.IsFailed().c_str(), "ipv6 does not conform to the rule");
479 }
480
481 HWTEST_F(NativeEngineTest, ConstructorTest041, testing::ext::TestSize.Level0)
482 {
483 OHOS::Uri::Uri uri("http、:/username:password@www.baidu.com:99/path/path?query#fagment");
484 ASSERT_STREQ(uri.IsFailed().c_str(), "scheme does not conform to the rule");
485 }
486
487 HWTEST_F(NativeEngineTest, ConstructorTest042, testing::ext::TestSize.Level0)
488 {
489 OHOS::Uri::Uri uri("http://[ffff::1]:、99/path/path?query#fagment");
490 ASSERT_STREQ(uri.IsFailed().c_str(), "Prot does not conform to the rule");
491 }
492
493 HWTEST_F(NativeEngineTest, ConstructorTest043, testing::ext::TestSize.Level0)
494 {
495 OHOS::Uri::Uri uri("http://[ffff::1:99/path/path?query#fagment");
496 ASSERT_STREQ(uri.IsFailed().c_str(), "IPv6 is missing a closing bracket");
497 }
498
499 HWTEST_F(NativeEngineTest, ConstructorTest044, testing::ext::TestSize.Level0)
500 {
501 OHOS::Uri::Uri uri("http://ffff::1]:99/path/path?query#fagment");
502 ASSERT_STREQ(uri.IsFailed().c_str(), "host does not conform to the rule");
503 }
504
505 HWTEST_F(NativeEngineTest, EqualsTest001, testing::ext::TestSize.Level0)
506 {
507 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment");
508 OHOS::Uri::Uri uri1 = uri;
509 ASSERT_TRUE(uri.Equals(uri1));
510 }
511
512 HWTEST_F(NativeEngineTest, EqualsTest002, testing::ext::TestSize.Level0)
513 {
514 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment");
515 OHOS::Uri::Uri uri1("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment");
516 ASSERT_TRUE(uri.Equals(uri1));
517 }
518
519 HWTEST_F(NativeEngineTest, EqualsTest003, testing::ext::TestSize.Level0)
520 {
521 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment");
522 OHOS::Uri::Uri uri1("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment123");
523 ASSERT_FALSE(uri.Equals(uri1));
524 }
525
526 HWTEST_F(NativeEngineTest, EqualsTest004, testing::ext::TestSize.Level0)
527 {
528 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
529 OHOS::Uri::Uri uri1("http://username:password@www.baidu.com:29/path/path?query#fagment");
530 ASSERT_FALSE(uri.Equals(uri1));
531 }
532
533 HWTEST_F(NativeEngineTest, EqualsTest005, testing::ext::TestSize.Level0)
534 {
535 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
536 OHOS::Uri::Uri uri1("http://user1name:password@www.baidu.com:99/path/path?query#fagment");
537 ASSERT_FALSE(uri.Equals(uri1));
538 }
539
540 HWTEST_F(NativeEngineTest, EqualsTest006, testing::ext::TestSize.Level0)
541 {
542 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
543 OHOS::Uri::Uri uri1("http://username:password@w2ww.baidu.com:99/path/path?query#fagment");
544 ASSERT_FALSE(uri.Equals(uri1));
545 }
546
547 HWTEST_F(NativeEngineTest, EqualsTest007, testing::ext::TestSize.Level0)
548 {
549 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
550 OHOS::Uri::Uri uri1("http://username:password@www.baidu.com:99/pa4th/path?query#fagment");
551 ASSERT_FALSE(uri.Equals(uri1));
552 }
553
554 HWTEST_F(NativeEngineTest, EqualsTest008, testing::ext::TestSize.Level0)
555 {
556 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?qu4ery#fagment");
557 OHOS::Uri::Uri uri1("http://username:password@www.baidu.com:99/path/path?query#fagment");
558 ASSERT_FALSE(uri.Equals(uri1));
559 }
560
561 HWTEST_F(NativeEngineTest, EqualsTest009, testing::ext::TestSize.Level0)
562 {
563 OHOS::Uri::Uri uri("http://username:password@www.baidu.com:99/path/path?query#fagment");
564 OHOS::Uri::Uri uri1("h4ttp://username:password@www.baidu.com:99/path/path?query#fagment");
565 ASSERT_FALSE(uri.Equals(uri1));
566 }
567
568 HWTEST_F(NativeEngineTest, NormalizeTest001, testing::ext::TestSize.Level0)
569 {
570 OHOS::Uri::Uri uri("http://user@[1:0:0:1:2:1:2:1]:99/path/66./../././mm/.././path1?query#fagment");
571 std::string normalize = uri.Normalize();
572 ASSERT_STREQ(normalize.c_str(), "http://user@[1:0:0:1:2:1:2:1]:99/path/path1?query#fagment");
573 }
574
575 HWTEST_F(NativeEngineTest, NormalizeTest002, testing::ext::TestSize.Level0)
576 {
577 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path?query#fagment");
578 std::string normalize = uri.Normalize();
579 ASSERT_STREQ(normalize.c_str(), "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path?query#fagment");
580 }
581
582 HWTEST_F(NativeEngineTest, NormalizeTest003, testing::ext::TestSize.Level0)
583 {
584 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment");
585 std::string normalize = uri.Normalize();
586 ASSERT_STREQ(normalize.c_str(), "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../../aa/bb/cc?query#fagment");
587 }
588
589 HWTEST_F(NativeEngineTest, NormalizeTest004, testing::ext::TestSize.Level0)
590 {
591 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99?query#fagment");
592 std::string normalize = uri.Normalize();
593 ASSERT_STREQ(normalize.c_str(), "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99?query#fagment");
594 }
595
596 HWTEST_F(NativeEngineTest, ToStringTest001, testing::ext::TestSize.Level0)
597 {
598 OHOS::Uri::Uri uri("http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/aa/bb/cc?query#fagment");
599 ASSERT_STREQ(uri.ToString().c_str(), "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/aa/bb/cc?query#fagment");
600 }
601
602 HWTEST_F(NativeEngineTest, ToStringTest002, testing::ext::TestSize.Level0)
603 {
604 OHOS::Uri::Uri uri("htt1p://gg:gaogao@[::192.9.5.5]:99/path/66path1?query#fagment");
605 ASSERT_STREQ(uri.ToString().c_str(), "htt1p://gg:gaogao@[::192.9.5.5]:99/path/66path1?query#fagment");
606 }
607
608 HWTEST_F(NativeEngineTest, ToStringTest003, testing::ext::TestSize.Level0)
609 {
610 OHOS::Uri::Uri uri("ftp://username:www.baidu.com/path?query#fagment");
611 ASSERT_STREQ(uri.ToString().c_str(), "ftp://username:www.baidu.com/path?query#fagment");
612 }
613
614 HWTEST_F(NativeEngineTest, IsAbsoluteTest001, testing::ext::TestSize.Level0)
615 {
616 OHOS::Uri::Uri uri("f/tp://username:password@www.baidu.com:88/path?query#fagment");
617 bool res = uri.IsAbsolute();
618 ASSERT_FALSE(res);
619 }
620
621 HWTEST_F(NativeEngineTest, IsAbsoluteTest002, testing::ext::TestSize.Level0)
622 {
623 OHOS::Uri::Uri uri("ftp://username:password@www.baidu.com:88/path?query#fagment");
624 bool res = uri.IsAbsolute();
625 ASSERT_TRUE(res);
626 }
627
628 HWTEST_F(NativeEngineTest, IsAbsoluteTest003, testing::ext::TestSize.Level0)
629 {
630 OHOS::Uri::Uri uri("htt/p://username:password@www.baidu.com:88/path?query#fagment");
631 bool res = uri.IsAbsolute();
632 ASSERT_FALSE(res);
633 }
634
635 HWTEST_F(NativeEngineTest, IsRelativeTest001, testing::ext::TestSize.Level0)
636 {
637 OHOS::Uri::Uri uri("https://www.example.com/aaa");
638 bool res = uri.IsRelative();
639 ASSERT_FALSE(res);
640 }
641
642 HWTEST_F(NativeEngineTest, IsRelativeTest002, testing::ext::TestSize.Level0)
643 {
644 OHOS::Uri::Uri uri("/bbb");
645 bool res = uri.IsRelative();
646 ASSERT_TRUE(res);
647 }
648
649 HWTEST_F(NativeEngineTest, IsOpaqueTest001, testing::ext::TestSize.Level0)
650 {
651 OHOS::Uri::Uri uri("aaa:user@example.com");
652 bool res = uri.IsOpaque();
653 ASSERT_TRUE(res);
654 }
655
656 HWTEST_F(NativeEngineTest, IsOpaqueTest002, testing::ext::TestSize.Level0)
657 {
658 OHOS::Uri::Uri uri("content://com.example/bbb");
659 bool res = uri.IsOpaque();
660 ASSERT_FALSE(res);
661 }
662
663 HWTEST_F(NativeEngineTest, IsHierarchicalTest001, testing::ext::TestSize.Level0)
664 {
665 OHOS::Uri::Uri uri("https://www.example.com/path/to/resource");
666 bool res = uri.IsHierarchical();
667 ASSERT_TRUE(res);
668 }
669
670 HWTEST_F(NativeEngineTest, IsHierarchicalTest002, testing::ext::TestSize.Level0)
671 {
672 OHOS::Uri::Uri uri("/path/to/resource");
673 bool res = uri.IsHierarchical();
674 ASSERT_TRUE(res);
675 }
676
677 HWTEST_F(NativeEngineTest, IsHierarchicalTest003, testing::ext::TestSize.Level0)
678 {
679 OHOS::Uri::Uri uri("tel:123456789");
680 bool res = uri.IsHierarchical();
681 ASSERT_FALSE(res);
682 }
683
684 HWTEST_F(NativeEngineTest, AddQueryValueTest001, testing::ext::TestSize.Level0)
685 {
686 OHOS::Uri::Uri uri("https://username:password@host:8080/file?aaa=1#myfragment");
687 std::string temp = uri.AddQueryValue("bbb", "2");
688 ASSERT_STREQ(temp.c_str(), "https://username:password@host:8080/file?aaa=1&bbb=2#myfragment");
689 }
690
691 HWTEST_F(NativeEngineTest, AddQueryValueTest002, testing::ext::TestSize.Level0)
692 {
693 OHOS::Uri::Uri uri("mao:user@example.com");
694 std::string temp = uri.AddQueryValue("bb", "cc");
695 ASSERT_STREQ(temp.c_str(), "mao:?bb=cc");
696 }
697
698 HWTEST_F(NativeEngineTest, ClearQueryTest001, testing::ext::TestSize.Level0)
699 {
700 OHOS::Uri::Uri uri("https://username:password@host:8080/file?aaa=1#myfragment");
701 std::string temp = uri.ClearQuery();
702 ASSERT_STREQ(temp.c_str(), "https://username:password@host:8080/file#myfragment");
703 }
704
705 HWTEST_F(NativeEngineTest, GetSegmentTest001, testing::ext::TestSize.Level0)
706 {
707 OHOS::Uri::Uri uri("https://username:password@host:8080/file?aaa=1#myfragment");
708 std::vector<std::string> temp = uri.GetSegment();
709 ASSERT_EQ(temp.size(), 1);
710 }
711
712 HWTEST_F(NativeEngineTest, AddSegmentTest001, testing::ext::TestSize.Level0)
713 {
714 OHOS::Uri::Uri uri("https://username:password@host:8080/file?aaa=1#myfragment");
715 std::string temp = uri.AddSegment("segment");
716 ASSERT_STREQ(temp.c_str(), "https://username:password@host:8080/file/segment?aaa=1#myfragment");
717 }
718
719 HWTEST_F(NativeEngineTest, AddSegmentTest002, testing::ext::TestSize.Level0)
720 {
721 OHOS::Uri::Uri uri("mao:user@example.com");
722 std::string temp = uri.AddSegment("aaa");
723 ASSERT_STREQ(temp.c_str(), "mao:/aaa");
724 }
725
726 HWTEST_F(NativeEngineTest, GetTest001, testing::ext::TestSize.Level0)
727 {
728 OHOS::Uri::Uri uri("https://username:password@host:8080");
729 ASSERT_STREQ(uri.GetPath().c_str(), "");
730 ASSERT_STREQ(uri.GetQuery().c_str(), "");
731 ASSERT_STREQ(uri.GetFragment().c_str(), "");
732 }
733
GetStringUtf8(napi_env env,napi_value str)734 std::string GetStringUtf8(napi_env env, napi_value str)
735 {
736 std::string buffer = "";
737 size_t bufferSize = 0;
738 if (napi_get_value_string_utf8(env, str, nullptr, 0, &bufferSize) != napi_ok) {
739 HILOG_ERROR("can not get src size");
740 return buffer;
741 }
742 buffer.resize(bufferSize);
743 if (napi_get_value_string_utf8(env, str, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) {
744 HILOG_ERROR("can not get src value");
745 return buffer;
746 }
747 return buffer;
748 }
749
StrToNapiValue(napi_env env,const std::string & result)750 napi_value StrToNapiValue(napi_env env, const std::string &result)
751 {
752 napi_value output = nullptr;
753 napi_create_string_utf8(env, result.c_str(), result.size(), &output);
754 return output;
755 }
756
GetArray(napi_env env,const napi_value tempStr)757 std::vector<std::string> GetArray(napi_env env, const napi_value tempStr)
758 {
759 std::vector<std::string> strVec;
760 napi_value napiStr = nullptr;
761 uint32_t length = 0;
762 size_t strLength = 0;
763 napi_get_array_length(env, tempStr, &length);
764 for (size_t i = 0; i < length; i++) {
765 napi_get_element(env, tempStr, i, &napiStr);
766 if (napi_get_value_string_utf8(env, napiStr, nullptr, 0, &strLength) != napi_ok) {
767 HILOG_ERROR("can not get napiStr size");
768 return strVec;
769 }
770 if (strLength > 0) {
771 std::string itemStr = "";
772 itemStr.resize(strLength);
773 if (napi_get_value_string_utf8(env, napiStr, itemStr.data(), strLength + 1, &strLength) != napi_ok) {
774 HILOG_ERROR("can not get napiStr size");
775 return strVec;
776 }
777 strVec.push_back(itemStr);
778 } else {
779 strVec.push_back("");
780 }
781 }
782 return strVec;
783 }
784
785 HWTEST_F(NativeEngineTest, ModuleTest001, testing::ext::TestSize.Level0)
786 {
787 napi_env env = (napi_env)engine_;
788 napi_value exports = nullptr;
789 napi_create_object(env, &exports);
790 OHOS::Uri::UriInit(env, exports);
791 napi_value uriClass = nullptr;
792 napi_value constructorArgs[1] = { 0 };
793 std::string input = "http://username:password@www.baidu.com:99/path/path?query#fagment";
794 napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]);
795 napi_status status = napi_get_named_property(env, exports, "Uri", &uriClass);
796 napi_value instance = nullptr;
797 status = napi_new_instance(env, uriClass, 1, constructorArgs, &instance);
798 napi_value getTemp = nullptr;
799 napi_get_named_property(env, instance, "scheme", &getTemp);
800 std::string res = GetStringUtf8(env, getTemp);
801 ASSERT_STREQ(res.c_str(), "http");
802
803 napi_get_named_property(env, instance, "authority", &getTemp);
804 res = GetStringUtf8(env, getTemp);
805 ASSERT_STREQ(res.c_str(), "username:password@www.baidu.com:99");
806
807 napi_get_named_property(env, instance, "ssp", &getTemp);
808 res = GetStringUtf8(env, getTemp);
809 ASSERT_STREQ(res.c_str(), "//username:password@www.baidu.com:99/path/path?query");
810
811 napi_get_named_property(env, instance, "userInfo", &getTemp);
812 res = GetStringUtf8(env, getTemp);
813 ASSERT_STREQ(res.c_str(), "username:password");
814
815 napi_get_named_property(env, instance, "host", &getTemp);
816 res = GetStringUtf8(env, getTemp);
817 ASSERT_STREQ(res.c_str(), "www.baidu.com");
818
819 napi_get_named_property(env, instance, "port", &getTemp);
820 res = GetStringUtf8(env, getTemp);
821 ASSERT_STREQ(res.c_str(), "99");
822
823 napi_get_named_property(env, instance, "path", &getTemp);
824 res = GetStringUtf8(env, getTemp);
825 ASSERT_STREQ(res.c_str(), "/path/path");
826
827 napi_get_named_property(env, instance, "query", &getTemp);
828 res = GetStringUtf8(env, getTemp);
829 ASSERT_STREQ(res.c_str(), "query");
830
831 napi_get_named_property(env, instance, "fragment", &getTemp);
832 res = GetStringUtf8(env, getTemp);
833 ASSERT_STREQ(res.c_str(), "fagment");
834
835 napi_get_named_property(env, instance, "isFailed", &getTemp);
836 res = GetStringUtf8(env, getTemp);
837 ASSERT_STREQ(res.c_str(), "");
838 }
839
840 HWTEST_F(NativeEngineTest, ModuleTest002, testing::ext::TestSize.Level0)
841 {
842 napi_env env = (napi_env)engine_;
843 napi_value exports = nullptr;
844 napi_create_object(env, &exports);
845 OHOS::Uri::UriInit(env, exports);
846 napi_value uriClass = nullptr;
847 napi_value constructorArgs[1] = { 0 };
848 std::string input = "http://username:password@www.baidu.com:99/path/path?query#fagment";
849 napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]);
850 napi_get_named_property(env, exports, "Uri", &uriClass);
851 napi_value instance = nullptr;
852 napi_new_instance(env, uriClass, 1, constructorArgs, &instance);
853 napi_value tempFn = nullptr;
854 napi_get_named_property(env, instance, "checkIsAbsolute", &tempFn);
855 napi_value result = nullptr;
856 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
857 ASSERT_TRUE(result);
858
859 napi_get_named_property(env, instance, "toString", &tempFn);
860 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
861 std::string res = GetStringUtf8(env, result);
862 ASSERT_STREQ(res.c_str(), "http://username:password@www.baidu.com:99/path/path?query#fagment");
863
864 napi_get_named_property(env, instance, "normalize", &tempFn);
865 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
866 res = GetStringUtf8(env, result);
867 ASSERT_STREQ(res.c_str(), "http://username:password@www.baidu.com:99/path/path?query#fagment");
868
869 napi_value equalsFn = nullptr;
870 napi_get_named_property(env, instance, "equals", &equalsFn);
871 napi_value constructorArgs1[1] = { 0 };
872 std::string input2 = "http://username:password@www.baidu.com:88/path?query1#fagment";
873 napi_create_string_utf8(env, input2.c_str(), input2.size(), &constructorArgs1[0]);
874 napi_value otherInstance = nullptr;
875 napi_new_instance(env, uriClass, 1, constructorArgs1, &otherInstance);
876 napi_value args[1] = { otherInstance };
877 napi_value result1 = nullptr;
878 napi_call_function(env, instance, equalsFn, 1, args, &result1);
879 bool res1 = true;
880 napi_get_value_bool(env, result1, &res1);
881 ASSERT_FALSE(res1);
882 }
883
884 HWTEST_F(NativeEngineTest, ModuleTest003, testing::ext::TestSize.Level0)
885 {
886 napi_env env = (napi_env)engine_;
887 napi_value exports = nullptr;
888 napi_create_object(env, &exports);
889 OHOS::Uri::UriInit(env, exports);
890 napi_value uriClass = nullptr;
891 napi_value constructorArgs[1] = { 0 };
892 std::string input = "http://name:word@www.uritest.com:99/path/abc?query#fagment";
893 napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]);
894 napi_get_named_property(env, exports, "Uri", &uriClass);
895 napi_value instance = nullptr;
896 napi_new_instance(env, uriClass, 1, constructorArgs, &instance);
897 napi_value tempFn = nullptr;
898
899 napi_get_named_property(env, instance, "checkIsRelative", &tempFn);
900 napi_value result = nullptr;
901 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
902 bool flag = true;
903 napi_get_value_bool(env, result, &flag);
904 ASSERT_FALSE(flag);
905
906 napi_get_named_property(env, instance, "checkIsOpaque", &tempFn);
907 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
908 napi_get_value_bool(env, result, &flag);
909 ASSERT_FALSE(flag);
910
911 napi_get_named_property(env, instance, "checkIsHierarchical", &tempFn);
912 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
913 napi_get_value_bool(env, result, &flag);
914 ASSERT_TRUE(flag);
915
916 napi_value key = StrToNapiValue(env, "aaa");
917 napi_value value = StrToNapiValue(env, "bbb");
918 napi_value keyArgs[] = { key, value };
919 napi_get_named_property(env, instance, "addQueryValue", &tempFn);
920 napi_call_function(env, instance, tempFn, 2, keyArgs, &result);
921 std::string res = GetStringUtf8(env, result);
922 ASSERT_STREQ(res.c_str(), "http://name:word@www.uritest.com:99/path/abc?query&aaa=bbb#fagment");
923
924 napi_get_named_property(env, instance, "getSegment", &tempFn);
925 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
926 std::vector<std::string> temp = GetArray(env, result);
927 ASSERT_STREQ(temp[0].c_str(), "path");
928 ASSERT_STREQ(temp[1].c_str(), "abc");
929
930 napi_value segment = StrToNapiValue(env, "aaa");
931 napi_value segargs[] = { segment };
932 napi_get_named_property(env, instance, "addSegment", &tempFn);
933 napi_call_function(env, instance, tempFn, 1, segargs, &result);
934 res = GetStringUtf8(env, result);
935 ASSERT_STREQ(res.c_str(), "http://name:word@www.uritest.com:99/path/abc/aaa?query#fagment");
936
937 napi_get_named_property(env, instance, "clearQuery", &tempFn);
938 napi_call_function(env, instance, tempFn, 0, nullptr, &result);
939 res = GetStringUtf8(env, result);
940 ASSERT_STREQ(res.c_str(), "http://name:word@www.uritest.com:99/path/abc#fagment");
941 }