1 /*
2 * Copyright (c) 2021 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 "../fetch_module.h"
17 #include "../http_request/http_constant.h"
18 #include "../http_request/http_request_utils.h"
19 #include "jerryscript-core.h"
20 #include "js_async_work.h"
21 #include "message_queue_utils.h"
22
23 #define FUNC_BEGIN() \
24 do { \
25 HTTP_REQUEST_INFO("%s BEGIN ##########", __FUNCTION__); \
26 } while (0)
27
28 #define FUNC_END_NO_NEW_LINE() \
29 do { \
30 HTTP_REQUEST_INFO("%s END ##########", __FUNCTION__); \
31 } while (0)
32
33 #define FUNC_END() \
34 do { \
35 HTTP_REQUEST_INFO("%s END ##########\n\n\n", __FUNCTION__); \
36 } while (0)
37
38 namespace OHOS {
39 namespace ACELite {
40 void InitFetchModule(JSIValue exports);
41 class JerryInitializer {
42 private:
43 int *temp;
44 JSIValue exports;
45
46 public:
JerryInitializer()47 JerryInitializer() : temp(nullptr), exports(nullptr) {}
Init()48 void Init()
49 {
50 temp = new int;
51 jerry_init(JERRY_INIT_EMPTY);
52 JsAsyncWork::SetAppQueueHandler(temp);
53 exports = JSI::CreateObject();
54 InitFetchModule(exports);
55 }
56
~JerryInitializer()57 ~JerryInitializer()
58 {
59 jerry_cleanup();
60 delete temp;
61 JSI::ReleaseValue(exports);
62 }
63 };
64
TestPutMessage(void * data)65 void TestPutMessage(void *data)
66 {
67 auto msg = static_cast<AbilityInnerMsg *>(data);
68 auto asyncWork = static_cast<AsyncWork *>(msg->data);
69 asyncWork->workHandler(asyncWork->data);
70 }
71
TestCallbackOnSuccess(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)72 JSIValue TestCallbackOnSuccess(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
73 {
74 FUNC_BEGIN();
75 (void)thisVal;
76 (void)argsNum;
77
78 JSIValue para = args[0];
79 HTTP_REQUEST_INFO("code = %d",
80 static_cast<int>(JSI::GetNumberProperty(para, HttpConstant::KEY_HTTP_RESPONSE_CODE)));
81
82 size_t size = 0;
83 char *data = JSI::GetStringPropertyWithBufferSize(para, HttpConstant::KEY_HTTP_RESPONSE_DATA, size);
84 std::string body;
85 for (uint32_t index = 0; index < size; ++index) {
86 if (data[index] != 0) {
87 body += data[index];
88 } else {
89 body += "0";
90 }
91 }
92 HTTP_REQUEST_INFO("%s", body.c_str());
93
94 JSIValue head = JSI::GetNamedProperty(para, HttpConstant::KEY_HTTP_RESPONSE_HEADERS);
95
96 JSIValue keys = JSI::GetObjectKeys(head);
97 uint32_t length = JSI::GetArrayLength(keys);
98 for (uint32_t i = 0; i < length; ++i) {
99 JSIValue k = JSI::GetPropertyByIndex(keys, i);
100 char *s = JSI::ValueToString(k);
101 char *v = JSI::GetStringProperty(head, s);
102 HTTP_REQUEST_INFO("%s ---------------- %s", s, v);
103 }
104
105 FUNC_END_NO_NEW_LINE();
106 return JSI::CreateUndefined();
107 }
108
TestCallbackOnFail(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)109 JSIValue TestCallbackOnFail(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
110 {
111 FUNC_BEGIN();
112 (void)thisVal;
113 (void)argsNum;
114
115 HTTP_REQUEST_INFO("err = %s", JSI::ValueToString(args[0]));
116 HTTP_REQUEST_INFO("code = %d", static_cast<int>(JSI::ValueToNumber(args[1])));
117
118 FUNC_END_NO_NEW_LINE();
119 return JSI::CreateUndefined();
120 }
121
TestCallbackOnComplete(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)122 JSIValue TestCallbackOnComplete(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
123 {
124 FUNC_BEGIN();
125 (void)thisVal;
126 (void)args;
127 (void)argsNum;
128
129 HTTP_REQUEST_INFO("request complete");
130
131 FUNC_END_NO_NEW_LINE();
132 return JSI::CreateUndefined();
133 }
134
TestHttpModuleMethodAndHeaderByDefault()135 void TestHttpModuleMethodAndHeaderByDefault()
136 {
137 FUNC_BEGIN();
138
139 JSIValue object = JSI::CreateObject();
140 if (object == nullptr) {
141 return;
142 }
143
144 JSIValue header = JSI::CreateObject();
145 JSI::SetStringProperty(header, "no-use", "test value");
146 JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_HEADER, header);
147
148 JSIValue url = JSI::CreateString("https://www.example.com");
149 JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_URL, url);
150
151 JSI::SetNamedProperty(object, CB_SUCCESS, JSI::CreateFunction(TestCallbackOnSuccess));
152 JSI::SetNamedProperty(object, CB_FAIL, JSI::CreateFunction(TestCallbackOnFail));
153 JSI::SetNamedProperty(object, CB_COMPLETE, JSI::CreateFunction(TestCallbackOnComplete));
154
155 JSIValue arg[1] = {object};
156 FetchModule::Fetch(nullptr, arg, 1);
157
158 FUNC_END();
159 }
160
161 } // namespace ACELite
162 } // namespace OHOS
163
main()164 int main()
165 {
166 OHOS::ACELite::JerryInitializer jerryInitializer;
167 jerryInitializer.Init();
168
169 OHOS::ACELite::TestHttpModuleMethodAndHeaderByDefault();
170
171 return 0;
172 }
173