flutter_api_mock_server 0.0.1
flutter_api_mock_server: ^0.0.1 copied to clipboard
A Flutter package to simulate mock API responses and network conditions like errors and latency.
flutter_api_mock_server #
A simple mock server for simulating API responses in Flutter apps. Define mock responses for different HTTP methods and paths, simulate network delays, and log requests and responses. Useful for testing and development.
Features #
- Add mock responses for any HTTP method and path
- Simulate network delays for each mock response
- Log requests and responses for debugging
- Remove or clear mock responses
- Fetch mock data using
fetchMockDatafor integration in your app - Returns 404 response if no mock is found
Getting started #
Add to your pubspec.yaml:
dependencies:
flutter_api_mock_server: ^<latest_version>
Import in your Dart code:
import 'package:flutter_api_mock_server/flutter_api_mock_server.dart';
Usage #
Create a mock server and add mock responses:
final mockServer = MockServer();
mockServer.addMockResponse(
path: '/api/user',
method: HttpMethod.get,
response: '{"id":1,"name":"John"}',
delay: Duration(milliseconds: 500),
statusCode: 200,
);
Fetch mock data for a request:
final response = await mockServer.fetchMockData('/api/user', HttpMethod.get);
print(response.response); // '{"id":1,"name":"John"}'
See the /example/main.dart for a complete Flutter demo app showing how to manage and use mocks interactively.
Additional information #
- For more details, see the API docs and example.
- Issues and contributions are welcome via the GitHub repository.
- Licensed under the terms in LICENSE.