dio_mocked_responses 1.1.0 copy "dio_mocked_responses: ^1.1.0" to clipboard
dio_mocked_responses: ^1.1.0 copied to clipboard

Interceptor that help you to mock backend responses in flutter project.

Flutter Dio Mock Interceptor #

Forked from Flutter Dio Mock Interceptor

License: MIT Publish to pub.dev

Environment #

The widget was only tested on following environment,

  • Flutter: 3.7.5+ (with sound null safety)
  • Dio: 5.0.0+

Usage #

Add interceptor to Dio

    final dio = Dio()
      ..interceptors.add(
        MockInterceptor(
          basePath: 'test/dio_responses',
        ),
      );

basePath is the path to the folder containing the mock responses. The path is relative to the root of the assets folder. His default value is test/dio_responses.

By example, if you want to test your backend API for the route api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts, create the file at test/dio_responses/api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts.json with:

{
"GET": {
  "statusCode": 200,
  "data": {
    "contacts": [
      {
        "id": 1,
        "name": "Seth Ladd"
      },
      {
        "id": 2,
        "name": "Eric Seidel"
      }
    ]
  }
}
}
  • For this example:
  test('Load file with Interceptor', () async {
    final dio = Dio()
      ..interceptors.add(
        MockInterceptor(basePath: 'test/dio_responses'),
      );

    final response = await dio.get(
      'api/client/55036c03-6d3f-4053-9547-c08a32ac9aca/contacts',
    );
    expect(response.statusCode, equals(200));
    expect(response.data, isNotNull);
    final json = jsonDecode(response.data);
    final contacts = json['contacts'];

    final seth = contacts.first;
    expect(seth['id'], 1);
    expect(seth['name'], 'Seth Ladd');

    final eric = contacts.last;
    expect(eric['id'], 2);
    expect(eric['name'], 'Eric Seidel');
  });
  • Dio post example:
Response response = await dio.post("/api/basic/data");
String json = response.data;
if (json.isEmpty) {
  throw Exception('response is empty');
}
Map<String, dynamic> data = jsonDecode(json);
bool isSuccess = data['success'] as bool; // true
Map<String, dynamic> result = data['result']; // result['test'] = 'test'
  • Template example:
{
  "POST": {
    "statusCode": 200,
    "template": {
      "size": 100000,
      "content": {
        "id": "test${index}",
        "name": "name_${index}"
      }
    }
  }
}

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2024-present Listo Paye

Copyright (c) 2023-present Yong-Xin Technology Ltd.

5
likes
0
points
107
downloads

Publisher

verified publisherbenoitfontaine.fr

Weekly Downloads

Interceptor that help you to mock backend responses in flutter project.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

dio, flutter, template_expressions

More

Packages that depend on dio_mocked_responses