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 : @TestOn('vm') 5 : import 'dart:async'; 6 : import 'dart:io'; 7 : import 'package:dio/dio.dart'; 8 : import 'package:test/test.dart'; 9 : 10 1 : void main() { 11 2 : test('#test headers', () { 12 2 : var headers = Headers.fromMap({ 13 1 : 'set-cookie': ['k=v', 'k1=v1'], 14 1 : 'content-length': ['200'], 15 1 : 'test': ['1', '2'], 16 : }); 17 1 : headers.add('SET-COOKIE', 'k2=v2'); 18 2 : assert(headers.value('content-length') == '200'); 19 4 : expect(Future(() => headers.value('test')), throwsException); 20 3 : assert(headers['set-cookie']?.length == 3); 21 1 : headers.remove('set-cookie', 'k=v'); 22 3 : assert(headers['set-cookie']?.length == 2); 23 1 : headers.removeAll('set-cookie'); 24 1 : assert(headers['set-cookie'] == null); 25 1 : var ls = []; 26 2 : headers.forEach((k, list) { 27 1 : ls.addAll(list); 28 : }); 29 2 : assert(ls.length == 3); 30 2 : assert(headers.toString() == 'content-length: 200\ntest: 1\ntest: 2\n'); 31 1 : headers.set('content-length', '300'); 32 2 : assert(headers.value('content-length') == '300'); 33 2 : headers.set('content-length', ['400']); 34 2 : assert(headers.value('content-length') == '400'); 35 : 36 1 : var headers1 = Headers(); 37 1 : headers1.set('xx', 'v'); 38 2 : assert(headers1.value('xx') == 'v'); 39 1 : headers1.clear(); 40 3 : assert(headers1.map.isEmpty == true); 41 : }); 42 : 43 2 : test('#send with an invalid URL', () { 44 1 : expect( 45 1 : Dio() 46 1 : .get('http://http.invalid') 47 3 : .catchError((e) => throw e.error as Object), 48 1 : throwsA(const TypeMatcher<SocketException>()), 49 : ); 50 : }); 51 : 52 2 : test('#cancellation', () async { 53 1 : var dio = Dio(); 54 1 : final token = CancelToken(); 55 3 : Timer(Duration(milliseconds: 10), () { 56 1 : token.cancel('cancelled'); 57 2 : dio.httpClientAdapter.close(force: true); 58 : }); 59 : 60 : var url = 'https://accounts.google.com'; 61 1 : expect( 62 : dio 63 1 : .get(url, cancelToken: token) 64 3 : .catchError((e) => throw CancelToken.isCancel(e as DioError)), 65 1 : throwsA(isTrue), 66 : ); 67 : }); 68 : 69 2 : test('#status error', () async { 70 3 : var dio = Dio()..options.baseUrl = 'http://httpbin.org/status/'; 71 1 : expect( 72 5 : dio.get('401').catchError((e) => throw e.response.statusCode as Object), 73 1 : throwsA(401), 74 : ); 75 : 76 2 : var r = await dio.get( 77 : '401', 78 2 : options: Options(validateStatus: (status) => true), 79 : ); 80 2 : expect(r.statusCode, 401); 81 : }); 82 : }