LCOV - code coverage report
Current view: top level - Users/duwen/Documents/code/dio/dio/test - options_test.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 107 108 99.1 %
Date: 2021-11-28 14:37:50 Functions: 0 0 -

          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 'package:dio/dio.dart';
       6             : import 'package:test/test.dart';
       7             : 
       8             : import 'echo_adapter.dart';
       9             : 
      10           1 : void main() {
      11           2 :   test('#test options', () {
      12           1 :     var map = {'a': '5'};
      13           1 :     var mapOverride = {'b': '6'};
      14           1 :     var baseOptions = BaseOptions(
      15             :       connectTimeout: 2000,
      16             :       receiveTimeout: 2000,
      17             :       sendTimeout: 2000,
      18             :       baseUrl: 'http://localhost',
      19             :       queryParameters: map,
      20             :       extra: map,
      21             :       headers: map,
      22             :       contentType: 'application/json',
      23             :       followRedirects: false,
      24             :     );
      25           1 :     var opt1 = baseOptions.copyWith(
      26             :       method: 'post',
      27             :       receiveTimeout: 3000,
      28             :       sendTimeout: 3000,
      29             :       baseUrl: 'https://flutterchina.club',
      30             :       extra: mapOverride,
      31             :       headers: mapOverride,
      32             :       contentType: 'text/html',
      33             :     );
      34           2 :     assert(opt1.method == 'post');
      35           2 :     assert(opt1.receiveTimeout == 3000);
      36           2 :     assert(opt1.connectTimeout == 2000);
      37           2 :     assert(opt1.followRedirects == false);
      38           2 :     assert(opt1.baseUrl == 'https://flutterchina.club');
      39           3 :     assert(opt1.headers['b'] == '6');
      40           3 :     assert(opt1.extra['b'] == '6');
      41           2 :     assert(opt1.queryParameters['b'] == null);
      42           2 :     assert(opt1.contentType == 'text/html');
      43             : 
      44           1 :     var opt2 = Options(
      45             :       method: 'get',
      46             :       receiveTimeout: 2000,
      47             :       sendTimeout: 2000,
      48             :       extra: map,
      49             :       headers: map,
      50             :       contentType: 'application/json',
      51             :       followRedirects: false,
      52             :     );
      53             : 
      54           1 :     var opt3 = opt2.copyWith(
      55             :       method: 'post',
      56             :       receiveTimeout: 3000,
      57             :       sendTimeout: 3000,
      58             :       extra: mapOverride,
      59             :       headers: mapOverride,
      60             :       contentType: 'text/html',
      61             :     );
      62             : 
      63           2 :     assert(opt3.method == 'post');
      64           2 :     assert(opt3.receiveTimeout == 3000);
      65           2 :     assert(opt3.followRedirects == false);
      66           3 :     assert(opt3.headers!['b'] == '6');
      67           3 :     assert(opt3.extra!['b'] == '6');
      68           2 :     assert(opt3.contentType == 'text/html');
      69             : 
      70           1 :     var opt4 = RequestOptions(
      71             :       path: '/xxx',
      72             :       sendTimeout: 2000,
      73             :       followRedirects: false,
      74             :     );
      75           1 :     var opt5 = opt4.copyWith(
      76             :       method: 'post',
      77             :       receiveTimeout: 3000,
      78             :       sendTimeout: 3000,
      79             :       extra: mapOverride,
      80             :       headers: mapOverride,
      81             :       data: 'xx=5',
      82             :       path: '/',
      83             :       contentType: 'text/html',
      84             :     );
      85           2 :     assert(opt5.method == 'post');
      86           2 :     assert(opt5.receiveTimeout == 3000);
      87           2 :     assert(opt5.followRedirects == false);
      88           2 :     assert(opt5.contentType == 'text/html');
      89           3 :     assert(opt5.headers['b'] == '6');
      90           3 :     assert(opt5.extra['b'] == '6');
      91           2 :     assert(opt5.data == 'xx=5');
      92           2 :     assert(opt5.path == '/');
      93             : 
      94             :     // Keys of header are case-insensitive
      95           3 :     expect(opt5.headers['B'], '6');
      96           2 :     opt5.headers['B'] = 9;
      97           3 :     assert(opt5.headers['b'] == 9);
      98             :   });
      99           2 :   test('#test options content-type', () {
     100             :     const contentType = 'text/html';
     101             :     const contentTypeJson = 'appliction/json';
     102           1 :     var headers = {'content-type': contentType};
     103           1 :     var jsonHeaders = {'content-type': contentTypeJson};
     104             : 
     105             :     try {
     106           1 :       BaseOptions(contentType: contentType, headers: headers);
     107           1 :       assert(false, 'baseOptions1');
     108             :     } catch (e) {
     109             :       //
     110             :     }
     111             : 
     112           1 :     var bo1 = BaseOptions(contentType: contentType);
     113           1 :     var bo2 = BaseOptions(headers: headers);
     114           1 :     var bo3 = BaseOptions();
     115             : 
     116           3 :     assert(bo1.headers['content-type'] == contentType);
     117           3 :     assert(bo2.headers['content-type'] == contentType);
     118           3 :     assert(bo3.headers['content-type'] == Headers.jsonContentType);
     119             : 
     120             :     try {
     121           1 :       bo1.copyWith(headers: headers);
     122           1 :       assert(false, 'baseOptions copyWith 1');
     123             :     } catch (e) {
     124             :       //
     125             :     }
     126             : 
     127             :     try {
     128           1 :       bo2.copyWith(contentType: contentType);
     129           1 :       assert(false, 'baseOptions copyWith 2');
     130             :     } catch (e) {
     131             :       //
     132             :     }
     133             : 
     134           1 :     bo3.copyWith();
     135             : 
     136             :     /// options
     137             :     try {
     138           1 :       Options(contentType: contentType, headers: headers);
     139           1 :       assert(false, 'Options1');
     140             :     } catch (e) {
     141             :       //
     142             :     }
     143             : 
     144           1 :     var o1 = Options(contentType: contentType);
     145           1 :     var o2 = Options(headers: headers);
     146             : 
     147             :     try {
     148           1 :       o1.copyWith(headers: headers);
     149           1 :       assert(false, 'Options copyWith 1');
     150             :     } catch (e) {
     151             :       //
     152             :     }
     153             : 
     154             :     try {
     155           1 :       o2.copyWith(contentType: contentType);
     156           1 :       assert(false, 'Options copyWith 2');
     157             :     } catch (e) {
     158             :       //
     159             :     }
     160             : 
     161           4 :     assert(Options(contentType: contentTypeJson).compose(bo1, '').contentType ==
     162             :         contentTypeJson);
     163             : 
     164           4 :     assert(Options(contentType: contentTypeJson).compose(bo2, '').contentType ==
     165             :         contentTypeJson);
     166             : 
     167           4 :     assert(Options(headers: jsonHeaders).compose(bo1, '').contentType ==
     168             :         contentTypeJson);
     169             : 
     170           4 :     assert(Options(headers: jsonHeaders).compose(bo2, '').contentType ==
     171             :         contentTypeJson);
     172             : 
     173             :     /// RequestOptions
     174             :     try {
     175           1 :       RequestOptions(path: '', contentType: contentType, headers: headers);
     176           1 :       assert(false, 'Options1');
     177             :     } catch (e) {
     178             :       //
     179             :     }
     180             : 
     181           1 :     var ro1 = RequestOptions(path: '', contentType: contentType);
     182           1 :     var ro2 = RequestOptions(path: '', headers: headers);
     183             : 
     184             :     try {
     185           1 :       ro1.copyWith(headers: headers);
     186           1 :       assert(false, 'RequestOptions copyWith 1');
     187             :     } catch (e) {
     188             :       //
     189             :     }
     190             : 
     191             :     try {
     192           1 :       ro2.copyWith(contentType: contentType);
     193           1 :       assert(false, 'RequestOptions copyWith 2');
     194             :     } catch (e) {
     195             :       //
     196             :     }
     197             : 
     198           1 :     var ro3 = RequestOptions(path: '');
     199           1 :     ro3.copyWith();
     200             :   });
     201             : 
     202           2 :   test('#test default content-type', () async {
     203           1 :     var dio = Dio();
     204           2 :     dio.options.baseUrl = EchoAdapter.mockBase;
     205           2 :     dio.httpClientAdapter = EchoAdapter();
     206             : 
     207           2 :     var r1 = await dio.get('');
     208           3 :     assert(r1.requestOptions.headers[Headers.contentTypeHeader] == null);
     209             : 
     210           2 :     dio.options.setRequestContentTypeWhenNoPayload = true;
     211             : 
     212           2 :     r1 = await dio.get('');
     213           4 :     assert(r1.requestOptions.headers[Headers.contentTypeHeader] ==
     214             :         Headers.jsonContentType);
     215             : 
     216           2 :     dio.options.setRequestContentTypeWhenNoPayload = false;
     217             : 
     218           2 :     var r2 = await dio.get(
     219             :       '',
     220           1 :       options: Options(contentType: Headers.jsonContentType),
     221             :     );
     222             : 
     223           4 :     assert(r2.requestOptions.headers[Headers.contentTypeHeader] ==
     224             :         Headers.jsonContentType);
     225             : 
     226           2 :     var r3 = await dio.get(
     227             :       '',
     228           2 :       options: Options(headers: {
     229             :         Headers.contentTypeHeader: Headers.jsonContentType,
     230             :       }),
     231             :     );
     232           4 :     assert(r3.requestOptions.headers[Headers.contentTypeHeader] ==
     233             :         Headers.jsonContentType);
     234             : 
     235           2 :     var r4 = await dio.post('', data: '');
     236           4 :     assert(r4.requestOptions.headers[Headers.contentTypeHeader] ==
     237             :         Headers.jsonContentType);
     238             :   });
     239             : 
     240           2 :   test('#test default content-type2', () async {
     241           1 :     final dio = Dio();
     242           2 :     dio.options.setRequestContentTypeWhenNoPayload = true;
     243           2 :     dio.options.baseUrl = 'https://www.example.com';
     244             : 
     245           4 :     var r1 = Options(method: 'GET').compose(dio.options, '/test').copyWith(
     246           1 :       headers: {Headers.contentTypeHeader: Headers.textPlainContentType},
     247             :     );
     248             :     assert(
     249           3 :         r1.headers[Headers.contentTypeHeader] == Headers.textPlainContentType);
     250             : 
     251           4 :     var r2 = Options(method: 'GET').compose(dio.options, '/test').copyWith(
     252             :           contentType: Headers.textPlainContentType,
     253             :         );
     254             :     assert(
     255           3 :         r2.headers[Headers.contentTypeHeader] == Headers.textPlainContentType);
     256             : 
     257             :     try {
     258           4 :       Options(method: 'GET').compose(dio.options, '/test').copyWith(
     259           1 :         headers: {Headers.contentTypeHeader: Headers.textPlainContentType},
     260             :         contentType: Headers.formUrlEncodedContentType,
     261             :       );
     262           0 :       assert(false);
     263             :     } catch (e) {
     264             :       //
     265             :     }
     266             : 
     267           2 :     dio.options.setRequestContentTypeWhenNoPayload = false;
     268             : 
     269           3 :     var r3 = Options(method: 'GET').compose(dio.options, '/test');
     270           3 :     assert(r3.uri.toString() == 'https://www.example.com/test');
     271           2 :     assert(r3.headers[Headers.contentTypeHeader] == null);
     272             :   });
     273             : }

Generated by: LCOV version 1.14