http_mock_adapter 0.1.6
http_mock_adapter: ^0.1.6 copied to clipboard
HTTP mock adapter for Dio & Mockito. By simply defining requests and corresponding responses through predefined adapters, you will be able to mock requests sent via Dio.
http-mock-adapter #
Description #
Dio HTTP mock adapter for Dart/Flutter (compatible with Mockito).
Sometimes, testing classes which are dependent on Dio requests are really tricky and require lots of boilerplate, this is where http_mock_adapter comes in, to make Dio
request testing more flexible than it has ever been.
By simply defining requests with their methods and responses by using chains of our DioAdapter and replacing Dio
's httpClientAdapter with your custom mocked adapter, you will be able to mock basically any request that Dio
can support.
Moreover, http_mock_adapter
package supports request mocking with interceptors which are created via DioInterceptor class, so instead of replacing Dio
's httpClientAdapter
, you can add your mocked interceptor inside the dio.interceptors list by using its add method.
You can find example in examples section of http_mock_adapter
package.
See the following DEV/Medium article for more background.
Usage #
Here is the basic usage scenario of the package (via DioAdapter
):
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
void main() async {
final dio = Dio();
final dioAdapter = DioAdapter();
dio.httpClientAdapter = dioAdapter;
const path = 'https://example.com';
dioAdapter
..onGet(
path,
(request) => request.reply(200, {'message': 'Successfully mocked GET!'}),
)
..onGet(
path,
(request) => request.reply(200, {'message': 'Successfully mocked POST!'}),
);
final onGetResponse = await dio.get(path);
print(onGetResponse.data); // {message: Successfully mocked GET!}
final onPostResponse = await dio.post(path);
print(onPostResponse.data); // {message: Successfully mocked POST!}
}
Installing #
Depend on it #
Add this to your package's pubspec.yaml
file:
dev_dependencies:
http_mock_adapter: ^0.1.6
Install it #
You can install packages from the command line:
with pub
:
$ pub get
...
with flutter
:
$ flutter pub get
...
Alternatively, your editor might support pub get
or flutter pub get
. Check the docs for your editor to learn more.
Import it #
Now in your Dart code, you can use:
import 'package:http_mock_adapter/http_mock_adapter.dart';
Changelog #
All notable changes to this project will be documented in the CHANGELOG.md file.
Authors #
See the AUTHORS file for information regarding the authors of the project.
License #
http-mock-adapter is licensed under the permissive MIT License (LICENSE).
Contribution #
For information regarding contributions, please refer to CONTRIBUTING.md file.