1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:odindio/odin_dio_interceptor.dart'; import 'package:odindio/odindio.dart'; import 'package:odindio/odindio_platform_interface.dart'; import 'package:odindio/odindio_method_channel.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'models/post_model.dart'; import 'models/student_model.dart'; import 'models/ten_model.dart'; import 'models/test_model.dart'; import 'package:path/path.dart' as path; import 'dart:io'; import 'odindio_test.reflectable.dart';
class MockOdindioPlatform with MockPlatformInterfaceMixin implements OdindioPlatform { @override Future<String?> getPlatformVersion() => Future.value('42'); }
class MultiRequest { static Future<Response<dynamic>> req1<T>() async { OdinDio odindioPlugin = OdinDio(); odindioPlugin.setBaseUrl("http://httpbin.org"); return await odindioPlugin.get('/json'); }
static Future<Response<dynamic>> req2<T>() async { var odindioPlugin = OdinDio(); odindioPlugin.setBaseUrl("http://tenapi.cn"); return await odindioPlugin.get('/acg'); } }
void main() { initializeReflectable(); final OdindioPlatform initialPlatform = OdindioPlatform.instance;
test('$MethodChannelOdindio is the default instance', () { expect(initialPlatform, isInstanceOf<MethodChannelOdindio>()); }); test('getJson-1', () async { OdinDio odindioPlugin = OdinDio(); odindioPlugin.setBaseUrl("http://httpbin.org"); odindioPlugin.removeDioLog(); var odi1 = OdinDioInterceptor(onRequestHandler: (request, handler) { print("onRequestHandler1"); }, onResponseHandler: (response, handler) { print("onResponseHandler1"); }, onErrorHandler: (err, handler) { print("err1"); }); var odi2 = OdinDioInterceptor(onRequestHandler: (request, handler) { print("onRequestHandler2"); }, onResponseHandler: (response, handler) { print("onResponseHandler2"); }, onErrorHandler: (err, handler) { print("err2"); }); odindioPlugin.interceptors().addAll([odi1, odi2]); odindioPlugin.interceptors().remove(odi1); var model = await odindioPlugin.getModel<TestModel>('/json'); print(json.encode(model)); }); test('getJson-2', () async { OdinDio odindioPlugin = OdinDio(); odindioPlugin.removeDioLog(); odindioPlugin.addDioLog(); odindioPlugin.setBaseUrl("http://192.168.1.145:8899"); var models = await odindioPlugin.getModels<StudentModel>('/stus'); print(json.encode(models)); expect(models.length, greaterThan(0)); }); test('getJson-3', () async { var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("https://tenapi.cn"); var model = await odinDioPlugin.getModel<TenModel>("/qqname", queryParameters: {"qq": "123456"}, options: Options(responseType: ResponseType.json)); print(json.encode(model)); expect(model.name, '腾讯视频'); });
test('getStream', () async { var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); var stream = await odinDioPlugin.getStream('/stream'); var lst = <int>[]; var bytes = await stream.first; lst = List.from(bytes.toList()); print(utf8.decode(lst)); expect(utf8.decode(lst).length, greaterThan(0)); });
test('getBytes', () async { var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); var lst = await odinDioPlugin.getBytes('/stream'); print(utf8.decode(lst)); expect(utf8.decode(lst).length, greaterThan(0)); });
test('postStu', () async { var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); odinDioPlugin.removeDioLog(); var data = odinDioPlugin.dataToFormData({"name": "bakabaka"}); var response5 = await odinDioPlugin.post('/postStu', data: data, options: Options(headers: {"customNewPost": "newPost"})); var model = odinDioPlugin.dioResultToModel<PostModel>(response5.data); print("model: ${json.encode(model)}"); expect(model.code, 0); });
test('multirequest', () async { var odinDioPlugin = OdinDio(); var response6 = await odinDioPlugin .multiRequest([MultiRequest.req1(), MultiRequest.req2()]); var rep1 = response6[0]; var rep1model = odinDioPlugin.dioResultToModel<TestModel>(rep1.data); print(json.encode(rep1model)); expect(rep1model.slideshow!.author, "Yours Truly"); var rep2 = response6[1]; print(json.encode(rep2.data.length)); expect(rep2.data!.length, greaterThan(0)); });
test('downloadFile', () async { double currentProgress = 0.0; var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); final cur = path.dirname(Platform.script.path).substring(1); var savePath = path.join(cur, 'download.txt'); var response7 = await odinDioPlugin.downloadFile( '/static/output.txt', savePath, onReceiveProgress: (received, total) { if (total != -1) { var downloadRatio = (received / total * 100).toStringAsFixed(0); currentProgress = received / total; } }); var f = File(savePath); expect(f.readAsStringSync(), equals('I am a text file')); f.deleteSync(recursive: false); });
test('uploadFile', () async { var odinDioPlugin = OdinDio(); final cur = path.dirname(Platform.script.path).substring(1); var uploadFile1 = path.join(cur, 'upload1.txt'); var uploadFile2 = path.join(cur, 'upload2.txt'); var uploadFile3 = path.join(cur, 'upload3.txt'); var formData = odinDioPlugin.dataToFormData({ 'name': 'wendux', 'age': 25, 'file': await MultipartFile.fromFile(uploadFile1, filename: 'upload1.txt'), 'files': [ await MultipartFile.fromFile(uploadFile2, filename: 'upload2.txt'), await MultipartFile.fromFile(uploadFile3, filename: 'upload3.txt'), ] }); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); var response6 = await odinDioPlugin.post('/upload', data: formData); print(response6.data); });
test('post stream data', () async { var odinDioPlugin = OdinDio(); var formData = {'name': 'wendux', 'age': 25}; var lst = utf8.encode(formData.toString()); odinDioPlugin.setBaseUrl("http://192.168.1.145:8899"); var response6 = await odinDioPlugin.postDataByStream('/stu', data: odinDioPlugin.listIntToStreamFormData(lst), options: Options(headers: { Headers.contentLengthHeader: lst.length, })); print(response6.data); });
test('resolve', () async { var odinDioPlugin = OdinDio(); odinDioPlugin.setBaseUrl("http://httpbin.org"); var odi1 = OdinDioInterceptor(onRequestHandler: (request, handler) { print("onRequestHandler1"); return handler .resolve(Response(requestOptions: request, data: 'fake data')); }, onResponseHandler: (response, handler) { print("onResponseHandler1"); }, onErrorHandler: (err, handler) { print("err1"); }); odinDioPlugin.interceptors().add(odi1); try { var model = await odinDioPlugin.get('/json'); } on DioError catch (e) { if (e.response != null) { print(e.response!.data); print(e.response!.headers); print(e.response!.requestOptions); } else { print(e.requestOptions); print(e.message); } } }); }
|