1 /*
2 * Copyright (C) 2021-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_common.h"
17
18 #include "constant.h"
19 #include "event_list.h"
20 #include "http_module.h"
21 #include "securec.h"
22
23 #define COMMON_DECLARE1 \
24 auto env = (napi_env)engine_; \
25 \
26 napi_value exports = NapiUtils::CreateObject(env); \
27 \
28 HttpModuleExports::InitHttpModule(env, exports); \
29 napi_value requestMethod = NapiUtils::GetNamedProperty(env, exports, HttpModuleExports::INTERFACE_REQUEST_METHOD); \
30 \
31 std::string methodGet = NapiUtils::GetStringPropertyUtf8(env, requestMethod, HttpConstant::HTTP_METHOD_GET); \
32 ASSERT_EQ(methodGet, HttpConstant::HTTP_METHOD_GET);
33
34 #define COMMON_DECLARE2 \
35 napi_value responseCode = NapiUtils::GetNamedProperty(env, exports, HttpModuleExports::INTERFACE_RESPONSE_CODE); \
36 uint32_t codeOK = NapiUtils::GetUint32Property(env, responseCode, "OK"); \
37 ASSERT_EQ(codeOK, (uint32_t)HttpModuleExports::ResponseCode::OK); \
38 \
39 napi_value createHttpFunc = NapiUtils::GetNamedProperty(env, exports, HttpModuleExports::FUNCTION_CREATE_HTTP); \
40 ASSERT_CHECK_VALUE_TYPE(env, createHttpFunc, napi_function);
41
42 #define COMMON_DECLARE3 \
43 napi_value httpRequestValue = NapiUtils::CallFunction(env, exports, createHttpFunc, 0, nullptr); \
44 ASSERT_CHECK_VALUE_TYPE(env, httpRequestValue, napi_object); \
45 \
46 napi_value requestFunc = \
47 NapiUtils::GetNamedProperty(env, httpRequestValue, HttpModuleExports::HttpRequest::FUNCTION_REQUEST); \
48 ASSERT_CHECK_VALUE_TYPE(env, requestFunc, napi_function);
49
50 #define DEFINE_TEST_BEGIN(name, ASSERT_CODE_OK) \
51 [[maybe_unused]] HWTEST_F(NativeEngineTest, /* NOLINT */ \
52 name, testing::ext::TestSize.Level0) \
53 { \
54 COMMON_DECLARE1 \
55 COMMON_DECLARE2 \
56 COMMON_DECLARE3 \
57 auto callbackOneParam = NapiUtils::CreateFunction(env, #name "CallbackOneParam", \
58 MAKE_CALLBACK_ONE_PARAM(#name, ASSERT_CODE_OK), nullptr); \
59 auto callbackTwoParam = NapiUtils::CreateFunction(env, #name "CallbackTwoParam", \
60 MAKE_CALLBACK_TWO_PARAM(#name, ASSERT_CODE_OK), nullptr);
61
62 #define DEFINE_TEST_END }
63
64 #define MAKE_CALLBACK_ONE_PARAM(FUNC_NAME, ASSERT_CODE_OK) \
65 [](napi_env env, napi_callback_info info) -> napi_value { \
66 NETSTACK_LOGI("%s", FUNC_NAME); \
67 \
68 napi_value thisVal = nullptr; \
69 size_t paramsCount = 1; \
70 napi_value params[1] = {nullptr}; \
71 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr)); \
72 \
73 if (NapiUtils::GetValueType(env, params[0]) != napi_undefined) { \
74 if (NapiUtils::HasNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_RESPONSE_CODE)) { \
75 uint32_t code = \
76 NapiUtils::GetUint32Property(env, params[0], HttpConstant::RESPONSE_KEY_RESPONSE_CODE); \
77 \
78 assert((code == (uint32_t)HttpModuleExports::ResponseCode::OK) == (ASSERT_CODE_OK)); \
79 } \
80 \
81 if (NapiUtils::HasNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_COOKIES)) { \
82 std::string cookies = \
83 NapiUtils::GetStringPropertyUtf8(env, params[0], HttpConstant::RESPONSE_KEY_COOKIES); \
84 \
85 NETSTACK_LOGI("cookies:\n%s", cookies.c_str()); \
86 } \
87 \
88 napi_value header; \
89 if (NapiUtils::HasNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_HEADER)) { \
90 header = \
91 OHOS::NetStack::NapiUtils::GetNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_HEADER); \
92 auto names = OHOS::NetStack::NapiUtils::GetPropertyNames(env, header); \
93 std::for_each(names.begin(), names.end(), [env, header](const std::string &name) { \
94 auto value = OHOS::NetStack::NapiUtils::GetStringPropertyUtf8(env, header, name); \
95 NETSTACK_LOGI("name = %s; value = %s", name.c_str(), value.c_str()); \
96 }); \
97 } else { \
98 NETSTACK_LOGI("##### Once Header Receive"); \
99 header = params[0]; \
100 auto names = OHOS::NetStack::NapiUtils::GetPropertyNames(env, header); \
101 NETSTACK_LOGI("names size = %zu", names.size()); \
102 std::for_each(names.begin(), names.end(), [env, header](const std::string &name) { \
103 auto value = OHOS::NetStack::NapiUtils::GetStringPropertyUtf8(env, header, name); \
104 NETSTACK_LOGI("Once Header name = %s; value = %s", name.c_str(), value.c_str()); \
105 }); \
106 } \
107 \
108 if (NapiUtils::HasNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_RESULT)) { \
109 napi_value result = NapiUtils::GetNamedProperty(env, params[0], HttpConstant::RESPONSE_KEY_RESULT); \
110 \
111 if (NapiUtils::GetValueType(env, result) == napi_string) { \
112 std::string data = OHOS::NetStack::NapiUtils::GetStringFromValueUtf8(env, result); \
113 NETSTACK_LOGI("data:\n%s", data.c_str()); \
114 } else if (NapiUtils::ValueIsArrayBuffer(env, result)) { \
115 size_t length = 0; \
116 void *data = NapiUtils::GetInfoFromArrayBufferValue(env, result, &length); \
117 FILE *fp = fopen("mine.png", "wb"); \
118 NETSTACK_LOGI("png size is is %zu", length); \
119 if (fp != nullptr) { \
120 fwrite(data, 1, length, fp); \
121 fclose(fp); \
122 } \
123 } \
124 } \
125 } \
126 \
127 NETSTACK_LOGI("\n\n\n"); \
128 return NapiUtils::GetUndefined(env); \
129 }
130
131 #define MAKE_CALLBACK_TWO_PARAM(FUNC_NAME, ASSERT_CODE_OK) \
132 [](napi_env env, napi_callback_info info) -> napi_value { \
133 NETSTACK_LOGI("%s", FUNC_NAME); \
134 \
135 napi_value thisVal = nullptr; \
136 size_t paramsCount = 2; \
137 napi_value params[2] = {nullptr}; \
138 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr)); \
139 \
140 bool typeRight = ((NapiUtils::GetValueType(env, params[0]) == napi_undefined && \
141 NapiUtils::GetValueType(env, params[1]) != napi_undefined) || \
142 (NapiUtils::GetValueType(env, params[0]) != napi_undefined && \
143 NapiUtils::GetValueType(env, params[1]) == napi_undefined)); \
144 assert(typeRight); \
145 \
146 if (NapiUtils::GetValueType(env, params[1]) != napi_undefined) { \
147 if (NapiUtils::HasNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_RESPONSE_CODE)) { \
148 uint32_t code = \
149 NapiUtils::GetUint32Property(env, params[1], HttpConstant::RESPONSE_KEY_RESPONSE_CODE); \
150 \
151 assert((code == (uint32_t)HttpModuleExports::ResponseCode::OK) == (ASSERT_CODE_OK)); \
152 } \
153 \
154 if (NapiUtils::HasNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_COOKIES)) { \
155 std::string cookies = \
156 NapiUtils::GetStringPropertyUtf8(env, params[1], HttpConstant::RESPONSE_KEY_COOKIES); \
157 \
158 NETSTACK_LOGI("cookies:\n%s", cookies.c_str()); \
159 } \
160 \
161 napi_value header; \
162 if (NapiUtils::HasNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_HEADER)) { \
163 header = \
164 OHOS::NetStack::NapiUtils::GetNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_HEADER); \
165 auto names = OHOS::NetStack::NapiUtils::GetPropertyNames(env, header); \
166 std::for_each(names.begin(), names.end(), [env, header](const std::string &name) { \
167 auto value = OHOS::NetStack::NapiUtils::GetStringPropertyUtf8(env, header, name); \
168 NETSTACK_LOGI("name = %s; value = %s", name.c_str(), value.c_str()); \
169 }); \
170 } else { \
171 NETSTACK_LOGI("##### On Header Receive"); \
172 header = params[1]; \
173 auto names = OHOS::NetStack::NapiUtils::GetPropertyNames(env, header); \
174 NETSTACK_LOGI("names size = %zu", names.size()); \
175 std::for_each(names.begin(), names.end(), [env, header](const std::string &name) { \
176 auto value = OHOS::NetStack::NapiUtils::GetStringPropertyUtf8(env, header, name); \
177 NETSTACK_LOGI("Header name = %s; value = %s", name.c_str(), value.c_str()); \
178 }); \
179 } \
180 \
181 if (NapiUtils::HasNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_RESULT)) { \
182 napi_value result = NapiUtils::GetNamedProperty(env, params[1], HttpConstant::RESPONSE_KEY_RESULT); \
183 \
184 if (NapiUtils::GetValueType(env, result) == napi_string) { \
185 std::string data = OHOS::NetStack::NapiUtils::GetStringFromValueUtf8(env, result); \
186 NETSTACK_LOGI("data:\n%s", data.c_str()); \
187 } else if (NapiUtils::ValueIsArrayBuffer(env, result)) { \
188 size_t length = 0; \
189 void *data = NapiUtils::GetInfoFromArrayBufferValue(env, result, &length); \
190 FILE *fp = fopen("mine.png", "wb"); \
191 NETSTACK_LOGI("png size is is %zu", length); \
192 if (fp != nullptr) { \
193 fwrite(data, 1, length, fp); \
194 fclose(fp); \
195 } \
196 } \
197 } \
198 } else if (NapiUtils::GetValueType(env, params[0]) != napi_undefined) { \
199 int32_t code = NapiUtils::GetInt32Property(env, params[0], "code"); \
200 NETSTACK_LOGI("error code = %d\n", code); \
201 } else { \
202 NETSTACK_LOGI("error is undefined"); \
203 } \
204 \
205 NETSTACK_LOGI("\n\n\n"); \
206 return NapiUtils::GetUndefined(env); \
207 }
208
209 namespace OHOS::NetStack {
210 static constexpr const int PARAM_TWO = 2;
211
CallOn(napi_env env,napi_value thisVal,napi_value callback)212 void CallOn(napi_env env, napi_value thisVal, napi_value callback)
213 {
214 napi_value func = NapiUtils::GetNamedProperty(env, thisVal, HttpModuleExports::HttpRequest::FUNCTION_ON);
215
216 napi_value argv[PARAM_TWO] = {NapiUtils::CreateStringUtf8(env, ON_HEADER_RECEIVE), callback};
217
218 NapiUtils::CallFunction(env, thisVal, func, PARAM_TWO, argv);
219 }
220
CallOnce(napi_env env,napi_value thisVal,napi_value callback)221 void CallOnce(napi_env env, napi_value thisVal, napi_value callback)
222 {
223 napi_value func = NapiUtils::GetNamedProperty(env, thisVal, HttpModuleExports::HttpRequest::FUNCTION_ONCE);
224
225 napi_value argv[PARAM_TWO] = {NapiUtils::CreateStringUtf8(env, ON_HEADER_RECEIVE), callback};
226
227 NapiUtils::CallFunction(env, thisVal, func, PARAM_TWO, argv);
228 }
229
CallOff(napi_env env,napi_value thisVal,napi_value callback)230 void CallOff(napi_env env, napi_value thisVal, napi_value callback)
231 {
232 napi_value func = NapiUtils::GetNamedProperty(env, thisVal, HttpModuleExports::HttpRequest::FUNCTION_OFF);
233
234 napi_value argv[PARAM_TWO] = {NapiUtils::CreateStringUtf8(env, ON_HEADER_RECEIVE), callback};
235
236 NapiUtils::CallFunction(env, thisVal, func, PARAM_TWO, argv);
237 }
238
CallPromiseThenCatch(napi_env env,napi_value promise,napi_value callback)239 void CallPromiseThenCatch(napi_env env, napi_value promise, napi_value callback)
240 {
241 bool isPromise = false;
242 napi_is_promise(env, promise, &isPromise);
243 if (!isPromise) {
244 return;
245 }
246
247 JSValue quickJsPromise = reinterpret_cast<QuickJSNativeValue *>(promise)->GetJsValue();
248
249 JSContext *ctx = ((QuickJSNativeEngine *)g_nativeEngine)->GetContext();
250
251 JSValue then = JS_GetPropertyStr(ctx, quickJsPromise, "then");
252 JSValue callbackValue = reinterpret_cast<QuickJSNativeValue *>(callback)->GetJsValue();
253 JS_Call(ctx, then, quickJsPromise, 1, &callbackValue);
254
255 JSValue theCatch = JS_GetPropertyStr(ctx, quickJsPromise, "catch");
256 JSValue callbackValueCatch = reinterpret_cast<QuickJSNativeValue *>(callback)->GetJsValue();
257 JS_Call(ctx, theCatch, quickJsPromise, 1, &callbackValueCatch);
258 }
259
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValue,true)260 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValue, true)
261 {
262 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP);
263
264 napi_value options = NapiUtils::CreateObject(env);
265
266 napi_value header = NapiUtils::CreateObject(env);
267 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
268 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
269
270 napi_value extraData = NapiUtils::CreateObject(env);
271 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "南京", "首府");
272 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "天气", "北京");
273
274 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
275 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
276
277 napi_value args[3] = {urlValue, options, callbackTwoParam};
278
279 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
280 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
281
282 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
283 ASSERT_VALUE_IS_PROMISE(env, retValue);
284 CallPromiseThenCatch(env, retValue, callbackOneParam);
285 }
286 DEFINE_TEST_END
287
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasPara,true)288 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasPara, true)
289 {
290 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
291
292 napi_value options = NapiUtils::CreateObject(env);
293
294 napi_value header = NapiUtils::CreateObject(env);
295 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
296 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
297
298 napi_value extraData = NapiUtils::CreateObject(env);
299 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "南京", "首府");
300 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "天气", "北京");
301
302 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
303 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
304
305 napi_value args[3] = {urlValue, options, callbackTwoParam};
306
307 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
308 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
309
310 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
311 ASSERT_VALUE_IS_PROMISE(env, retValue);
312 CallPromiseThenCatch(env, retValue, callbackOneParam);
313 }
314 DEFINE_TEST_END
315
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasParaNoEncode,true)316 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasParaNoEncode, true)
317 {
318 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
319
320 napi_value options = NapiUtils::CreateObject(env);
321
322 napi_value header = NapiUtils::CreateObject(env);
323 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
324 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
325
326 napi_value extraData = NapiUtils::CreateObject(env);
327 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "age", "199");
328 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "country", "China");
329
330 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
331 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
332
333 napi_value args[3] = {urlValue, options, callbackTwoParam};
334
335 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
336 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
337
338 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
339 ASSERT_VALUE_IS_PROMISE(env, retValue);
340 CallPromiseThenCatch(env, retValue, callbackOneParam);
341 }
342 DEFINE_TEST_END
343
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataString,true)344 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataString, true)
345 {
346 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
347
348 napi_value options = NapiUtils::CreateObject(env);
349
350 napi_value header = NapiUtils::CreateObject(env);
351 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
352 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
353
354 napi_value extraData = NapiUtils::CreateStringUtf8(env, "this-is-my-get-string=test&this-test=age");
355
356 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
357 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
358
359 napi_value args[3] = {urlValue, options, callbackTwoParam};
360
361 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
362 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
363
364 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
365 ASSERT_VALUE_IS_PROMISE(env, retValue);
366 CallPromiseThenCatch(env, retValue, callbackOneParam);
367 }
368 DEFINE_TEST_END
369
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetNoExtraData,true)370 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetNoExtraData, true)
371 {
372 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
373
374 napi_value options = NapiUtils::CreateObject(env);
375
376 napi_value header = NapiUtils::CreateObject(env);
377 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
378 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
379
380 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
381
382 napi_value args[3] = {urlValue, options, callbackTwoParam};
383
384 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
385 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
386
387 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
388 ASSERT_VALUE_IS_PROMISE(env, retValue);
389 CallPromiseThenCatch(env, retValue, callbackOneParam);
390 }
391 DEFINE_TEST_END
392
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNullString,true)393 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNullString, true)
394 {
395 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
396
397 napi_value options = NapiUtils::CreateObject(env);
398
399 napi_value header = NapiUtils::CreateObject(env);
400 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
401 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
402
403 napi_value extraData = NapiUtils::CreateStringUtf8(env, "");
404
405 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
406 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
407
408 napi_value args[3] = {urlValue, options, callbackTwoParam};
409
410 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
411 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
412
413 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
414 ASSERT_VALUE_IS_PROMISE(env, retValue);
415 CallPromiseThenCatch(env, retValue, callbackOneParam);
416 }
417 DEFINE_TEST_END
418
DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefaultHasPara,true)419 DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefaultHasPara, true)
420 {
421 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
422
423 napi_value options = NapiUtils::CreateObject(env);
424
425 napi_value extraData = NapiUtils::CreateObject(env);
426 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "age", "199");
427 NapiUtils::SetStringPropertyUtf8(env, (napi_value)extraData, "country", "China");
428
429 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
430
431 napi_value args[3] = {urlValue, options, callbackTwoParam};
432
433 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
434 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
435
436 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
437 ASSERT_VALUE_IS_PROMISE(env, retValue);
438 CallPromiseThenCatch(env, retValue, callbackOneParam);
439 }
440 DEFINE_TEST_END
441
DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefault,true)442 DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefault, true)
443 {
444 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
445
446 napi_value args[2] = {urlValue, callbackTwoParam};
447
448 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
449 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
450
451 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 1, args);
452 ASSERT_VALUE_IS_PROMISE(env, retValue);
453 CallPromiseThenCatch(env, retValue, callbackOneParam);
454 }
455 DEFINE_TEST_END
456
DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataString,true)457 DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataString, true)
458 {
459 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
460
461 napi_value options = NapiUtils::CreateObject(env);
462
463 napi_value header = NapiUtils::CreateObject(env);
464 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
465 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
466
467 napi_value extraData = NapiUtils::CreateStringUtf8(env, "Linux Http Test String");
468
469 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
470 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
471 NapiUtils::SetStringPropertyUtf8(env, options, std::string(HttpConstant::PARAM_KEY_METHOD),
472 std::string(HttpConstant::HTTP_METHOD_POST));
473
474 napi_value args[3] = {urlValue, options, callbackTwoParam};
475
476 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
477 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
478
479 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
480 ASSERT_VALUE_IS_PROMISE(env, retValue);
481 CallPromiseThenCatch(env, retValue, callbackOneParam);
482 }
483 DEFINE_TEST_END
484
DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderFail,true)485 DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderFail, true)
486 {
487 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP
488 ":7878"
489 "?name=test");
490
491 napi_value args[2] = {urlValue, callbackTwoParam};
492
493 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
494 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
495
496 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 1, args);
497 ASSERT_VALUE_IS_PROMISE(env, retValue);
498 CallPromiseThenCatch(env, retValue, callbackOneParam);
499 }
500 DEFINE_TEST_END
501
DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataArrayBuffer,true)502 DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataArrayBuffer, true)
503 {
504 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
505
506 napi_value options = NapiUtils::CreateObject(env);
507
508 napi_value header = NapiUtils::CreateObject(env);
509 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
510 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
511 NapiUtils::SetStringPropertyUtf8(env, header, std::string(HttpConstant::HTTP_CONTENT_TYPE), "mine-png");
512
513 FILE *fp = fopen("for-post.png", "rb");
514 fseek(fp, 0L, SEEK_END);
515 long size = ftell(fp);
516 void *buffer = malloc(size);
517 fseek(fp, 0, SEEK_SET);
518 size_t readSize = fread((void *)buffer, 1, size, fp);
519 NETSTACK_LOGI("file size = %ld read size = %zu", size, readSize);
520
521 void *data = nullptr;
522 napi_value extraData = NapiUtils::CreateArrayBuffer(env, readSize, &data);
523 memcpy_s(data, readSize, buffer, readSize);
524
525 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
526 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData);
527 NapiUtils::SetStringPropertyUtf8(env, options, std::string(HttpConstant::PARAM_KEY_METHOD),
528 std::string(HttpConstant::HTTP_METHOD_POST));
529
530 napi_value args[3] = {urlValue, options, callbackTwoParam};
531
532 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
533 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
534
535 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
536 ASSERT_VALUE_IS_PROMISE(env, retValue);
537 CallPromiseThenCatch(env, retValue, callbackOneParam);
538 }
539 DEFINE_TEST_END
540
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueHeaderKeyMultiCase,true)541 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueHeaderKeyMultiCase, true)
542 {
543 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
544
545 napi_value options = NapiUtils::CreateObject(env);
546
547 napi_value header = NapiUtils::CreateObject(env);
548 NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header");
549 NapiUtils::SetStringPropertyUtf8(env, header, "NO-USE", "Thanks");
550 NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header");
551
552 NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header);
553
554 napi_value args[3] = {urlValue, options, callbackTwoParam};
555
556 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 3, args);
557 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
558
559 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
560 ASSERT_VALUE_IS_PROMISE(env, retValue);
561 CallPromiseThenCatch(env, retValue, callbackOneParam);
562 }
563 DEFINE_TEST_END
564
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnce,true)565 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnce, true)
566 {
567 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
568
569 napi_value args[2] = {urlValue, callbackTwoParam};
570
571 CallOnce(env, httpRequestValue, callbackOneParam);
572
573 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
574 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
575
576 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 1, args);
577 ASSERT_VALUE_IS_PROMISE(env, retValue);
578 CallPromiseThenCatch(env, retValue, callbackOneParam);
579 }
580 DEFINE_TEST_END
581
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnOn,true)582 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnOn, true)
583 {
584 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
585
586 napi_value args[2] = {urlValue, callbackTwoParam};
587
588 CallOn(env, httpRequestValue, callbackTwoParam);
589
590 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
591 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
592
593 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 1, args);
594 ASSERT_VALUE_IS_PROMISE(env, retValue);
595 CallPromiseThenCatch(env, retValue, callbackOneParam);
596 }
597 DEFINE_TEST_END
598
DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOff,true)599 DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOff, true)
600 {
601 napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test");
602
603 napi_value args[2] = {urlValue, callbackTwoParam};
604
605 CallOn(env, httpRequestValue, callbackTwoParam);
606 CallOff(env, httpRequestValue, callbackTwoParam);
607
608 napi_value retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 2, args);
609 ASSERT_CHECK_VALUE_TYPE(env, retValue, napi_undefined);
610
611 retValue = NapiUtils::CallFunction(env, httpRequestValue, requestFunc, 1, args);
612 ASSERT_VALUE_IS_PROMISE(env, retValue);
613 CallPromiseThenCatch(env, retValue, callbackOneParam);
614 }
615 DEFINE_TEST_END
616 } // namespace OHOS::NetStack
617
main(int argc,char ** argv)618 int main(int argc, char **argv)
619 {
620 testing::GTEST_FLAG(output) = "xml:./";
621 testing::InitGoogleTest(&argc, argv);
622
623 JSRuntime *rt = JS_NewRuntime();
624
625 if (rt == nullptr) {
626 return 0;
627 }
628
629 JSContext *ctx = JS_NewContext(rt);
630 if (ctx == nullptr) {
631 return 0;
632 }
633
634 js_std_add_helpers(ctx, 0, nullptr);
635
636 g_nativeEngine = new QuickJSNativeEngine(rt, ctx, nullptr); // default instance id 0
637
638 int ret = RUN_ALL_TESTS();
639 (void)ret;
640
641 g_nativeEngine->Loop(LOOP_DEFAULT);
642
643 return 0;
644 }