Line data Source code
1 : // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 2 : // for details. All rights reserved. Use of this source code is governed by a 3 : // BSD-style license that can be found in the LICENSE file. 4 : 5 : @TestOn('vm') 6 : import 'dart:io'; 7 : import 'package:dio/dio.dart'; 8 : import 'package:test/test.dart'; 9 : import 'utils.dart'; 10 : 11 1 : void main() { 12 1 : setUp(startServer); 13 1 : tearDown(stopServer); 14 2 : test('#test download1', () async { 15 : const savePath = '../_download_test.md'; 16 1 : var dio = Dio(); 17 4 : dio.options.baseUrl = serverUrl.toString(); 18 2 : await dio.download( 19 : '/download', savePath, // disable gzip 20 1 : onReceiveProgress: (received, total) { 21 : // ignore progress 22 : }, 23 : ); 24 : 25 1 : var f = File(savePath); 26 3 : expect(f.readAsStringSync(), equals('I am a text file')); 27 1 : f.deleteSync(recursive: false); 28 : }); 29 : 30 2 : test('#test download2', () async { 31 : const savePath = '../_download_test.md'; 32 1 : var dio = Dio(); 33 4 : dio.options.baseUrl = serverUrl.toString(); 34 2 : await dio.downloadUri( 35 2 : serverUrl.replace(path: '/download'), 36 1 : (header) => savePath, // disable gzip 37 : ); 38 : 39 1 : var f = File(savePath); 40 3 : expect(f.readAsStringSync(), equals('I am a text file')); 41 1 : f.deleteSync(recursive: false); 42 : }); 43 : 44 2 : test('#test download error', () async { 45 : const savePath = '../_download_test.md'; 46 1 : var dio = Dio(); 47 4 : dio.options.baseUrl = serverUrl.toString(); 48 1 : var r = await dio 49 1 : .download('/error', savePath) 50 3 : .catchError((e) => (e as DioError).response!); 51 2 : assert(r.data == 'error'); 52 1 : r = await dio 53 1 : .download( 54 : '/error', 55 : savePath, 56 1 : options: Options(receiveDataWhenStatusError: false), 57 : ) 58 3 : .catchError((e) => (e as DioError).response!); 59 1 : assert(r.data == null); 60 : }); 61 : 62 2 : test('#test download timeout', () async { 63 : const savePath = '../_download_test.md'; 64 2 : var dio = Dio(BaseOptions( 65 : receiveTimeout: 1, 66 2 : baseUrl: serverUrl.toString(), 67 : )); 68 1 : expect( 69 : dio 70 1 : .download('/download', savePath) 71 3 : .catchError((e) => throw (e as DioError).type), 72 1 : throwsA(DioErrorType.receiveTimeout)); 73 : //print(r); 74 : }); 75 : 76 2 : test('#test download cancellation', () async { 77 : const savePath = '../_download_test.md'; 78 1 : var cancelToken = CancelToken(); 79 3 : Future.delayed(Duration(milliseconds: 100), () { 80 1 : cancelToken.cancel(); 81 : }); 82 1 : expect( 83 1 : Dio() 84 1 : .download( 85 3 : serverUrl.toString() + '/download', 86 : savePath, 87 : cancelToken: cancelToken, 88 : ) 89 3 : .catchError((e) => throw (e as DioError).type), 90 1 : throwsA(DioErrorType.cancel), 91 : ); 92 : //print(r); 93 : }); 94 : }