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
16import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index';
17import request from '@ohos.request';
18
19const TAG = "REQUEST_TEST";
20let keyStr = 'download test ';
21
22describe('RequestTest', function () {
23    beforeAll(function () {
24        console.info('beforeAll called')
25    })
26
27    afterAll(function () {
28        console.info('afterAll called')
29    })
30
31    beforeEach(function () {
32        console.info('beforeEach called')
33    })
34
35    afterEach(function () {
36        console.info('afterEach called')
37    })
38    console.log(TAG + "*************Unit Test Begin*************");
39
40    let downloadTask;
41    let downloadConfig;
42    let globalDownloadConf = {
43        url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
44        header: {
45            headers: 'http'
46        },
47        enableMetered: false,
48        enableRoaming: false,
49        description: 'XTS download test!',
50        networkType: request.NETWORK_WIFI,
51        filePath: `/data/storage/el2/base/haps/entry/files/`,
52        title: 'XTS download test!',
53        background: false
54    };
55
56    /**
57     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001
58     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001
59     * @tc.desc      : alled when the current download session is in process.
60     * @tc.size      : MediumTest
61     * @tc.type      : Function
62     * @tc.level     : Level 2
63     */
64    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001', 0, async function (done) {
65        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
66        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 is starting-----------------------");
67        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001.txt'
68        request.download(downloadConfig, (err, data) => {
69            try {
70                downloadTask = data;
71                console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 downloadTask: " + JSON.stringify(downloadTask));
72                expect(true).assertEqual(downloadTask != undefined);
73                downloadTask.on('progress', (data1, data2) => {
74                    try {
75                        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 on data1 =" + data1);
76                        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 on data2 =" + data2);
77                        expect(true).assertEqual(data1 != undefined);
78                        expect(true).assertEqual(data2 != undefined);
79                        if (data1 == data2) {
80                            expect(true).assertTrue();
81                            downloadTask.remove()
82                            done();
83                        }
84                    } catch (err) {
85                        console.error("====>SUB_REQUEST_DOWNLOAD_API_PROMISE_0001 throw_error: " + err);
86                        done();
87                    }
88                });
89            } catch (err) {
90                console.error("====>SUB_REQUEST_DOWNLOAD_API_PROMISE_0001 error: " + err);
91                done();
92            }
93        });
94    });
95
96    /**
97     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002
98     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002
99     * @tc.desc       Called when the current download session complete、pause or remove.
100     * @tc.size      : MediumTest
101     * @tc.type      : Function
102     * @tc.level     : Level 2
103     */
104    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002', 0, async function (done) {
105        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
106        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 is starting-----------------------");
107        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002.txt'
108        request.download(downloadConfig, (err, data) => {
109            downloadTask = data;
110            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 downloadTask: " + downloadTask);
111            try {
112                expect(true).assertEqual(downloadTask != undefined);
113                downloadTask.on('complete', () => {
114                    try {
115                        console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 task completed.')
116                        expect(true).assertTrue();
117                    } catch (err) {
118                        console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 throw_error: " + err);
119                    }
120                    done();
121                });
122            } catch (err) {
123                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 error: " + err);
124                done();
125            }
126        });
127    });
128
129    /**
130     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003
131     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003
132     * @tc.desc      : Called when the current download session complete、pause or remove.
133     * @tc.size      : MediumTest
134     * @tc.type      : Function
135     * @tc.level     : Level 2
136     */
137    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003', 0, async function (done) {
138        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
139        downloadConfig.url = 'https://gitee.com/tiga-ultraman/downloadTests/releases/download/v1.01/large_test.zip';
140        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 is starting-----------------------");
141        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003.txt'
142        request.download(downloadConfig, (err, data) => {
143            downloadTask = data;
144            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 downloadTask: " + downloadTask);
145            try {
146                expect(true).assertEqual(downloadTask != undefined);
147                downloadTask.on('pause', () => {
148                    try {
149                        console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 task completed.')
150                        expect(true).assertTrue();
151                    } catch (err) {
152                        console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 throw_error: " + err);
153                    }
154                    downloadTask.remove()
155                    done();
156                });
157                downloadTask.on('progress', (data1, data2) => {
158                    if (data1 > 0) {
159                        downloadTask.pause()
160                    }
161                });
162            } catch (err) {
163                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 error: " + err);
164                done();
165            }
166        });
167    });
168
169    /**
170     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004
171     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004
172     * @tc.desc      : Called when the current download session complete、pause or remove.
173     * @tc.size      : MediumTest
174     * @tc.type      : Function
175     * @tc.level     : Level 2
176     */
177    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004', 0, async function (done) {
178        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
179        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 is starting-----------------------");
180        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004.txt'
181        request.download(downloadConfig, (err, data) => {
182            downloadTask = data;
183            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 downloadTask: " + downloadTask);
184            try {
185                expect(downloadTask != undefined).assertEqual(true);
186                downloadTask.on('remove', () => {
187                    try {
188                        console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 remove completed')
189                        expect(true).assertTrue();
190                    } catch (err) {
191                        console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 throw_error: " + err);
192                    }
193                    done();
194                });
195                downloadTask.on('progress', (data1, data2) => {
196                    if (data1 > 0) {
197                        downloadTask.remove()
198                    }
199                });
200            } catch (err) {
201                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 error: " + err);
202                done();
203            }
204        });
205    });
206
207    /**
208     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005
209     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005
210     * @tc.desc      : Called when the current download session fails.
211     * @tc.size      : MediumTest
212     * @tc.type      : Function
213     * @tc.level     : Level 2
214     */
215    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005', 0, async function (done) {
216        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
217        downloadConfig.url = 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.pngkfkew';
218        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 is starting-----------------------");
219        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005.txt'
220        downloadConfig.url += '123456'
221        request.download(downloadConfig, (err, data) => {
222            downloadTask = data;
223            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 downloadTask: " + downloadTask);
224            try {
225                expect(downloadTask != undefined).assertEqual(true);
226                downloadTask.on('fail', (err) => {
227                    try {
228                        console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 fail completed')
229                        expect(true).assertTrue();
230                    } catch (err) {
231                        console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 throw_error: " + err);
232                    }
233                    done();
234                })
235            } catch (err) {
236                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 error: " + err);
237                done();
238            }
239        });
240    });
241
242    /**
243     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001
244     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001
245     * @tc.desc      : alled when the current download session is in process.
246     * @tc.size      : MediumTest
247     * @tc.type      : Function
248     * @tc.level     : Level 2
249     */
250    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001', 0, async function (done) {
251        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
252        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 is starting-----------------------");
253        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001.txt'
254        request.download(downloadConfig, (err, data) => {
255            try {
256                downloadTask = data;
257                console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 downloadTask: " + downloadTask);
258                expect(downloadTask != undefined).assertEqual(true);
259                downloadTask.on('progress', async (data1, data2) => { });
260                downloadTask.off('progress');
261                done();
262            } catch (err) {
263                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 error: " + err);
264                done();
265            }
266        });
267    });
268
269    /**
270     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002
271     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002
272     * @tc.desc      : alled when the current download session complete、pause or remove.
273     * @tc.size      : MediumTest
274     * @tc.type      : Function
275     * @tc.level     : Level 2
276     */
277    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002', 0, async function (done) {
278        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
279        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 is starting-----------------------");
280        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002.txt'
281        request.download(downloadConfig, (err, data) => {
282            downloadTask = data;
283            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 downloadTask: " + downloadTask);
284            try {
285                expect(downloadTask != undefined).assertEqual(true);
286                downloadTask.on('complete', () => { });
287                downloadTask.off('complete');
288                done();
289            } catch (err) {
290                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 error: " + err);
291                done();
292            }
293        });
294    });
295
296    /**
297     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003
298     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003
299     * @tc.desc      : alled when the current download session complete、pause or remove.
300     * @tc.size      : MediumTest
301     * @tc.type      : Function
302     * @tc.level     : Level 2
303     */
304    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003', 0, async function (done) {
305        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
306        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 is starting-----------------------");
307        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003.txt'
308        request.download(downloadConfig, (err, data) => {
309            downloadTask = data;
310            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 downloadTask: " + downloadTask);
311            try {
312                expect(downloadTask != undefined).assertEqual(true);
313                function PauseCallback() { }
314                downloadTask.on('pause', PauseCallback);
315                downloadTask.off('pause', PauseCallback);
316                done();
317            } catch (err) {
318                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 error: " + err);
319                done();
320            }
321        });
322    });
323
324    /**
325     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004
326     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004
327     * @tc.desc      : alled when the current download session complete、pause or remove.
328     * @tc.size      : MediumTest
329     * @tc.type      : Function
330     * @tc.level     : Level 2
331     */
332    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004', 0, async function (done) {
333        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
334        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 is starting-----------------------");
335        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004.txt'
336        request.download(downloadConfig, (err, data) => {
337            downloadTask = data;
338            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 downloadTask: " + downloadTask);
339            try {
340                expect(downloadTask != undefined).assertEqual(true);
341                function RemoveCallback() { }
342                downloadTask.on('remove', RemoveCallback);
343                downloadTask.off('remove', RemoveCallback);
344                done();
345            } catch (err) {
346                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 error: " + err);
347                done();
348            }
349        });
350    });
351
352    /**
353     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005
354     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005
355     * @tc.desc      : Called when the current download session fails.
356     * @tc.size      : MediumTest
357     * @tc.type      : Function
358     * @tc.level     : Level 2
359     */
360    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005', 0, async function (done) {
361        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
362        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 is starting-----------------------");
363        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005.txt'
364        request.download(downloadConfig, (err, data) => {
365            downloadTask = data;
366            console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 downloadTask: " + downloadTask);
367            try {
368                expect(downloadTask != undefined).assertEqual(true);
369                function FailCallback(data) { }
370                downloadTask.on('fail', FailCallback);
371                downloadTask.off('fail', FailCallback);
372                done();
373            } catch (err) {
374                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 error: " + err);
375                done();
376            }
377        });
378    });
379
380    /**
381     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_REMOVE_0001
382     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_REMOVE_0001
383     * @tc.desc      : Deletes a download session and the downloaded files.
384     * @tc.size      : MediumTest
385     * @tc.type      : Function
386     * @tc.level     : Level 2
387     */
388    it('SUB_REQUEST_DOWNLOAD_API_REMOVE_0001', 0, async function (done) {
389        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
390        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 is starting-----------------------");
391        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMOVE_0001.txt'
392        request.download(downloadConfig, (err, data) => {
393            downloadTask = data;
394            console.info("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 downloadTask: " + downloadTask);
395            try {
396                expect(downloadTask != undefined).assertEqual(true);
397                downloadTask.remove((err, data) => {
398                    try {
399                        if (err) {
400                            console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 Failed to remove the download task.');
401                            expect().assertFail();
402                        }
403                        if (data) {
404                            console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 removed data:' + JSON.stringify(data));
405                            expect(data == true).assertTrue();
406                        } else {
407                            console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 Failed to remove the download task.');
408                            expect().assertFail();
409                        }
410                    } catch (err) {
411                        console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 remove_throw_err:' + JSON.stringify(err))
412                    }
413                    done();
414                });
415            } catch (err) {
416                console.error("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 error: " + err);
417                done();
418            }
419        })
420    });
421
422    /**
423     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_REMOVE_0002
424     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_REMOVE_0002
425     * @tc.desc      : Deletes a download session and the downloaded files.
426     * @tc.size      : MediumTest
427     * @tc.type      : Function
428     * @tc.level     : Level 2
429     */
430    it('SUB_REQUEST_DOWNLOAD_API_REMOVE_0002', 0, async function (done) {
431        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
432        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 is starting-----------------------");
433        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMOVE_0002.txt'
434        request.download(downloadConfig, (err, data) => {
435            downloadTask = data;
436            console.info("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 downloadTask: " + downloadTask);
437            try {
438                expect(downloadTask != undefined).assertEqual(true);
439                downloadTask.remove().then(data => {
440                    if (data) {
441                        console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 removed data:' + JSON.stringify(data));
442                        expect(data == true).assertTrue();
443                    } else {
444                        console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 Failed to remove the download task.');
445                        expect().assertFail();
446                    }
447                    done();
448                }).catch((err) => {
449                    console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 Failed to remove the download task.');
450                    done();
451                })
452            } catch (err) {
453                console.error("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 error: " + err);
454                done();
455            }
456        })
457    });
458
459    /**
460     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_PAUSE_0001
461     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_PAUSE_0001
462     * @tc.desc      : Pause a download session.
463     * @tc.size      : MediumTest
464     * @tc.type      : Function
465     * @tc.level     : Level 2
466     */
467    it('SUB_REQUEST_DOWNLOAD_API_PAUSE_0001', 0, async function (done) {
468        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
469        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 is starting-----------------------");
470        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_PAUSE_0001.txt'
471        request.download(downloadConfig, (err, data) => {
472            downloadTask = data;
473            console.info("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 downloadTask: " + downloadTask);
474            expect(downloadTask != undefined).assertEqual(true);
475            try {
476                downloadTask.pause(() => {
477                    try {
478                        console.info('====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 Download task pause success.');
479                        expect(true).assertTrue();
480                    } catch (err) {
481                        console.info('====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 pause_throw_err:' + JSON.stringify(err))
482                    }
483                    done();
484                })
485            } catch (err) {
486                console.error("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 error: " + JSON.stringify(err));
487                done();
488            }
489        })
490    });
491
492    /**
493     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_PAUSE_0002
494     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_PAUSE_0002
495     * @tc.desc      : Pause a download session.
496     * @tc.size      : MediumTest
497     * @tc.type      : Function
498     * @tc.level     : Level 2
499     */
500    it('SUB_REQUEST_DOWNLOAD_API_PAUSE_0002', 0, async function (done) {
501        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
502        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 is starting-----------------------");
503        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_PAUSE_0002.txt'
504        request.download(downloadConfig, (err, data) => {
505            downloadTask = data;
506            console.info("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 downloadTask: " + downloadTask);
507            try {
508                expect(downloadTask != undefined).assertEqual(true);
509                downloadTask.pause().then(() => {
510                    console.info('====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 Download task pause success.');
511                    expect(true).assertTrue();
512                    done();
513                }).catch((err) => {
514                    console.error("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 throw_error: " + JSON.stringify(err));
515                    done();
516                })
517            } catch (err) {
518                console.error("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 error: " + JSON.stringify(err));
519                done();
520            }
521        })
522    });
523
524    /**
525     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_REMUSE_0001
526     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_REMUSE_0001
527     * @tc.desc      : Resume a paused download session.
528     * @tc.size      : MediumTest
529     * @tc.type      : Function
530     * @tc.level     : Level 2
531     */
532    it('SUB_REQUEST_DOWNLOAD_API_REMUSE_0001', 0, async function (done) {
533        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
534        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 is starting-----------------------");
535        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMUSE_0001.txt'
536        request.download(downloadConfig, (err, data) => {
537            downloadTask = data;
538            console.info("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 downloadTask: " + downloadTask);
539            expect(downloadTask != undefined).assertEqual(true);
540            try {
541                downloadTask.resume(() => {
542                    try {
543                        console.info('====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 Download task resume success.');
544                        expect(true).assertTrue();
545                    } catch (err) {
546                        console.info('====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 pause_throw_err:' + JSON.stringify(err))
547                    }
548                    downloadTask.remove()
549                    done();
550                })
551            } catch (err) {
552                console.error("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 error: " + JSON.stringify(err));
553                done();
554            }
555        })
556    });
557
558    /**
559     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_REMUSE_0002
560     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_REMUSE_0002
561     * @tc.desc      : Resume a paused download session.
562     * @tc.size      : MediumTest
563     * @tc.type      : Function
564     * @tc.level     : Level 2
565     */
566    it('SUB_REQUEST_DOWNLOAD_API_REMUSE_0002', 0, async function (done) {
567        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
568        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 is starting-----------------------");
569        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMUSE_0002.txt'
570        request.download(downloadConfig, (err, data) => {
571            downloadTask = data;
572            console.info("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 downloadTask: " + downloadTask);
573            try {
574                expect(downloadTask != undefined).assertEqual(true);
575                downloadTask.resume().then(() => {
576                    console.info('====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 Download task resume success.');
577                    expect(true).assertTrue();
578                    downloadTask.remove()
579                    done()
580                }).catch((err) => {
581                    console.error("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 throw_error: " + JSON.stringify(err));
582                    done();
583                })
584            } catch (err) {
585                console.error("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 error: " + JSON.stringify(err));
586                done();
587            }
588        })
589    });
590
591    /**
592     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_QUERY_0001
593     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_QUERY_0001
594     * @tc.desc      : Queries download information of a session, which is defined in DownloadSession.DownloadInfo.
595     * @tc.size      : MediumTest
596     * @tc.type      : Function
597     * @tc.level     : Level 2
598     */
599    it('SUB_REQUEST_DOWNLOAD_API_QUERY_0001', 0, async function (done) {
600        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
601        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_QUERY_0001 is starting-----------------------");
602        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERY_0001.txt'
603        request.download(downloadConfig, (err, data) => {
604            downloadTask = data;
605            console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 downloadTask: " + downloadTask);
606            try {
607                expect(downloadTask != undefined).assertEqual(true);
608                downloadTask.query((err, downloadInfo) => {
609                    try {
610                        if (err) {
611                            console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 Failed to query: ' + JSON.stringify(err));
612                            expect().assertFail();
613                        } else {
614                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.description);
615                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.downloadedBytes);
616                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.downloadId);
617                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.failedReason);
618                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.fileName);
619                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.filePath);
620                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.pausedReason);
621                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.status);
622                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.targetURI);
623                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.downloadTitle);
624                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: ' + downloadInfo.downloadTotalBytes);
625                            expect(true).assertTrue();
626                        }
627                    } catch (err) {
628                        console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 throw_error: " + JSON.stringify(err));
629                    }
630                    done();
631                })
632            } catch (err) {
633                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 error: " + JSON.stringify(err));
634                done();
635            }
636        })
637    });
638
639    /**
640     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_QUERY_0002
641     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_QUERY_0002
642     * @tc.desc      : Queries download information of a session, which is defined in DownloadSession.DownloadInfo.
643     * @tc.size      : MediumTest
644     * @tc.type      : Function
645     * @tc.level     : Level 2
646     */
647    it('SUB_REQUEST_DOWNLOAD_API_QUERY_0002', 0, async function (done) {
648        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
649        console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_QUERY_0002 is starting-----------------------");
650        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERY_0002.txt'
651        request.download(downloadConfig, (err, data) => {
652            downloadTask = data;
653            console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 downloadTask: " + downloadTask);
654            try {
655                expect(downloadTask != undefined).assertEqual(true);
656                downloadTask.query().then((downloadInfo) => {
657                    try {
658                        if (err) {
659                            console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 Failed to query: ' + JSON.stringify(err));
660                            expect().assertFail();
661                        } else {
662                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.description);
663                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.downloadedBytes);
664                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.downloadId);
665                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.failedReason);
666                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.fileName);
667                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.filePath);
668                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.pausedReason);
669                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.status);
670                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.targetURI);
671                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.downloadTitle);
672                            console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: ' + downloadInfo.downloadTotalBytes);
673                            expect(true).assertTrue();
674                        }
675                        done();
676                    } catch (err) {
677                        console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 error: " + JSON.stringify(err));
678                        done();
679                    }
680                }).catch((err) => {
681                    console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002  catch_error: " + JSON.stringify(err));
682                    expect().assertFail();
683                    done();
684                })
685            } catch (err) {
686                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 error: " + JSON.stringify(err));
687                done();
688            }
689        })
690    });
691
692    /**
693     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001
694     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001
695     * @tc.desc      : Queries the MIME type of the download file.
696     * @tc.size      : MediumTest
697     * @tc.type      : Function
698     * @tc.level     : Level 2
699     */
700    it('SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001', 0, async function (done) {
701        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
702        console.info("====>---------------------SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 is starting---------------------");
703        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001.txt'
704        request.download(downloadConfig, (err, data) => {
705            downloadTask = data;
706            console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 downloadTask: " + downloadTask);
707            try {
708                expect(downloadTask != undefined).assertEqual(true);
709                downloadTask.on('complete', () => {
710                    try {
711                        downloadTask.queryMimeType((err, data) => {
712                            try {
713                                if (err) {
714                                    console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 Failed to queryMimeType the download task.');
715                                    expect().assertFail();
716                                }
717                                if (data) {
718                                    console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 data:' + JSON.stringify(data));
719                                    expect(typeof data == "string").assertTrue();
720                                } else {
721                                    console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 data error");
722                                    expect().assertFail();
723                                }
724                            } catch (err) {
725                                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 throw_error: " + JSON.stringify(err));
726                            }
727                            done();
728                        });
729                        console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 task completed.')
730                        expect(true).assertTrue();
731                    } catch (err) {
732                        console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 throw_error: " + err);
733                    }
734                    done();
735                });
736            } catch (err) {
737                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 err: " + JSON.stringify(err));
738                done();
739            }
740        })
741    });
742
743    /**
744     * @tc.number    : SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002
745     * @tc.name      : SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002
746     * @tc.desc      : Queries the MIME type of the download file.
747     * @tc.size      : MediumTest
748     * @tc.type      : Function
749     * @tc.level     : Level 2
750     */
751    it('SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002', 0, async function (done) {
752        downloadConfig = JSON.parse(JSON.stringify(globalDownloadConf));
753        console.info("====>-------------------SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 is starting----------------------");
754        downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002.txt'
755        request.download(downloadConfig, (err, data) => {
756            downloadTask = data;
757            console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 downloadTask: " + downloadTask);
758            try {
759                expect(downloadTask != undefined).assertEqual(true);
760                downloadTask.on('complete', () => {
761                    try {
762                        downloadTask.queryMimeType().then(data => {
763                            if (data) {
764                                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 Download task queryMimeType.');
765                                expect(typeof data == "string").assertTrue();
766                            } else {
767                                console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 Failed to queryMimeType the download task.');
768                                expect().assertFail();
769                            }
770                            done();
771                        }).catch((err) => {
772                            console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 throw_error: " + JSON.stringify(err));
773                            expect().assertFail();
774                            done();
775                        })
776                        console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 task completed.')
777                        expect(true).assertTrue();
778                    } catch (err) {
779                        console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 throw_error: " + err);
780                    }
781                    done();
782                });
783            } catch (err) {
784                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 error: " + JSON.stringify(err));
785                done();
786            }
787        })
788    });
789    console.log(TAG + "*************Unit Test End*************");
790})
791