LCOV - code coverage report
Current view: top level - lib/src - route_matcher.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 32 32 100.0 %
Date: 2021-03-29 22:32:58 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : import 'dart:io';
       3             : 
       4             : import '../webserver.dart';
       5             : 
       6             : class RouteMatcher {
       7           2 :   static List<HttpRoute> match(
       8             :       String input, List<HttpRoute> options, RouteMethod method) {
       9           6 :     final inputParts = List<String>.from(Uri.parse(input).pathSegments);
      10             : 
      11           4 :     if (inputParts.last == "") {
      12           1 :       inputParts.removeLast();
      13             :     }
      14             : 
      15           2 :     var output = <HttpRoute>[];
      16             : 
      17           4 :     for (var item in options) {
      18           8 :       if (item.method != method && item.method != RouteMethod.all) {
      19             :         continue;
      20             :       }
      21             : 
      22           4 :       if (item.route == "*") {
      23           1 :         output.add(item);
      24             :         continue;
      25             :       }
      26             : 
      27           8 :       final itemParts = List<String>.from(Uri.parse(item.route).pathSegments);
      28             : 
      29           4 :       if (itemParts.last == "") {
      30           1 :         itemParts.removeLast();
      31             :       }
      32             : 
      33           6 :       if (itemParts.length != inputParts.length) {
      34             :         continue;
      35             :       }
      36             : 
      37             :       var matchesAll = true;
      38           6 :       for (var i = 0; i < inputParts.length; i++) {
      39           4 :         if (itemParts[i].startsWith(":")) {
      40             :           continue;
      41             :         }
      42           6 :         if (!RegExp("^${itemParts[i]}\$", caseSensitive: false)
      43           4 :             .hasMatch(inputParts[i])) {
      44             :           matchesAll = false;
      45             :           break;
      46             :         }
      47             :       }
      48             :       if (matchesAll) {
      49           2 :         output.add(item);
      50             :       }
      51             :     }
      52             :     return output;
      53             :   }
      54             : 
      55           1 :   static Map<String, String> getParams(String route, String input) {
      56           2 :     final routeParts = route.split("/")..remove("");
      57           2 :     final inputParts = input.split("/")..remove("");
      58             : 
      59           3 :     if (inputParts.length != routeParts.length) {
      60           1 :       throw NotMatchingRouteException();
      61             :     }
      62             : 
      63           1 :     final output = <String, String>{};
      64             : 
      65           3 :     for (var i = 0; i < routeParts.length; i++) {
      66           1 :       final routePart = routeParts[i];
      67           1 :       final inputPart = inputParts[i];
      68             : 
      69           1 :       if (routePart.contains(":")) {
      70           2 :         final routeParams = routePart.split(":")..remove("");
      71             : 
      72           2 :         for (var item in routeParams) {
      73           1 :           output[item] = inputPart;
      74             :         }
      75             :       }
      76             :     }
      77             :     return output;
      78             :   }
      79             : }
      80             : 
      81             : class HttpRoute {
      82             :   final String route;
      83             :   final FutureOr Function(HttpRequest req, HttpResponse res) callback;
      84             :   final RouteMethod method;
      85             :   final List<FutureOr Function(HttpRequest req, HttpResponse res)> middleware;
      86             : 
      87           2 :   HttpRoute(this.route, this.callback, this.method,
      88             :       {this.middleware = const []});
      89             : }
      90             : 
      91             : class NotMatchingRouteException implements Exception {}

Generated by: LCOV version 1.14