Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lomsa-dev/http-mock-adapter
A simple to use mocking package for Dio intended to be used in tests.
https://github.com/lomsa-dev/http-mock-adapter
dart dart-package dartlang dio flutter flutter-package http http-adapter integration-testing mock mockito testing testing-tools unit-testing
Last synced: 4 months ago
JSON representation
A simple to use mocking package for Dio intended to be used in tests.
- Host: GitHub
- URL: https://github.com/lomsa-dev/http-mock-adapter
- Owner: lomsa-dev
- License: mit
- Created: 2020-11-09T10:54:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-11-28T11:17:01.000Z (about 1 year ago)
- Last Synced: 2024-10-09T15:32:41.429Z (4 months ago)
- Topics: dart, dart-package, dartlang, dio, flutter, flutter-package, http, http-adapter, integration-testing, mock, mockito, testing, testing-tools, unit-testing
- Language: Dart
- Homepage: https://pub.dev/packages/http_mock_adapter
- Size: 9.03 MB
- Stars: 65
- Watchers: 6
- Forks: 42
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# http-mock-adapter
[](https://pub.dev/packages/http_mock_adapter "Published package version")
[](https://github.com/dart-lang/lints "Package linter helper")
[](https://github.com/lomsa-dev/http-mock-adapter/actions?query=workflow%3A%22Dart+CI%22 "Dart CI workflow")
[](https://github.com/lomsa-dev/http-mock-adapter/actions/workflows/publish.yml "Publish Package workflow")
[](https://github.com/lomsa-dev/http-mock-adapter#License "Project's LICENSE section")
[](https://codecov.io/gh/lomsa-dev/http-mock-adapter)## Description
`http_mock_adapter` is a simple to use mocking package for [Dio](https://pub.dev/packages/dio) intended to be used in tests. It provides various types and methods to declaratively mock request-response communication.
## Usage
Here is a very basic usage scenario:
```dart
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';void main() async {
//// Exact body check
// final dio = Dio(BaseOptions(contentType: Headers.jsonContentType));
// dioAdapter = DioAdapter(
// dio: dio,
// matcher: const FullHttpRequestMatcher(needsExactBody: true),
// );// Basic setup
final dio = Dio(BaseOptions());
final dioAdapter = DioAdapter(dio: dio);const path = 'https://example.com';
dioAdapter.onGet(
path,
(server) => server.reply(
200,
{'message': 'Success!'},
// Reply would wait for one-sec before returning data.
delay: const Duration(seconds: 1),
),
);final response = await dio.get(path);
print(response.data); // {message: Success!}
}
```### Real-world example
The intended usage domain is in tests when trying to simulate behavior of request-response communication with a server. The [example](https://github.com/lomsa-dev/http-mock-adapter/blob/main/example/main.dart) portrays a decent use case of how one might make good use of the package.
## Installing
### Quick install
You can quickly install the package from the command-line:
With `dart`:
```sh
$ dart pub add --dev http_mock_adapter
...
```With `flutter`:
```sh
$ flutter pub add --dev http_mock_adapter
...
```### Manual install
#### Depend on it
Add this to your package's `pubspec.yaml` file:
```yaml
dev_dependencies:
http_mock_adapter: ^0.6.1
```#### Install it
You can then install the package from the command-line:
With `dart`:
```sh
$ dart pub get
...
```With `flutter`:
```sh
$ flutter pub get
...
```Alternatively, your editor might support `dart 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:
```dart
import 'package:http_mock_adapter/http_mock_adapter.dart';
```## Changelog
All notable changes to this project will be documented in the [CHANGELOG.md](https://github.com/lomsa-dev/http-mock-adapter/blob/main/CHANGELOG.md "Project's CHANGELOG.md file") file.
## Authors
See the [AUTHORS](https://github.com/lomsa-dev/http-mock-adapter/blob/main/AUTHORS "Project's AUTHORS file") file for information regarding the authors of the project.
## License
http-mock-adapter is licensed under the permissive MIT License ([LICENSE](https://github.com/lomsa-dev/http-mock-adapter/blob/main/LICENSE "Copy of the MIT license")).
## Contribution
Made with [contrib.rocks](https://contrib.rocks).
For information regarding contributions, please refer to [CONTRIBUTING.md](https://github.com/lomsa-dev/http-mock-adapter/blob/main/CONTRIBUTING.md "Project's CONTRIBUTING.md file") file.