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 {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'; 17import { agent } from '@ohos.request'; 18import featureAbility from '@ohos.ability.featureAbility' 19import fs from '@ohos.file.fs'; 20 21describe('RequestTaskTest', function () { 22 beforeAll(function () { 23 console.info('beforeAll called') 24 }) 25 26 afterAll(function () { 27 console.info('afterAll called') 28 }) 29 30 beforeEach(function () { 31 console.info('beforeEach called') 32 }) 33 34 afterEach(async function () { 35 console.info('afterEach called') 36 if (fs.accessSync(cacheDir + '/test.txt')) { 37 fs.unlinkSync(cacheDir + '/test.txt') 38 } 39 if (fs.accessSync(cacheDir + '/test.apk')) { 40 fs.unlinkSync(cacheDir + '/test.apk') 41 } 42 if (task !== undefined) { 43 await agent.remove(context, task.tid) 44 task = undefined 45 } 46 }) 47 48 function sleep(ms) { 49 return new Promise(resolve => setTimeout(resolve, ms)) 50 } 51 52 let task; 53 let context = featureAbility.getContext(); 54 let cacheDir = '/data/storage/el2/base/haps/entry/files'; 55 let fileSpec = { 56 path: `${cacheDir}/test.txt` 57 } 58 let formItem = [{ 59 name: 'test', 60 type: `${cacheDir}`, 61 value: [ fileSpec ] 62 }] 63 64 /** 65 * @tc.name: testTaskAction001 66 * @tc.desc: Test create task when lack action 67 * @tc.type: FUNC 68 * @tc.require: 69 */ 70 it('testTaskAction001', function (done) { 71 let conf = { 72 url: 'http://127.0.0.1', 73 } 74 try { 75 agent.create(conf, (err) => { 76 if (err) { 77 expect(err.code).assertEqual(401) 78 done() 79 } else { 80 expect(false).assertTrue(); 81 done() 82 } 83 }) 84 } catch (err) { 85 expect(err.code).assertEqual(401) 86 done() 87 } 88 }) 89 90 /** 91 * @tc.name: testTaskAction002 92 * @tc.desc: Test create task when action is string 93 * @tc.type: FUNC 94 * @tc.require: 95 */ 96 it('testTaskAction002', async function (done) { 97 let conf = { 98 action: 'UPLOAD', 99 url: 'http://127.0.0.1' 100 } 101 try { 102 agent.create(conf, (err) => { 103 if (err) { 104 expect(err.code).assertEqual(401) 105 done() 106 } else { 107 expect(false).assertTrue(); 108 done() 109 } 110 }) 111 } catch (err) { 112 expect(err.code).assertEqual(401) 113 done() 114 } 115 }) 116 117 /** 118 * @tc.name: testTaskAction003 119 * @tc.desc: Test create task when action is 2 120 * @tc.type: FUNC 121 * @tc.require: 122 */ 123 it('testTaskAction003', function (done) { 124 let conf = { 125 action: 2, 126 url: 'http://127.0.0.1' 127 } 128 agent.create(conf, (err) => { 129 if (err) { 130 expect(true).assertTrue() 131 done() 132 } else { 133 expect(false).assertTrue(); 134 done() 135 } 136 }) 137 }) 138 139 /** 140 * @tc.name: testTaskAction004 141 * @tc.desc: Test create task when action is UPLOAD 142 * @tc.type: FUNC 143 * @tc.require: 144 */ 145 it('testTaskAction004', function (done) { 146 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 147 fs.closeSync(file); 148 let conf = { 149 action: agent.Action.UPLOAD, 150 url: 'http://127.0.0.1', 151 data: formItem 152 } 153 agent.create(conf, async (err, data) => { 154 if (err) { 155 expect(false).assertTrue() 156 done() 157 } 158 expect(true).assertTrue() 159 done() 160 }) 161 }) 162 163 /** 164 * @tc.name: testTaskAction005 165 * @tc.desc: Test create task when action is DOWNLOAD 166 * @tc.type: FUNC 167 * @tc.require: 168 */ 169 it('testTaskAction005', function (done) { 170 let conf = { 171 action: agent.Action.DOWNLOAD, 172 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 173 saveas: `${cacheDir}` 174 } 175 agent.create(conf, async (err, data) => { 176 if (err) { 177 expect(false).assertTrue() 178 done() 179 } 180 data.on('completed', function (progress) { 181 if (fs.accessSync(`${cacheDir}/test.apk`)) { 182 expect(true).assertTrue() 183 done() 184 } 185 }) 186 }) 187 }) 188 189 /** 190 * @tc.name: testTaskUrl001 191 * @tc.desc: Test create task when lack url 192 * @tc.type: FUNC 193 * @tc.require: 194 */ 195 it('testTaskUrl001', function (done) { 196 let conf = { 197 action: agent.Action.DOWNLOAD, 198 } 199 try { 200 agent.create(conf, (err) => { 201 if (err) { 202 expect(err.code).assertEqual(401) 203 done() 204 } else { 205 expect(false).assertTrue(); 206 done() 207 } 208 }) 209 } catch (err) { 210 expect(err.code).assertEqual(401) 211 done() 212 } 213 }) 214 215 /** 216 * @tc.name: testTaskUrl002 217 * @tc.desc: Test create task when url is empty 218 * @tc.type: FUNC 219 * @tc.require: 220 */ 221 it('testTaskUrl002', function (done) { 222 let conf = { 223 action: agent.Action.DOWNLOAD, 224 url: '', 225 } 226 agent.create(conf, (err) => { 227 if (err) { 228 expect(true).assertTrue() 229 done() 230 } else { 231 expect(false).assertTrue(); 232 done() 233 } 234 }) 235 }) 236 237 /** 238 * @tc.name: testTaskUrl003 239 * @tc.desc: Test create task when url is not support download 240 * @tc.type: FUNC 241 * @tc.require: 242 */ 243 it('testTaskUrl003', function (done) { 244 let conf = { 245 action: agent.Action.DOWNLOAD, 246 url: 'https://gitee.com/openharmony/request_request', 247 } 248 agent.create(conf, (err) => { 249 if (err) { 250 expect(err.code).assertEqual(13400003) 251 done() 252 } else { 253 expect(false).assertTrue(); 254 done() 255 } 256 }) 257 }) 258 259 /** 260 * @tc.name: testTaskUrl004 261 * @tc.desc: Test create task when url is not support upload 262 * @tc.type: FUNC 263 * @tc.require: 264 */ 265 it('testTaskUrl004', function (done) { 266 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 267 fs.closeSync(file); 268 let conf = { 269 action: agent.Action.UPLOAD, 270 url: 'https://gitee.com/openharmony/request_request', 271 data: formItem 272 } 273 agent.create(conf, (err) => { 274 if (err) { 275 expect(err.code).assertEqual(13400003) 276 done() 277 } else { 278 expect(false).assertTrue(); 279 done() 280 } 281 }) 282 }) 283 284 /** 285 * @tc.name: testTaskTitle001 286 * @tc.desc: Test create task when title is given 287 * @tc.type: FUNC 288 * @tc.require: 289 */ 290 it('testTaskTitle001', async function (done) { 291 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 292 fs.closeSync(file); 293 let conf = { 294 action: agent.Action.UPLOAD, 295 url: 'http://127.0.0.1', 296 data: { 297 name: 'test', 298 value: { 299 path: `${cacheDir}/test.txt` 300 }, 301 }, 302 title: 'upload test.txt' 303 } 304 task = await agent.create(conf); 305 expect(task.title).assertEqual('upload test.txt') 306 done() 307 }) 308 309 /** 310 * @tc.name: testTaskTitle002 311 * @tc.desc: Test create task when title is number 312 * @tc.type: FUNC 313 * @tc.require: 314 */ 315 it('testTaskTitle002', async function (done) { 316 let conf = { 317 action: agent.Action.UPLOAD, 318 url: 'http://127.0.0.1', 319 data: { 320 name: 'test', 321 value: { 322 path: `${cacheDir}/test.txt` 323 }, 324 }, 325 title: 123 326 } 327 task = await agent.create(conf); 328 expect(task.title).assertEqual("") 329 done() 330 }) 331 332 /** 333 * @tc.name: testTaskDescription001 334 * @tc.desc: Test create task when description is given 335 * @tc.type: FUNC 336 * @tc.require: 337 */ 338 it('testTaskDescription001', async function (done) { 339 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 340 fs.closeSync(file); 341 let conf = { 342 action: agent.Action.UPLOAD, 343 url: 'http://127.0.0.1', 344 data: { 345 name: 'test', 346 value: { 347 path: `${cacheDir}/test.txt` 348 }, 349 }, 350 description: 'test upload' 351 } 352 task = await agent.create(conf); 353 expect(task.description).assertEqual('test upload') 354 expect(task.conf.mode).assertEqual(agent.Mode.BACKGROUND) 355 done() 356 }) 357 358 /** 359 * @tc.name: testTaskDescription002 360 * @tc.desc: Test create task when description is number 361 * @tc.type: FUNC 362 * @tc.require: 363 */ 364 it('testTaskDescription002', async function (done) { 365 let conf = { 366 action: agent.Action.UPLOAD, 367 url: 'http://127.0.0.1', 368 data: { 369 name: 'test', 370 value: { 371 path: `${cacheDir}/test.txt` 372 }, 373 }, 374 title: 123 375 } 376 task = await agent.create(conf); 377 expect(task.description).assertEqual("") 378 done() 379 }) 380 381 /** 382 * @tc.name: testTaskMode001 383 * @tc.desc: Test create task when mode is FRONTEND 384 * @tc.type: FUNC 385 * @tc.require: 386 */ 387 it('testTaskMode001', async function (done) { 388 let conf = { 389 action: agent.Action.UPLOAD, 390 url: 'http://127.0.0.1', 391 data: { 392 name: 'test', 393 value: { 394 path: `${cacheDir}/test.txt` 395 }, 396 }, 397 mode: agent.Mode.FRONTEND 398 } 399 task = await agent.create(conf); 400 task.start().then(() => { 401 agent.remove(context, task.tid) 402 expect(true).assertTrue() 403 done() 404 }).catch((err) => { 405 expect(false).assertTrue() 406 done() 407 }) 408 }) 409 410 /** 411 * @tc.name: testTaskMode002 412 * @tc.desc: Test create task when mode is BACKGROUND 413 * @tc.type: FUNC 414 * @tc.require: 415 */ 416 it('testTaskMode002', async function (done) { 417 let conf = { 418 action: agent.Action.UPLOAD, 419 url: 'http://127.0.0.1', 420 data: { 421 name: 'test', 422 value: { 423 path: `${cacheDir}/test.txt` 424 }, 425 }, 426 mode: agent.Mode.BACKGROUND 427 } 428 task = await agent.create(conf); 429 task.start().then(() => { 430 expect(true).assertTrue() 431 done() 432 }).catch((err) => { 433 expect(false).assertTrue() 434 done() 435 }) 436 }) 437 438 /** 439 * @tc.name: testTaskMode003 440 * @tc.desc: Test create task when mode is string 441 * @tc.type: FUNC 442 * @tc.require: 443 */ 444 it('testTaskMode003', async function (done) { 445 let conf = { 446 action: agent.Action.UPLOAD, 447 url: 'http://127.0.0.1', 448 data: { 449 name: 'test', 450 value: { 451 path: `${cacheDir}/test.txt` 452 }, 453 }, 454 mode: "BACKGROUND" 455 } 456 task = await agent.create(conf); 457 task.start().then(() => { 458 expect(task.conf.mode).assertEqual(agent.Mode.BACKGROUND) 459 done() 460 }).catch((err) => { 461 expect(false).assertTrue() 462 done() 463 }) 464 }) 465 466 /** 467 * @tc.name: testTaskCover001 468 * @tc.desc: Test create task when cover is true and file exists 469 * @tc.type: FUNC 470 * @tc.require: 471 */ 472 it('testTaskCover001', async function (done) { 473 let file = fs.openSync(cacheDir + '/test.apk', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 474 fs.closeSync(file); 475 let conf = { 476 action: agent.Action.DOWNLOAD, 477 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 478 data: { 479 name: 'test', 480 value: { 481 path: `${cacheDir}/test.apk` 482 }, 483 }, 484 cover: true 485 } 486 task = await agent.create(conf); 487 task.start().then(() => { 488 expect(true).assertTrue() 489 done() 490 }).catch((err) => { 491 expect(false).assertTrue() 492 done() 493 }) 494 }) 495 496 /** 497 * @tc.name: testTaskCover002 498 * @tc.desc: Test create task when cover is true and file not exists 499 * @tc.type: FUNC 500 * @tc.require: 501 */ 502 it('testTaskCover002', async function (done) { 503 let conf = { 504 action: agent.Action.DOWNLOAD, 505 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 506 data: { 507 name: 'test', 508 value: { 509 path: `${cacheDir}/test.apk` 510 }, 511 }, 512 cover: true 513 } 514 task = await agent.create(conf); 515 task.start().then(() => { 516 expect(true).assertTrue() 517 done() 518 }).catch((err) => { 519 expect(false).assertTrue() 520 done() 521 }) 522 }) 523 524 /** 525 * @tc.name: testTaskCover003 526 * @tc.desc: Test create task when cover is false and file exists 527 * @tc.type: FUNC 528 * @tc.require: 529 */ 530 it('testTaskCover003', async function (done) { 531 let file = fs.openSync(cacheDir + '/test.apk', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 532 fs.closeSync(file); 533 let conf = { 534 action: agent.Action.DOWNLOAD, 535 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 536 data: { 537 name: 'test', 538 value: { 539 path: `${cacheDir}/test.apk` 540 }, 541 }, 542 cover: false 543 } 544 task = await agent.create(conf); 545 task.start().then(() => { 546 expect(true).assertTrue() 547 done() 548 }).catch((err) => { 549 expect(false).assertTrue() 550 done() 551 }) 552 }) 553 554 /** 555 * @tc.name: testTaskCover004 556 * @tc.desc: Test create task when cover is false and file not exists 557 * @tc.type: FUNC 558 * @tc.require: 559 */ 560 it('testTaskCover004', async function (done) { 561 let conf = { 562 action: agent.Action.DOWNLOAD, 563 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 564 data: { 565 name: 'test', 566 value: { 567 path: `${cacheDir}/test.apk` 568 }, 569 }, 570 cover: false 571 } 572 task = await agent.create(conf); 573 task.start().then(() => { 574 expect(true).assertTrue() 575 done() 576 }).catch((err) => { 577 expect(false).assertTrue() 578 done() 579 }) 580 }) 581 582 /** 583 * @tc.name: testTaskCover005 584 * @tc.desc: Test create task when cover is string 585 * @tc.type: FUNC 586 * @tc.require: 587 */ 588 it('testTaskCover005', async function (done) { 589 let conf = { 590 action: agent.Action.DOWNLOAD, 591 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 592 data: { 593 name: 'test', 594 value: { 595 path: `${cacheDir}/test.apk` 596 }, 597 }, 598 cover: "true" 599 } 600 task = await agent.create(conf); 601 task.start().then(() => { 602 expect(task.conf.cover).assertEqual(true) 603 done() 604 }).catch((err) => { 605 expect(false).assertTrue() 606 done() 607 }) 608 }) 609 610 /** 611 * @tc.name: testTaskMethod001 612 * @tc.desc: Test create task when method is POST for upload 613 * @tc.type: FUNC 614 * @tc.require: 615 */ 616 it('testTaskMethod001', async function (done) { 617 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 618 fs.closeSync(file); 619 let conf = { 620 action: agent.Action.UPLOAD, 621 url: 'http://127.0.0.1', 622 data: { 623 name: 'test', 624 value: { 625 path: `${cacheDir}/test.txt` 626 }, 627 }, 628 method: 'POST' 629 } 630 task = await agent.create(conf); 631 task.start().then(() => { 632 expect(true).assertTrue() 633 done() 634 }).catch((err) => { 635 expect(false).assertTrue() 636 done() 637 }) 638 }) 639 640 /** 641 * @tc.name: testTaskMethod002 642 * @tc.desc: Test create task when method is POST for download 643 * @tc.type: FUNC 644 * @tc.require: 645 */ 646 it('testTaskMethod002', async function (done) { 647 let conf = { 648 action: agent.Action.DOWNLOAD, 649 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 650 saveas: `${cacheDir}`, 651 method: 'POST' 652 } 653 task = await agent.create(conf); 654 task.start().then(() => { 655 expect(true).assertTrue() 656 done() 657 }).catch((err) => { 658 expect(false).assertTrue() 659 done() 660 }) 661 }) 662 663 /** 664 * @tc.name: testTaskMethod003 665 * @tc.desc: Test create task when method is number 666 * @tc.type: FUNC 667 * @tc.require: 668 */ 669 it('testTaskMethod003', async function (done) { 670 let conf = { 671 action: agent.Action.DOWNLOAD, 672 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 673 saveas: `${cacheDir}`, 674 method: 123 675 } 676 task = await agent.create(conf); 677 task.start().then(() => { 678 expect(task.conf.method).assertEqual('GET') 679 done() 680 }).catch((err) => { 681 expect(false).assertTrue() 682 done() 683 }) 684 }) 685 686 /** 687 * @tc.name: testTaskMethod004 688 * @tc.desc: Test create task when method is empty 689 * @tc.type: FUNC 690 * @tc.require: 691 */ 692 it('testTaskMethod004', async function (done) { 693 let conf = { 694 action: agent.Action.DOWNLOAD, 695 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 696 saveas: `${cacheDir}`, 697 method: '' 698 } 699 task = await agent.create(conf); 700 task.start().then(() => { 701 expect(task.conf.method).assertEqual('GET') 702 done() 703 }).catch((err) => { 704 expect(false).assertTrue() 705 done() 706 }) 707 }) 708 709 /** 710 * @tc.name: testTaskMethod005 711 * @tc.desc: Test create task when method is GET for upload 712 * @tc.type: FUNC 713 * @tc.require: 714 */ 715 it('testTaskMethod005', async function (done) { 716 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 717 fs.closeSync(file); 718 let conf = { 719 action: agent.Action.UPLOAD, 720 url: 'http://127.0.0.1', 721 data: { 722 name: 'test', 723 value: { 724 path: `${cacheDir}/test.txt` 725 }, 726 }, 727 method: 'GET' 728 } 729 task = await agent.create(conf); 730 task.start().then(() => { 731 expect(true).assertTrue() 732 done() 733 }).catch((err) => { 734 expect(false).assertTrue() 735 done() 736 }) 737 }) 738 739 /** 740 * @tc.name: testTaskMethod006 741 * @tc.desc: Test create task when method is PUT for download 742 * @tc.type: FUNC 743 * @tc.require: 744 */ 745 it('testTaskMethod006', async function (done) { 746 let conf = { 747 action: agent.Action.DOWNLOAD, 748 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 749 saveas: `${cacheDir}`, 750 method: 'PUT' 751 } 752 task = await agent.create(conf); 753 task.start().then(() => { 754 expect(true).assertTrue() 755 done() 756 }).catch((err) => { 757 expect(false).assertTrue() 758 done() 759 }) 760 }) 761 762 /** 763 * @tc.name: testTaskHeaders001 764 * @tc.desc: Test create task when headers content-type is application/json but data is file for upload 765 * @tc.type: FUNC 766 * @tc.require: 767 */ 768 it('testTaskHeaders001', async function (done) { 769 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 770 fs.closeSync(file); 771 let conf = { 772 action: agent.Action.UPLOAD, 773 url: 'http://127.0.0.1', 774 data: { 775 name: 'test', 776 value: { 777 path: `${cacheDir}/test.txt` 778 }, 779 }, 780 headers: JSON.stringify({'content-type': 'application/json'}), 781 } 782 task = await agent.create(conf); 783 task.start().then(() => { 784 expect(true).assertTrue() 785 done() 786 }).catch((err) => { 787 expect(false).assertTrue() 788 done() 789 }) 790 }) 791 792 /** 793 * @tc.name: testTaskHeaders002 794 * @tc.desc: Test create task when lack headers for upload 795 * @tc.type: FUNC 796 * @tc.require: 797 */ 798 it('testTaskHeaders002', async function (done) { 799 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 800 fs.closeSync(file); 801 let conf = { 802 action: agent.Action.UPLOAD, 803 url: 'http://127.0.0.1', 804 data: { 805 name: 'test', 806 value: { 807 path: `${cacheDir}/test.txt` 808 }, 809 }, 810 } 811 task = await agent.create(conf); 812 task.start().then(() => { 813 expect(task.conf.headers).assertEqual('multipart/form-data') 814 done() 815 }).catch((err) => { 816 expect(false).assertTrue() 817 done() 818 }) 819 }) 820 821 /** 822 * @tc.name: testTaskHeaders003 823 * @tc.desc: Test create task when lack headers for download 824 * @tc.type: FUNC 825 * @tc.require: 826 */ 827 it('testTaskHeaders003', async function (done) { 828 let conf = { 829 action: agent.Action.DOWNLOAD, 830 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 831 } 832 task = await agent.create(conf); 833 task.start().then(() => { 834 expect(task.conf.headers).assertEqual('application/json') 835 done() 836 }).catch((err) => { 837 expect(false).assertTrue() 838 done() 839 }) 840 }) 841 842 /** 843 * @tc.name: testTaskSaveas001 844 * @tc.desc: Test create task when lack saveas is number for download 845 * @tc.type: FUNC 846 * @tc.require: 847 */ 848 it('testTaskSaveas001', async function (done) { 849 let conf = { 850 action: agent.Action.DOWNLOAD, 851 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 852 saveas: 123 853 } 854 task = await agent.create(conf); 855 task.on('completed', function() { 856 if (fs.accessSync(`${cacheDir}/test.apk`)) { 857 expect(true).assertTrue() 858 done() 859 } 860 }) 861 task.start() 862 }) 863 864 /** 865 * @tc.name: testTaskData001 866 * @tc.desc: Test create task when data lack name 867 * @tc.type: FUNC 868 * @tc.require: 869 */ 870 it('testTaskData001', function (done) { 871 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 872 fs.closeSync(file); 873 let conf = { 874 action: agent.Action.UPLOAD, 875 url: 'http://127.0.0.1', 876 data: { 877 value: { 878 path: `${cacheDir}/test.txt` 879 }, 880 } 881 } 882 try { 883 agent.create(conf, (err) => { 884 if (err) { 885 expect(err.code).assertEqual(401) 886 done() 887 } else { 888 expect(false).assertTrue(); 889 done() 890 } 891 }) 892 } catch (err) { 893 expect(err.code).assertEqual(401) 894 done() 895 } 896 }) 897 898 /** 899 * @tc.name: testTaskData002 900 * @tc.desc: Test create task when data name is number 901 * @tc.type: FUNC 902 * @tc.require: 903 */ 904 it('testTaskData002', function (done) { 905 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 906 fs.closeSync(file); 907 let conf = { 908 action: agent.Action.UPLOAD, 909 url: 'http://127.0.0.1', 910 data: { 911 name: 123, 912 value: { 913 path: `${cacheDir}/test.txt` 914 }, 915 } 916 } 917 try { 918 agent.create(conf, (err) => { 919 if (err) { 920 expect(err.code).assertEqual(401) 921 done() 922 } else { 923 expect(false).assertTrue(); 924 done() 925 } 926 }) 927 } catch (err) { 928 expect(err.code).assertEqual(401) 929 done() 930 } 931 }) 932 933 /** 934 * @tc.name: testTaskData003 935 * @tc.desc: Test create task when data lack value 936 * @tc.type: FUNC 937 * @tc.require: 938 */ 939 it('testTaskData003', function (done) { 940 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 941 fs.closeSync(file); 942 let conf = { 943 action: agent.Action.UPLOAD, 944 url: 'http://127.0.0.1', 945 data: { 946 name: 'test' 947 } 948 } 949 try { 950 agent.create(conf, (err) => { 951 if (err) { 952 expect(err.code).assertEqual(401) 953 done() 954 } else { 955 expect(false).assertTrue(); 956 done() 957 } 958 }) 959 } catch (err) { 960 expect(err.code).assertEqual(401) 961 done() 962 } 963 }) 964 965 /** 966 * @tc.name: testTaskData004 967 * @tc.desc: Test create task when data value is number 968 * @tc.type: FUNC 969 * @tc.require: 970 */ 971 it('testTaskData004', function (done) { 972 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 973 fs.closeSync(file); 974 let conf = { 975 action: agent.Action.UPLOAD, 976 url: 'http://127.0.0.1', 977 data: { 978 name: 'test', 979 value: 123 980 } 981 } 982 try { 983 agent.create(conf, (err) => { 984 if (err) { 985 expect(err.code).assertEqual(401) 986 done() 987 } else { 988 expect(false).assertTrue(); 989 done() 990 } 991 }) 992 } catch (err) { 993 expect(err.code).assertEqual(401) 994 done() 995 } 996 }) 997 998 /** 999 * @tc.name: testTaskData005 1000 * @tc.desc: Test create task when data path is '', path is not exits 1001 * @tc.type: FUNC 1002 * @tc.require: 1003 */ 1004 it('testTaskData005', function (done) { 1005 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1006 fs.closeSync(file); 1007 let conf = { 1008 action: agent.Action.UPLOAD, 1009 url: 'http://127.0.0.1', 1010 data: { 1011 name: 'test', 1012 value: { 1013 path: '' 1014 } 1015 } 1016 } 1017 try { 1018 agent.create(conf, (err) => { 1019 if (err) { 1020 expect(err.code).assertEqual(401) 1021 done() 1022 } else { 1023 expect(false).assertTrue(); 1024 done() 1025 } 1026 }) 1027 } catch (err) { 1028 expect(err.code).assertEqual(401) 1029 done() 1030 } 1031 }) 1032 1033 /** 1034 * @tc.name: testTaskData006 1035 * @tc.desc: Test create task when data path is number 1036 * @tc.type: FUNC 1037 * @tc.require: 1038 */ 1039 it('testTaskData006', function (done) { 1040 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1041 fs.closeSync(file); 1042 let conf = { 1043 action: agent.Action.UPLOAD, 1044 url: 'http://127.0.0.1', 1045 data: { 1046 name: 'test', 1047 value: { 1048 path: 123 1049 } 1050 } 1051 } 1052 try { 1053 agent.create(conf, (err) => { 1054 if (err) { 1055 expect(err.code).assertEqual(401) 1056 done() 1057 } else { 1058 expect(false).assertTrue(); 1059 done() 1060 } 1061 }) 1062 } catch (err) { 1063 expect(err.code).assertEqual(401) 1064 done() 1065 } 1066 }) 1067 1068 1069 /** 1070 * @tc.name: testTaskData007 1071 * @tc.desc: Test create task when data path is not access permission 1072 * @tc.type: FUNC 1073 * @tc.require: 1074 */ 1075 it('testTaskData007', function (done) { 1076 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1077 fs.closeSync(file); 1078 let conf = { 1079 action: agent.Action.UPLOAD, 1080 url: 'http://127.0.0.1', 1081 data: { 1082 name: 'test', 1083 value: { 1084 path: 'system/etc/init.cfg' 1085 } 1086 } 1087 } 1088 agent.create(conf, (err) => { 1089 if (err) { 1090 expect(err.code).assertEqual(13400001) 1091 done() 1092 } else { 1093 expect(false).assertTrue(); 1094 done() 1095 } 1096 }) 1097 }) 1098 1099 /** 1100 * @tc.name: testTaskData008 1101 * @tc.desc: Test create task when data filename is number 1102 * @tc.type: FUNC 1103 * @tc.require: 1104 */ 1105 it('testTaskData008', function (done) { 1106 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1107 fs.closeSync(file); 1108 let conf = { 1109 action: agent.Action.UPLOAD, 1110 url: 'http://127.0.0.1', 1111 data: { 1112 name: 'test', 1113 value: { 1114 path: `${cacheDir}/test.txt`, 1115 filename: 123 1116 } 1117 } 1118 } 1119 agent.create(conf, (err) => { 1120 if (err) { 1121 expect(false).assertTrue() 1122 done() 1123 } else { 1124 expect(true).assertTrue(); 1125 done() 1126 } 1127 }) 1128 }) 1129 1130 1131 /** 1132 * @tc.name: testTaskData009 1133 * @tc.desc: Test create task when data mimetype is number 1134 * @tc.type: FUNC 1135 * @tc.require: 1136 */ 1137 it('testTaskData009', function (done) { 1138 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1139 fs.closeSync(file); 1140 let conf = { 1141 action: agent.Action.UPLOAD, 1142 url: 'http://127.0.0.1', 1143 data: { 1144 name: 'test', 1145 value: { 1146 path: `${cacheDir}/test.txt`, 1147 mimetype: 123 1148 } 1149 } 1150 } 1151 agent.create(conf, (err) => { 1152 if (err) { 1153 expect(false).assertTrue() 1154 done() 1155 } else { 1156 expect(true).assertTrue(); 1157 done() 1158 } 1159 }) 1160 }) 1161 1162 /** 1163 * @tc.name: testTaskData010 1164 * @tc.desc: Test create task when data path and filename is different 1165 * @tc.type: FUNC 1166 * @tc.require: 1167 */ 1168 it('testTaskData010', function (done) { 1169 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1170 fs.closeSync(file); 1171 let conf = { 1172 action: agent.Action.UPLOAD, 1173 url: 'http://127.0.0.1', 1174 data: { 1175 name: 'test', 1176 value: { 1177 path: `${cacheDir}/test.txt`, 1178 filename: 'a.txt' 1179 } 1180 } 1181 } 1182 agent.create(conf, (err) => { 1183 if (err) { 1184 expect(false).assertTrue() 1185 done() 1186 } else { 1187 expect(true).assertTrue(); 1188 done() 1189 } 1190 }) 1191 }) 1192 1193 /** 1194 * @tc.name: testTaskData011 1195 * @tc.desc: Test create task when data two files for upload 1196 * @tc.type: FUNC 1197 * @tc.require: 1198 */ 1199 it('testTaskData011', function (done) { 1200 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1201 fs.closeSync(file); 1202 let conf = { 1203 action: agent.Action.UPLOAD, 1204 url: 'http://127.0.0.1', 1205 data: { 1206 name: 'test', 1207 value: [ 1208 { 1209 path: `${cacheDir}/test.txt`, 1210 }, 1211 { 1212 path: `${cacheDir}/test.txt`, 1213 }, 1214 ] 1215 } 1216 } 1217 agent.create(conf, (err) => { 1218 if (err) { 1219 expect(false).assertTrue() 1220 done() 1221 } else { 1222 expect(true).assertTrue(); 1223 done() 1224 } 1225 }) 1226 }) 1227 1228 /** 1229 * @tc.name: testTaskData012 1230 * @tc.desc: Test create task when data value is string 1231 * @tc.type: FUNC 1232 * @tc.require: 1233 */ 1234 it('testTaskData012', function (done) { 1235 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1236 fs.closeSync(file); 1237 let conf = { 1238 action: agent.Action.UPLOAD, 1239 url: 'http://127.0.0.1', 1240 data: { 1241 name: 'test', 1242 value: 'test' 1243 } 1244 } 1245 agent.create(conf, (err) => { 1246 if (err) { 1247 expect(false).assertTrue() 1248 done() 1249 } else { 1250 expect(true).assertTrue(); 1251 done() 1252 } 1253 }) 1254 }) 1255 1256 /** 1257 * @tc.name: testTaskData013 1258 * @tc.desc: Test create task when data path and filename is same 1259 * @tc.type: FUNC 1260 * @tc.require: 1261 */ 1262 it('testTaskData013', function (done) { 1263 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1264 fs.closeSync(file); 1265 let conf = { 1266 action: agent.Action.UPLOAD, 1267 url: 'http://127.0.0.1', 1268 data: { 1269 name: 'test', 1270 value: { 1271 path: `${cacheDir}/test.txt`, 1272 filename: 'test.txt' 1273 } 1274 } 1275 } 1276 agent.create(conf, (err) => { 1277 if (err) { 1278 expect(false).assertTrue() 1279 done() 1280 } else { 1281 expect(true).assertTrue(); 1282 done() 1283 } 1284 }) 1285 }) 1286 1287 /** 1288 * @tc.name: testTaskNetwork001 1289 * @tc.desc: Test create task when network is 3 1290 * @tc.type: FUNC 1291 * @tc.require: 1292 */ 1293 it('testTaskNetwork001', async function (done) { 1294 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1295 fs.closeSync(file); 1296 let conf = { 1297 action: agent.Action.UPLOAD, 1298 url: 'http://127.0.0.1', 1299 data: { 1300 name: 'test', 1301 value: { 1302 path: `${cacheDir}/test.txt`, 1303 } 1304 }, 1305 network: 3 1306 } 1307 task.create(context, conf).then(() => { 1308 expect(true).assertTrue() 1309 done() 1310 }).catch((err) => { 1311 expect(false).assertTrue() 1312 done() 1313 }) 1314 }) 1315 1316 /** 1317 * @tc.name: testTaskNetwork002 1318 * @tc.desc: Test create task when network is string 1319 * @tc.type: FUNC 1320 * @tc.require: 1321 */ 1322 it('testTaskNetwork002', async function (done) { 1323 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1324 fs.closeSync(file); 1325 let conf = { 1326 action: agent.Action.UPLOAD, 1327 url: 'http://127.0.0.1', 1328 data: { 1329 name: 'test', 1330 value: { 1331 path: `${cacheDir}/test.txt`, 1332 } 1333 }, 1334 network: "ANY" 1335 } 1336 task.create(context, conf).then(() => { 1337 expect(true).assertTrue() 1338 done() 1339 }).catch((err) => { 1340 expect(false).assertTrue() 1341 done() 1342 }) 1343 }) 1344 1345 /** 1346 * @tc.name: testTaskNetwork003 1347 * @tc.desc: Test create task when network is WIFI for DOWNLOAD 1348 * @tc.type: FUNC 1349 * @tc.require: 1350 */ 1351 it('testTaskNetwork003', async function (done) { 1352 let conf = { 1353 action: agent.Action.DOWNLOAD, 1354 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 1355 network: agent.NetWork.WIFI 1356 } 1357 task.create(context, conf).then(() => { 1358 expect(true).assertTrue() 1359 done() 1360 }).catch((err) => { 1361 expect(false).assertTrue() 1362 done() 1363 }) 1364 }) 1365 1366 /** 1367 * @tc.name: testTaskNetwork004 1368 * @tc.desc: Test create task when network is any for UPLOAD 1369 * @tc.type: FUNC 1370 * @tc.require: 1371 */ 1372 it('testTaskNetwork004', async function (done) { 1373 let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1374 fs.closeSync(file); 1375 let conf = { 1376 action: agent.Action.UPLOAD, 1377 url: 'http://127.0.0.1', 1378 data: { 1379 name: 'test', 1380 value: { 1381 path: `${cacheDir}/test.txt`, 1382 } 1383 }, 1384 network: agent.NetWork.WIFI 1385 } 1386 1387 task = await agent.create(conf); 1388 task.start().then(() => { 1389 expect(true).assertTrue() 1390 done() 1391 }).catch((err) => { 1392 expect(false).assertTrue() 1393 done() 1394 }) 1395 }) 1396 1397 /** 1398 * @tc.name: testTaskRetry001 1399 * @tc.desc: Test create task when retry is true for frontend 1400 * @tc.type: FUNC 1401 * @tc.require: 1402 */ 1403 it('testTaskRetry001', async function (done) { 1404 let conf = { 1405 action: agent.Action.DOWNLOAD, 1406 url: 'xxx', 1407 mode: agent.Mode.FRONTEND, 1408 retry: true 1409 } 1410 task = await agent.create(context, conf); 1411 task.on('failed', function () { 1412 expect(true).assertTrue() 1413 done() 1414 }) 1415 task.start() 1416 }) 1417 1418 /** 1419 * @tc.name: testTaskRetry002 1420 * @tc.desc: Test create task when retry is true for background 1421 * @tc.type: FUNC 1422 * @tc.require: 1423 */ 1424 it('testTaskRetry002', async function (done) { 1425 let conf = { 1426 action: agent.Action.DOWNLOAD, 1427 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk', 1428 mode: agent.Mode.BACKGROUND, 1429 retry: true 1430 } 1431 task = await agent.create(conf); 1432 task.on('progress', function (progress) { 1433 if (progress.state === agent.State.RETRYING) { 1434 expect(true).assertTrue() 1435 done() 1436 } 1437 }) 1438 task.start() 1439 }) 1440 1441 /** 1442 * @tc.name: testTaskRetry003 1443 * @tc.desc: Test create task when retry is string 1444 * @tc.type: FUNC 1445 * @tc.require: 1446 */ 1447 it('testTaskRetry003', async function (done) { 1448 let conf = { 1449 action: agent.Action.DOWNLOAD, 1450 url: 'xxx', 1451 mode: agent.Mode.FRONTEND, 1452 retry: 'true' 1453 } 1454 task = await agent.create(context, conf); 1455 task.on('failed', function () { 1456 expect(true).assertTrue() 1457 done() 1458 }) 1459 task.start() 1460 }) 1461 1462 /** 1463 * @tc.name: testTaskRetry004 1464 * @tc.desc: Test create task when retry is false for frontend 1465 * @tc.type: FUNC 1466 * @tc.require: 1467 */ 1468 it('testTaskRetry004', async function (done) { 1469 let conf = { 1470 action: agent.Action.DOWNLOAD, 1471 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test1.apk', 1472 mode: agent.Mode.FRONTEND, 1473 retry: false 1474 } 1475 task = await agent.create(conf); 1476 task.on('failed', function () { 1477 expect(true).assertTrue() 1478 done() 1479 }) 1480 task.start() 1481 }) 1482 1483 /** 1484 * @tc.name: testTaskRetry005 1485 * @tc.desc: Test create task when retry is false for background 1486 * @tc.type: FUNC 1487 * @tc.require: 1488 */ 1489 it('testTaskRetry005', async function (done) { 1490 let conf = { 1491 action: agent.Action.DOWNLOAD, 1492 url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test1.apk', 1493 mode: agent.Mode.FRONTEND, 1494 retry: false 1495 } 1496 task.on('failed', function () { 1497 expect(true).assertTrue() 1498 done() 1499 }) 1500 task = await agent.create(conf); 1501 task.start() 1502 }) 1503})