request method
Executes a logical request.
Implementation
@override
Future<HttpResponseSpec> request(
MapScenarioWorld world,
HttpRequestSpec request,
) async {
final path = endpoints[request.endpointId];
if (path == null) {
throw StateError('Unknown logical endpoint: ${request.endpointId}');
}
final uri = baseUri.resolve(path);
final stopwatch = Stopwatch()..start();
final httpRequest = await client.openUrl(request.method, uri);
httpRequest.headers.contentType = ContentType.json;
request.headers.forEach(httpRequest.headers.set);
if (request.body != null) httpRequest.write(jsonEncode(request.body));
final response = await httpRequest.close();
final bodyText = await utf8.decoder.bind(response).join();
stopwatch.stop();
Object? body;
if (bodyText.isNotEmpty) {
try {
body = jsonDecode(bodyText);
} on FormatException {
body = bodyText;
}
}
return HttpResponseSpec(
statusCode: response.statusCode,
body: body,
latency: stopwatch.elapsed,
);
}