Future function()

in integration_test/lib/functions.dart [36:112]


Future<Response> function(Request request) async {
  _watch.start();
  _requestCount++;
  _activeRequests++;
  if (_activeRequests > _maxActiveRequests) {
    _maxActiveRequests = _activeRequests;
  }

  final urlPath = request.url.path;

  if (urlPath.contains('slow')) {
    // Adds a one-second pause to matching requests.
    // Good for testing concurrency
    await Future<void>.delayed(const Duration(seconds: 1));
  }

  try {
    if (urlPath.startsWith('info')) {
      final output = {
        'request': request.requestedUri.toString(),
        'thisInstance': {
          'activeRequests': _activeRequests,
          'maxActiveRequests': _maxActiveRequests,
          'totalRequests': _requestCount,
          'upTime': _watch.elapsed.toString(),
        },
        'platform': {
          'numberOfProcessors': Platform.numberOfProcessors,
          'operatingSystem': Platform.operatingSystem,
          'operatingSystemVersion': Platform.operatingSystemVersion,
          'version': Platform.version,
        },
        'process': {
          'currentRss': ProcessInfo.currentRss,
          'maxRss': ProcessInfo.maxRss,
        },
        'headers': request.headers,
        'environment': Platform.environment,
      };

      return Response.ok(
        encodeJsonPretty(output),
        headers: _jsonHeaders,
      );
    }

    if (urlPath.startsWith('exception')) {
      throw BadRequestException(400, 'Testing `throw BadRequestException`');
    }

    if (urlPath.startsWith('error')) {
      if (urlPath.contains('async')) {
        // Add a pause to the result
        await Future<void>.value();
        unawaited(
          Future<void>.value().then((value) => throw StateError('async error')),
        );
      }
      throw Exception('An error was forced by requesting "$urlPath"');
    }

    if (urlPath.startsWith('print')) {
      for (var segment in request.url.pathSegments) {
        print(segment);
      }
      return Response.ok('Printing: $urlPath');
    }

    if (urlPath.startsWith('binary')) {
      return Response.ok(_helloWorldBytes);
    }

    return Response.ok('Hello, World!');
  } finally {
    _activeRequests--;
  }
}