dio_mock_interceptor 2.1.0 copy "dio_mock_interceptor: ^2.1.0" to clipboard
dio_mock_interceptor: ^2.1.0 copied to clipboard

This widget help you to mock backend responses in flutter project.

Flutter Dio Mock Interceptor #

License: MIT

This widget help you to mock backend responses in flutter project.

Environment #

The widget was only tested on following environment,

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

Usage #

  • Install:

    dev_dependencies:
      dio_mock_interceptor: ^2.1.0
    
  • Create a mock folder in your project, add json files to mock http responses, example:

    [
      {
        "path": "/api/basic/data",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "success": true,
          "code": "0000",
          "result": {
              "test": "test"
          }
        }
      },
      {
        "path": "/api/basic/data/empty",
        "method": "POST",
        "statusCode": 200,
        "data": {}
      }
    ]
    
  • Setup mock folder to assets section of pubspec.yaml:

    assets:
      - mock/
    
  • Add interceptor to Dio:

    import 'package:dio_mock_interceptor/dio_mock_interceptor.dart';
      
    dio.interceptors.add(MockInterceptor());
    
  • 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:

    [
      {
        "path": "/api/template/without-data-block",
        "method": "POST",
        "statusCode": 200,
        "template": {
          "size": 100000,
          "content": {
            "id": "test${index}",
            "name": "name_${index}"
          }
        }
      },
      {
        "path": "/api/template/with-data-block",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "listA": "${template}"
        },
        "template": {
          "size": 1000,
          "content": {
            "id": "test${index}",
            "name": "name_${index}"
          }
        }
      },
      {
        "path": "/api/template/with-data-block/ex2",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "listA": "${template}",
          "field2": {
            "listB": "${template}"
          }
        },
        "template": {
          "size": 1000,
          "content": {
            "id": "test${index}",
            "name": "name_${index}"
          }
        }
      },
      {
        "path": "/api/templates/ex1",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "listA": "${templates.name1}",
          "field": {
            "listB": "${templates.name2}"
          }
        },
        "templates": {
          "name1": {
            "size": 1000,
            "content": {
              "id": "test${index}",
              "name": "name_${index}"
            }
          },
          "name2": {
            "size": 10,
            "content": {
              "id": "test2${index}",
              "name": "name2_${index}"
            }
          }
        }
      }
    ]
    
  • Expression example:

    [
      {
        "path": "/api/expression/req-data",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "desc": "Hi ${req['data']['name']}, I am ${req['data']['name2'] + '_varSuffix'}",
          "desc2": "test header, ${req['headers']['content-type']}",
          "desc3": "test queryParameter, ${req['queryParameters']['name3']}",
          "desc4": "test baseUrl, ${req['baseUrl']}",
          "desc5": "test method, ${req['method']}",
          "desc6": "test path, ${req['path']}"
        }
      },
      {
        "path": "/api/expression/req-data/form-data",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "desc": "Hi ${req['data']['name']}, test date: ${req['data']['date']}"
        }
      },
      {
        "path": "/api/expression/vars",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "listA": "${templates.name1}",
          "field": {
            "listB": "${templates.name2}"
          },
          "arry": "${groups}",
          "objA": "${obj}"
        },
        "vars": {
          "n": 5,
          "groups": [
            "May",
            "YongXin",
            "John"
          ],
          "obj": {
            "name": "objName"
          }
        },
        "templates": {
          "name1": {
            "size": 1000,
            "content": {
              "id": "test${index}",
              "group": "g_${groups[index%3]}",
              "name": "name_${index}",
              "req-data-name": "test_${req['data']['name']}"
            }
          },
          "name2": {
            "size": 10,
            "content": {
              "id": "test2${index}",
              "name": "name2_${index}"
            }
          }
        }
      },
      {
        "path": "/api/expression/vars/template-ex",
        "method": "POST",
        "statusCode": 200,
        "data": {
          "id": "yong-xin",
          "listA": "${template}"
        },
        "vars": {
          "n": 5,
          "groups": [
            "May",
            "YongXin",
            "John"
          ],
          "obj": {
            "name": "objName"
          }
        },
        "template": {
          "size": 1000,
          "content": {
            "id": "test${index}",
            "group": "g_${groups[index%3]}",
            "name": "name_${index}",
            "req-data-name": "test_${(req['data']['name'].contains('Mercury')? 'a':'b')}"
          }
        }
      }
    ]
    

License #

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

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

6
likes
140
pub points
72%
popularity

Publisher

verified publisheryong-xin.tech

This widget help you to mock backend responses in flutter project.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

dio, flutter, template_expressions

More

Packages that depend on dio_mock_interceptor