A package:http client that can mock server responses.
Instead of sending requests over a network, the MockClient
class is also
a server in itself. It uses the cross-platform angel_route
library to provide
you with a routing solution that supports path parameters and middleware out-of-the-box.
All your handlers just need to accept a MockHttpContext
. If they return true
, then
other handlers will be allowed to run (effectively accomplishing a middleware concept).
Otherwise, the return value will be processed.
You can return:
Response
or StreamedResponse
null
or nothing - do not write anything else to the serverJSON.encode
var client = new http.MockClient();
client.router
..get('/hello/:name', (http.MockHttpContext ctx) {
// Return a plain response
return new http.Response('Hello, ${ctx.request.params["name"]}!', 200);
})
..all('*', (http.MockHttpContext ctx) {
return new http.Response('404 Not Found', 404);
});
// And then, in a test...
var response = await client.get('/hello/world');
expect(response.body, 'Hello, world!');
Add this to your package's pubspec.yaml file:
dependencies:
http_mock: "^1.0.0"
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:http_mock/http_mock.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
1.0.0 | Jul 20, 2017 |
|
|
We analyzed this package on Apr 9, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
25 | / 100 |
Health:
Code health derived from static analysis.
[more]
|
98 | / 100 |
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
80 | / 100 |
Overall score:
Weighted score of the above.
[more]
|
58 |
Detected platforms: Flutter, other
Primary library:
package:http_mock/http_mock.dart
with components:io
.
Maintain CHANGELOG.md
.
Changelog entries help clients to follow the progress in your code.
The description is too short.
Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.
Maintain an example.
Create a short demo in the
example/
directory to show how to use this package. Common file name patterns include:main.dart
,example.dart
or you could also usehttp_mock.dart
.
Fix analysis and formatting issues.
Analysis or formatting checks reported 2 hints.
Strong-mode analysis of
lib/http_mock.dart
gave the following hint:line: 50 col: 19
'JSON' is deprecated and shouldn't be used.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.19.0 | ||
angel_route | ^1.0.0 | 1.0.8 | 2.0.5 |
http | ^0.11.3 | 0.11.3+16 | |
Transitive dependencies | |||
async | 2.0.6 | ||
charcode | 1.1.1 | ||
collection | 1.14.9 | ||
http_parser | 3.1.1 | ||
path | 1.5.1 | ||
source_span | 1.4.0 | ||
string_scanner | 1.0.2 | ||
typed_data | 1.1.5 | ||
Dev dependencies | |||
test | ^0.12.0 |