Mock HTTP Request
Forked from mock_request to support NNBD
Manufacture dart:io HttpRequests, HttpResponses, HttpHeaders, etc. This makes it possible to test server-side Dart applications without having to ever bind to a port.
This package was originally designed to make testing Angel3 applications smoother, but works with any Dart-based server.
Installation
In your pubspec.yaml:
dev_dependencies:
angel3_mock_request: ^10.0.0
Usage
var rq = MockHttpRequest('GET', Uri.parse('/foo'));
await rq.close();
await AngelHttp(app).handleRequest(rq); // Run within your server-side application
var rs = rq.response;
expect(rs.statusCode, equals(200));
expect(await rs.transform(utf8.decoder).join(),
equals(json.encode('Hello, world!')));
More examples can be found in the included test cases.
Migrating from 9.x to 10.0.0
The breaking changes of each release are listed in the CHANGELOG. Those of 10.0.0 that may need changes in an app:
- Streams:
MockHttpRequestextendsStream<Uint8List>, so its stream methods have the signatures ofdart:io'sHttpRequest; code that treated it as aStream<List<int>>may need.cast<List<int>>(). - Encoding:
writethrows anArgumentErrorfor a character its encoding cannot represent, instead of truncating it. Set thecontent-typecharset, orencoding, to one that can represent it.