|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:dio/adapter.dart'; |
| 5 | +import 'package:dio/dio.dart'; |
| 6 | +import 'package:easy_localization/easy_localization.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:fluttermvvmtemplate/core/base/model/base_error.dart'; |
| 9 | +import 'package:fluttermvvmtemplate/core/constants/enums/http_request_enum.dart'; |
| 10 | +import 'package:fluttermvvmtemplate/core/base/model/base_model.dart'; |
| 11 | +import 'package:fluttermvvmtemplate/core/extension/network_exntension.dart'; |
| 12 | +import 'package:fluttermvvmtemplate/core/init/network/ICoreDio.dart'; |
| 13 | +import 'package:fluttermvvmtemplate/core/init/network/IResponseModel.dart'; |
| 14 | + |
| 15 | +class CoreDioMock with DioMixin implements ICoreDioFull, Dio { |
| 16 | + final BaseOptions options; |
| 17 | + |
| 18 | + CoreDioMock(this.options) { |
| 19 | + this.options = options; |
| 20 | + this.interceptors.add(InterceptorsWrapper()); |
| 21 | + this.httpClientAdapter = DefaultHttpClientAdapter(); |
| 22 | + } |
| 23 | + @override |
| 24 | + Future<IResponseModel<R>> fetch<R, T extends BaseModel>(String path, |
| 25 | + {HttpTypes type, |
| 26 | + T parseModel, |
| 27 | + data, |
| 28 | + Map<String, Object> queryParameters, |
| 29 | + void Function(int p1, int p2) onReceiveProgress}) async { |
| 30 | + final response = await request(path, data: data, options: Options(method: type.rawValue)); |
| 31 | + |
| 32 | + switch (response.statusCode) { |
| 33 | + case HttpStatus.ok: |
| 34 | + case HttpStatus.accepted: |
| 35 | + final model = _responseParser<R, T>(parseModel, response.data); |
| 36 | + return ResponseModel<R>(data: model); |
| 37 | + default: |
| 38 | + return ResponseModel(error: BaseError("message")); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Future<IResponseModel<R>> fetchNoNetwork<R, T extends BaseModel>(String path, |
| 43 | + {HttpTypes type, |
| 44 | + T parseModel, |
| 45 | + data, |
| 46 | + Map<String, Object> queryParameters, |
| 47 | + void Function(int p1, int p2) onReceiveProgress}) async { |
| 48 | + String dumyJson = """[ |
| 49 | + { |
| 50 | + "userId": 1, |
| 51 | + "id": 1, |
| 52 | + "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", |
| 53 | + "body": "quia et suscipit suscipit recusandae consequuntur expedita et cumnreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto" |
| 54 | + }, |
| 55 | + { |
| 56 | + "userId": 1, |
| 57 | + "id": 2, |
| 58 | + "title": "qui est esse", |
| 59 | + "body": "est rerum tempore vitaensequi sint nihil reprehenderit dolor beatae ea dolores nequenfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendisnqui aperiam non debitis possimus qui neque nisi nulla" |
| 60 | + }]"""; |
| 61 | + final response = jsonDecode(dumyJson); |
| 62 | + final model = _responseParser<R, T>(parseModel, response); |
| 63 | + return ResponseModel<R>(data: model); |
| 64 | + } |
| 65 | + |
| 66 | + R _responseParser<R, T>(BaseModel model, dynamic data) { |
| 67 | + if (data is List) { |
| 68 | + return data.map((e) => model.fromJson(e)).toList().cast<T>() as R; |
| 69 | + } else if (data is Map) { |
| 70 | + return model.fromJson(data) as R; |
| 71 | + } |
| 72 | + return data as R; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class Dio {} |
0 commit comments