LCOV - code coverage report
Current view: top level - test - curt_test.dart (source / functions) Hit Total Coverage
Test: curt Lines: 185 185 100.0 %
Date: 2024-05-26 14:46:03 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:io';
       2             : 
       3             : import 'package:curt/src/curt.dart';
       4             : import 'package:curt/src/curt_response.dart';
       5             : import 'package:test/test.dart';
       6             : import 'package:testainers/testainers.dart';
       7             : 
       8             : ///
       9             : ///
      10             : ///
      11           1 : void main() {
      12             :   ///
      13           2 :   group('Online Tests', () {
      14           1 :     final Curt curt = Curt();
      15             : 
      16           2 :     test('GET https://google.com', () async {
      17           1 :       final CurtResponse response = await curt.get(
      18           1 :         Uri.parse('https://google.com'),
      19             :       );
      20           2 :       expect(response.statusCode, 301);
      21           2 :       expect(response.headers, isNotEmpty);
      22           2 :       expect(response.body, isNotEmpty);
      23             :     });
      24             :   });
      25             : 
      26             :   ///
      27           1 :   group(
      28             :     'Local Tests',
      29           1 :     () {
      30           1 :       final Curt curt = Curt();
      31             : 
      32           1 :       final Curt insecure = Curt(insecure: true);
      33             : 
      34           1 :       final Curt followRedirects = Curt(followRedirects: true);
      35             : 
      36           1 :       final TestainersHttpbucket container = TestainersHttpbucket();
      37             : 
      38             :       const Map<String, String> headers = <String, String>{
      39             :         'Content-Type': 'application/json; charset=utf-8',
      40             :       };
      41             : 
      42             :       const String server = 'localhost';
      43             : 
      44             :       ///
      45           2 :       setUpAll(() async {
      46           1 :         await container.start();
      47             :       });
      48             : 
      49           2 :       test('GET HTTP 200', () async {
      50           1 :         final CurtResponse response = await curt.get(
      51           3 :           Uri.parse('http://$server:${container.httpPort}/status/200'),
      52             :         );
      53           2 :         expect(response.statusCode, 200);
      54           2 :         expect(response.headers, isNotEmpty);
      55           2 :         expect(response.body, isNotEmpty);
      56             :       });
      57             : 
      58           2 :       test('GET HTTP 403', () async {
      59           1 :         final CurtResponse response = await curt.get(
      60           3 :           Uri.parse('http://$server:${container.httpPort}/status/403'),
      61             :         );
      62           2 :         expect(response.statusCode, 403);
      63           2 :         expect(response.headers, isNotEmpty);
      64           2 :         expect(response.body, isNotEmpty);
      65             :       });
      66             : 
      67           2 :       test('GET HTTP 404', () async {
      68           1 :         final CurtResponse response = await curt.get(
      69           3 :           Uri.parse('http://$server:${container.httpPort}/status/404'),
      70             :         );
      71           2 :         expect(response.statusCode, 404);
      72           2 :         expect(response.headers, isNotEmpty);
      73           2 :         expect(response.body, isNotEmpty);
      74             :       });
      75             : 
      76           2 :       test('GET HTTP 500', () async {
      77           1 :         final CurtResponse response = await curt.get(
      78           3 :           Uri.parse('http://$server:${container.httpPort}/status/500'),
      79             :         );
      80           2 :         expect(response.statusCode, 500);
      81           2 :         expect(response.headers, isNotEmpty);
      82           2 :         expect(response.body, isNotEmpty);
      83             :       });
      84             : 
      85           2 :       test('POST HTTP 200', () async {
      86           1 :         final CurtResponse response = await curt.post(
      87           3 :           Uri.parse('http://$server:${container.httpPort}/status/200'),
      88             :           headers: headers,
      89             :         );
      90           2 :         expect(response.statusCode, 200);
      91           2 :         expect(response.headers, isNotEmpty);
      92           2 :         expect(response.body, isNotEmpty);
      93             :       });
      94             : 
      95           2 :       test('POST HTTP 403', () async {
      96           1 :         final CurtResponse response = await curt.post(
      97           3 :           Uri.parse('http://$server:${container.httpPort}/status/403'),
      98             :           headers: headers,
      99             :         );
     100           2 :         expect(response.statusCode, 403);
     101           2 :         expect(response.headers, isNotEmpty);
     102           2 :         expect(response.body, isNotEmpty);
     103             :       });
     104             : 
     105           2 :       test('POST HTTP 404', () async {
     106           1 :         final CurtResponse response = await curt.post(
     107           3 :           Uri.parse('http://$server:${container.httpPort}/status/404'),
     108             :           headers: headers,
     109             :         );
     110           2 :         expect(response.statusCode, 404);
     111           2 :         expect(response.headers, isNotEmpty);
     112           2 :         expect(response.body, isNotEmpty);
     113             :       });
     114             : 
     115           2 :       test('POST HTTP 500', () async {
     116           1 :         final CurtResponse response = await curt.post(
     117           3 :           Uri.parse('http://$server:${container.httpPort}/status/500'),
     118             :           headers: headers,
     119             :         );
     120           2 :         expect(response.statusCode, 500);
     121           2 :         expect(response.headers, isNotEmpty);
     122           2 :         expect(response.body, isNotEmpty);
     123             :       });
     124             : 
     125           2 :       test('PUT HTTP 200', () async {
     126           1 :         final CurtResponse response = await curt.put(
     127           3 :           Uri.parse('http://$server:${container.httpPort}/status/200'),
     128             :           headers: headers,
     129             :         );
     130           2 :         expect(response.statusCode, 200);
     131           2 :         expect(response.headers, isNotEmpty);
     132           2 :         expect(response.body, isNotEmpty);
     133             :       });
     134             : 
     135           2 :       test('PUT HTTP 403', () async {
     136           1 :         final CurtResponse response = await curt.put(
     137           3 :           Uri.parse('http://$server:${container.httpPort}/status/403'),
     138             :           headers: headers,
     139             :         );
     140           2 :         expect(response.statusCode, 403);
     141           2 :         expect(response.headers, isNotEmpty);
     142           2 :         expect(response.body, isNotEmpty);
     143             :       });
     144             : 
     145           2 :       test('PUT HTTP 404', () async {
     146           1 :         final CurtResponse response = await curt.put(
     147           3 :           Uri.parse('http://$server:${container.httpPort}/status/404'),
     148             :           headers: headers,
     149             :         );
     150           2 :         expect(response.statusCode, 404);
     151           2 :         expect(response.headers, isNotEmpty);
     152           2 :         expect(response.body, isNotEmpty);
     153             :       });
     154             : 
     155           2 :       test('PUT HTTP 500', () async {
     156           1 :         final CurtResponse response = await curt.put(
     157           3 :           Uri.parse('http://$server:${container.httpPort}/status/500'),
     158             :           headers: headers,
     159             :         );
     160           2 :         expect(response.statusCode, 500);
     161           2 :         expect(response.headers, isNotEmpty);
     162           2 :         expect(response.body, isNotEmpty);
     163             :       });
     164             : 
     165           2 :       test('DELETE HTTP 200', () async {
     166           1 :         final CurtResponse response = await curt.delete(
     167           3 :           Uri.parse('http://$server:${container.httpPort}/status/200'),
     168             :           headers: headers,
     169             :         );
     170           2 :         expect(response.statusCode, 200);
     171           2 :         expect(response.headers, isNotEmpty);
     172           2 :         expect(response.body, isNotEmpty);
     173             :       });
     174             : 
     175           2 :       test('DELETE HTTP 403', () async {
     176           1 :         final CurtResponse response = await curt.delete(
     177           3 :           Uri.parse('http://$server:${container.httpPort}/status/403'),
     178             :           headers: headers,
     179             :         );
     180           2 :         expect(response.statusCode, 403);
     181           2 :         expect(response.headers, isNotEmpty);
     182           2 :         expect(response.body, isNotEmpty);
     183             :       });
     184             : 
     185           2 :       test('DELETE HTTP 404', () async {
     186           1 :         final CurtResponse response = await curt.delete(
     187           3 :           Uri.parse('http://$server:${container.httpPort}/status/404'),
     188             :           headers: headers,
     189             :         );
     190           2 :         expect(response.statusCode, 404);
     191           2 :         expect(response.headers, isNotEmpty);
     192           2 :         expect(response.body, isNotEmpty);
     193             :       });
     194             : 
     195           2 :       test('DELETE HTTP 500', () async {
     196           1 :         final CurtResponse response = await curt.delete(
     197           3 :           Uri.parse('http://$server:${container.httpPort}/status/500'),
     198             :           headers: headers,
     199             :         );
     200           2 :         expect(response.statusCode, 500);
     201           2 :         expect(response.headers, isNotEmpty);
     202           2 :         expect(response.body, isNotEmpty);
     203             :       });
     204             : 
     205           2 :       test('Body Length 123', () async {
     206           1 :         final CurtResponse response = await curt.get(
     207           3 :           Uri.parse('http://$server:${container.httpPort}/length/123'),
     208             :         );
     209             : 
     210           2 :         expect(response.statusCode, 200);
     211           2 :         expect(response.headers, isNotEmpty);
     212           3 :         expect(response.headers.contentLength, 123);
     213           2 :         expect(response.body, isNotEmpty);
     214           3 :         expect(response.body.length, 123);
     215             :       });
     216             : 
     217           2 :       test('Body Length 321', () async {
     218           1 :         final CurtResponse response = await curt.get(
     219           3 :           Uri.parse('http://$server:${container.httpPort}/length/321'),
     220             :         );
     221             : 
     222           2 :         expect(response.statusCode, 200);
     223           2 :         expect(response.headers, isNotEmpty);
     224           3 :         expect(response.headers.contentLength, 321);
     225           2 :         expect(response.body, isNotEmpty);
     226           3 :         expect(response.body.length, 321);
     227             :       });
     228             : 
     229           2 :       test('Body Length 1024', () async {
     230           1 :         final CurtResponse response = await curt.get(
     231           3 :           Uri.parse('http://$server:${container.httpPort}/length/1024'),
     232             :         );
     233             : 
     234           2 :         expect(response.statusCode, 200);
     235           2 :         expect(response.headers, isNotEmpty);
     236           3 :         expect(response.headers.contentLength, 1024);
     237           2 :         expect(response.body, isNotEmpty);
     238           3 :         expect(response.body.length, 1024);
     239             :       });
     240             : 
     241           2 :       test('Redirect with no follow', () async {
     242           1 :         final Uri uri = Uri(
     243             :           scheme: 'http',
     244             :           host: server,
     245           1 :           port: container.httpPort,
     246             :           path: 'redirect',
     247           1 :           queryParameters: <String, dynamic>{
     248           2 :             'url': 'http://$server:${container.httpPort}/status/200',
     249             :           },
     250             :         );
     251             : 
     252           1 :         final CurtResponse response = await curt.get(uri);
     253             : 
     254           2 :         expect(response.statusCode, 302);
     255           2 :         expect(response.headers, isNotEmpty);
     256           1 :         expect(
     257           2 :           response.headers.value(HttpHeaders.locationHeader),
     258           2 :           uri.queryParameters['url'],
     259             :         );
     260           2 :         expect(response.body, isEmpty);
     261             :       });
     262             : 
     263           2 :       test('Redirect', () async {
     264           1 :         final Uri uri = Uri(
     265             :           scheme: 'http',
     266             :           host: server,
     267           1 :           port: container.httpPort,
     268             :           path: 'redirect',
     269           1 :           queryParameters: <String, dynamic>{
     270           2 :             'url': 'http://$server:${container.httpPort}/status/200',
     271             :           },
     272             :         );
     273             : 
     274           1 :         final CurtResponse response = await followRedirects.get(uri);
     275             : 
     276           2 :         expect(response.statusCode, 200);
     277           2 :         expect(response.headers, isNotEmpty);
     278           3 :         expect(response.headers.value(HttpHeaders.locationHeader), isNull);
     279           2 :         expect(response.body, isNotEmpty);
     280             :       });
     281             : 
     282           2 :       test('Simple HTTPS GET', () async {
     283           1 :         final CurtResponse response = await insecure.get(
     284           3 :           Uri.parse('https://$server:${container.httpsPort}/status/200'),
     285             :         );
     286           2 :         expect(response.statusCode, 200);
     287           2 :         expect(response.headers, isNotEmpty);
     288           2 :         expect(response.body, isNotEmpty);
     289             :       });
     290             : 
     291             :       ///
     292           2 :       test('Simple HTTPS POST', () async {
     293           1 :         final CurtResponse response = await insecure.post(
     294           3 :           Uri.parse('https://$server:${container.httpsPort}/status/200'),
     295             :           headers: headers,
     296             :         );
     297           2 :         expect(response.statusCode, 200);
     298           2 :         expect(response.headers, isNotEmpty);
     299           2 :         expect(response.body, isNotEmpty);
     300             :       });
     301             : 
     302             :       ///
     303           2 :       test('Simple HTTPS PUT', () async {
     304           1 :         final CurtResponse response = await insecure.put(
     305           3 :           Uri.parse('https://$server:${container.httpsPort}/status/200'),
     306             :           headers: headers,
     307             :         );
     308           2 :         expect(response.statusCode, 200);
     309           2 :         expect(response.headers, isNotEmpty);
     310           2 :         expect(response.body, isNotEmpty);
     311             :       });
     312             : 
     313             :       ///
     314           2 :       test('Simple HTTPS DELETE', () async {
     315           1 :         final CurtResponse response = await insecure.delete(
     316           3 :           Uri.parse('https://$server:${container.httpsPort}/status/200'),
     317             :           headers: headers,
     318             :         );
     319           2 :         expect(response.statusCode, 200);
     320           2 :         expect(response.headers, isNotEmpty);
     321           2 :         expect(response.body, isNotEmpty);
     322             :       });
     323             : 
     324             :       ///
     325           2 :       tearDownAll(container.stop);
     326             :     },
     327           1 :     onPlatform: <String, dynamic>{
     328             :       'mac-os': const Skip('No docker installed on GitHub actions.'),
     329             :       'windows': const Skip('Need a windows container image.'),
     330             :     },
     331             :   );
     332             : }

Generated by: LCOV version 1.14