http_cassette_http 0.1.0 copy "http_cassette_http: ^0.1.0" to clipboard
http_cassette_http: ^0.1.0 copied to clipboard

HTTP Cassette integration for recording and replaying requests made with http.

example/http_cassette_http_example.dart

import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:http_cassette/http_cassette.dart';
import 'package:http_cassette_http/http_cassette_http.dart';

Future<void> main() async {
  final engine = CassetteEngine(store: MemoryCassetteStore());
  final transport = _ExampleClient();
  final client = CassetteHttpClient(engine, inner: transport);

  final uri = Uri.parse('https://example.test/status');
  final live = await engine.record(
    'example/status',
    () => client.get(uri),
  );
  final recorded = await engine.replay(
    'example/status',
    () => client.get(uri),
  );

  if (live.body != 'Available' || recorded.body != 'Available') {
    throw StateError('The HTTP response was not preserved.');
  }
  if (transport.sendCount != 1) {
    throw StateError('Replay unexpectedly reached the wrapped client.');
  }

  client.close();
  if (transport.closeCount != 1) {
    throw StateError('The wrapped client was not closed exactly once.');
  }
}

final class _ExampleClient extends http.BaseClient {
  var sendCount = 0;
  var closeCount = 0;

  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) async {
    sendCount += 1;
    return http.StreamedResponse(
      Stream<List<int>>.value('Available'.codeUnits),
      200,
      request: request,
      headers: <String, String>{'content-type': 'text/plain'},
    );
  }

  @override
  void close() {
    closeCount += 1;
  }
}
0
likes
160
points
30
downloads

Documentation

API reference

Publisher

verified publisherebenezerundie.com

Weekly Downloads

HTTP Cassette integration for recording and replaying requests made with http.

Repository (GitHub)
View/report issues

Topics

#http #testing

License

BSD-3-Clause (license)

Dependencies

http, http_cassette

More

Packages that depend on http_cassette_http