• 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
16import fileio from '@ohos.fileio';
17import request from "@ohos.request";
18import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
19import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
20
21export default function requestUploadNormalJSUnit() {
22  describe('requestUploadNormalJSUnit', function () {
23    console.info('====>################################request upload Test start');
24
25    /**
26     * beforeAll: Prerequisites at the test suite level, which are executed before the test suite is executed.
27     */
28    beforeAll(async function (done) {
29      console.info('====>beforeAll: Prerequisites are executed.');
30      try {
31        console.info('beforeAll====>------------text.txt file generate start----------------');
32        let pathDir = globalThis.abilityContext.cacheDir;
33        let filePath = pathDir + `/test.txt`;
34        let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
35        let content = ''.padEnd(1 * 1024 *1024, 'h');
36        fileio.writeSync(fd, content);
37        fileio.closeSync(fd);
38        console.info('====>beforeAll: text.txt file generate success');
39        done();
40      } catch (err) {
41        console.info('====>beforeAll: abilityDelegator.executeShellCommand err or text.txt file generate failed: ' + err);
42        done();
43      }
44    });
45
46    let RequestData = {
47      name: 'name',
48      value: '123'
49    }
50    let File = {
51      filename: 'test',
52      name: 'test',
53      uri: 'internal://cache/test.txt',
54      type: 'txt'
55    }
56    let uploadConfig = {
57      url: 'http://127.0.0.1',
58      header: {
59        headers: 'http'
60      },
61      method: 'POST',
62      files: [File],
63      data: [RequestData]
64    };
65
66    /**
67     * beforeEach: Prerequisites at the test case level, which are executed before each test case is executed.
68     */
69    beforeEach(function () {
70      console.info('====>beforeEach: Prerequisites is executed.');
71    });
72
73    /**
74     * afterEach: Test case-level clearance conditions, which are executed after each test case is executed.
75     */
76    afterEach(function () {
77      console.info('====>afterEach: Test case-level clearance conditions is executed.');
78    });
79
80    /**
81     * afterAll: Test suite-level cleanup condition, which is executed after the test suite is executed.
82     */
83    afterAll(function () {
84      console.info('====>afterAll: Test suite-level cleanup condition is executed');
85    });
86
87    /**
88     * @tc.number SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010
89     * @tc.name SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010
90     * @tc.desc Starts a download session.
91     * @tc.size      : MEDIUM
92     * @tc.type      : Function
93     * @tc.level     : Level 2
94     */
95    it('SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010', 0, async function (done) {
96      console.info("-----------------------SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010 is starting-----------------------");
97      let attachments = [{
98        name: "uploadTest",
99        value: {
100          path: "./test.txt",
101          filename: "test.txt",
102          mimetype: "application/octet-stream"
103        }
104      }];
105
106      let config = {
107        action: request.agent.Action.UPLOAD,
108        url: 'http://192.168.217.58',
109        title: 'uploadTest',
110        description: 'Sample code for event listening',
111        mode: request.agent.Mode.FOREGROUND,
112        overwrite: true,
113        method: "POST",
114        data: attachments,
115      };
116
117      request.agent.create(globalThis.abilityContext, config, async (err, task) => {
118        console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010 downloadTask: " + task);
119        try {
120          if (err) {
121            console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010 create err: " + JSON.stringify(err));
122            expect(err.code).assertEqual(201);
123            done();
124          } else {
125            expect().assertFail();
126            console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010 create success: " + task);
127            done();
128          }
129        } catch (error) {
130          console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Callback_0010 fail: " + JSON.stringify(error));
131          done();
132        }
133      });
134    });
135
136    /**
137     * @tc.number SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010
138     * @tc.name SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010
139     * @tc.desc Starts a download session.
140     * @tc.size      : MEDIUM
141     * @tc.type      : Function
142     * @tc.level     : Level 2
143     */
144    it('SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010', 0, async function (done) {
145      console.info("-----------------------SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010 is starting-----------------------");
146      let attachments = [{
147        name: "uploadTest",
148        value: {
149          path: "./test.txt",
150          filename: "test.txt",
151          mimetype: "application/octet-stream"
152        }
153      }];
154
155      let config = {
156        action: request.agent.Action.UPLOAD,
157        url: 'http://192.168.217.58',
158        title: 'uploadTest',
159        description: 'Sample code for event listening',
160        mode: request.agent.Mode.FOREGROUND,
161        overwrite: true,
162        method: "POST",
163        data: attachments,
164      };
165
166      try {
167        var task = await request.agent.create(globalThis.abilityContext, config);
168        expect().assertFail();
169        console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010 create success: " + task);
170      } catch (err) {
171        console.info("====>SUB_Misc_REQUEST_Create_Promise_Upload_Promise_0010 catch error: " + JSON.stringify(err));
172        expect(err.code).assertEqual(201);
173        done();
174      }
175    });
176
177    /**
178     * @tc.number SUB_Misc_Request_UploadManagement_0001
179     * @tc.name SUB_Misc_Request_UploadManagement_0001
180     * @tc.desc Starts a download session.
181     * @tc.size      : MEDIUM
182     * @tc.type      : Function
183     * @tc.level     : Level 2
184     */
185    it('SUB_Misc_Request_UploadManagement_0001', 0, async (done: Function) => {
186      console.info("-----------------------SUB_Misc_Request_UploadManagement_0001 is starting-----------------------");
187      try {
188        await request.uploadFile(globalThis.abilityContext, uploadConfig);
189        expect().assertFail();
190        console.info("====>SUB_Misc_Request_UploadManagement_0001 uploadFile success");
191        done();
192      } catch (err) {
193        console.info("====>SUB_Misc_Request_UploadManagement_0001 create err: " + JSON.stringify(err));
194        expect(err.code).assertEqual(201);
195        done();
196      }
197    });
198
199    /**
200     * @tc.number SUB_Misc_Request_UploadManagement_Callback_0001
201     * @tc.name SUB_Misc_Request_UploadManagement_Callback_0001
202     * @tc.desc Starts a download session.
203     * @tc.size      : MEDIUM
204     * @tc.type      : Function
205     * @tc.level     : Level 2
206     */
207    it('SUB_Misc_Request_UploadManagement_Callback_0001', 0, async (done: Function) => {
208      console.info("-----------------------SUB_Misc_Request_UploadManagement_Callback_0001 is starting-----------------------");
209      try {
210        request.uploadFile(globalThis.abilityContext, uploadConfig, (err, uploadTask)=>{
211          if (err) {
212            console.info("====>SUB_Misc_Request_UploadManagement_Callback_0001 create err: " + JSON.stringify(err));
213            expect(err.code).assertEqual(201);
214            done();
215          } else {
216            console.error("====>SUB_Misc_Request_UploadManagement_Callback_0001 create success without permission");
217            expect().assertFail();
218            done();
219          }
220        })
221      } catch (err) {
222        console.error("====>SUB_Misc_Request_UploadManagement_Callback_0001 create err: " + JSON.stringify(err));
223        expect().assertFail();
224        done();
225      }
226    });
227
228  })
229}
230