diff --git a/lib/src/interceptors/dio_interceptor.dart b/lib/src/interceptors/dio_interceptor.dart index bbb8357..f851754 100644 --- a/lib/src/interceptors/dio_interceptor.dart +++ b/lib/src/interceptors/dio_interceptor.dart @@ -20,7 +20,8 @@ class YxNetInspectorDioInterceptor extends Interceptor { } @override - void onResponse(Response response, ResponseInterceptorHandler handler) { + void onResponse( + Response response, ResponseInterceptorHandler handler,) { // 记录响应 try { // 计算请求耗时(如果可能) @@ -36,7 +37,7 @@ class YxNetInspectorDioInterceptor extends Interceptor { // 这里没有准确的耗时,因为 logRequest 记录了开始时间。 // Controller 会根据 ID 自动计算耗时:Duration = Now - StartTime ); - } catch (e) { + } on Object catch (_) { // 忽略日志记录错误,避免影响业务 } handler.next(response); @@ -53,7 +54,7 @@ class YxNetInspectorDioInterceptor extends Interceptor { error: err.message ?? err.toString(), statusCode: err.response?.statusCode, ); - } catch (e) { + } on Object catch (_) { // 忽略日志记录错误 } handler.next(err); diff --git a/lib/src/models/network_log_entry.dart b/lib/src/models/network_log_entry.dart index 750c6f3..475f514 100644 --- a/lib/src/models/network_log_entry.dart +++ b/lib/src/models/network_log_entry.dart @@ -134,7 +134,7 @@ class NetworkLogEntry { final buffer = StringBuffer('curl -X $method "$url"'); headers?.forEach((key, value) { // 转义双引号以兼容所有平台 - final escapedValue = value.toString().replaceAll('"', '\\"'); + final escapedValue = value.toString().replaceAll('"', r'\"'); buffer.write(' -H "$key: $escapedValue"'); }); if (requestData != null) { @@ -144,12 +144,12 @@ class NetworkLogEntry { } else { try { body = jsonEncode(requestData); - } catch (_) { + } on Object catch (_) { body = requestData.toString(); } } // 转义双引号和反斜杠 - final escapedBody = body.replaceAll('\\', '\\\\').replaceAll('"', '\\"'); + final escapedBody = body.replaceAll(r'\', r'\\').replaceAll('"', r'\"'); buffer.write(' -d "$escapedBody"'); } return buffer.toString(); diff --git a/lib/src/widgets/panel/password_dialog.dart b/lib/src/widgets/panel/password_dialog.dart index 2be07ee..b3b4087 100644 --- a/lib/src/widgets/panel/password_dialog.dart +++ b/lib/src/widgets/panel/password_dialog.dart @@ -62,7 +62,7 @@ class _PasswordDialogState extends State { borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( - color: Colors.black.withOpacity(0.1), + color: Colors.black.withValues(alpha: 0.1), blurRadius: 20, offset: const Offset(0, 10), ), diff --git a/test/unit/dio_interceptor_test.dart b/test/unit/dio_interceptor_test.dart index 0f75912..cae36f0 100644 --- a/test/unit/dio_interceptor_test.dart +++ b/test/unit/dio_interceptor_test.dart @@ -12,7 +12,7 @@ void main() { setUp(() { controller = YxNetInspectorController.instance; // Initialize controller with logging enabled (debug mode) - controller.initialize(const YxNetInspectorConfig(showInDebugMode: true)); + controller.initialize(const YxNetInspectorConfig()); dio = Dio(); // Add our inspector interceptor FIRST @@ -23,14 +23,16 @@ void main() { final initialLogCount = controller.logs.length; // Add a mock interceptor to return success response immediately - dio.interceptors.add(InterceptorsWrapper( - onRequest: (options, handler) { - // Allow request to proceed (so our inspector sees it) - handler.next(options); - }, - onError: (e, handler) => handler.next(e), - onResponse: (e, handler) => handler.next(e), - )); + dio.interceptors.add( + InterceptorsWrapper( + onRequest: (options, handler) { + // Allow request to proceed (so our inspector sees it) + handler.next(options); + }, + onError: (e, handler) => handler.next(e), + onResponse: (e, handler) => handler.next(e), + ), + ); // Mock Adapter to avoid real network dio.httpClientAdapter = _MockAdapter((options) { @@ -43,7 +45,7 @@ void main() { ); }); - await dio.get('https://example.com/api/test'); + await dio.get('https://example.com/api/test'); expect(controller.logs.length, greaterThan(initialLogCount)); final latestLog = controller.logs.first; @@ -66,8 +68,9 @@ void main() { }); try { - await dio.post('https://example.com/api/fail', data: {'foo': 'bar'}); - } catch (e) { + await dio.post('https://example.com/api/fail', + data: {'foo': 'bar'},); + } on Object catch (_) { // Expected } @@ -82,13 +85,15 @@ void main() { } class _MockAdapter implements HttpClientAdapter { + _MockAdapter(this._mockResponse); final ResponseBody Function(RequestOptions options) _mockResponse; - _MockAdapter(this._mockResponse); - @override - Future fetch(RequestOptions options, - Stream>? requestStream, Future? cancelFuture) async { + Future fetch( + RequestOptions options, + Stream>? requestStream, + Future? cancelFuture, + ) async { return _mockResponse(options); } diff --git a/test/widget/password_protection_test.dart b/test/widget/password_protection_test.dart index ef2225e..c9051aa 100644 --- a/test/widget/password_protection_test.dart +++ b/test/widget/password_protection_test.dart @@ -17,7 +17,7 @@ void main() { testWidgets('Panel should be unlocked by default (no password)', (WidgetTester tester) async { - controller.initialize(const YxNetInspectorConfig(showInDebugMode: true)); + controller.initialize(const YxNetInspectorConfig()); expect(controller.isUnlocked, isTrue); @@ -40,9 +40,8 @@ void main() { testWidgets('Panel should be locked if password is set', (WidgetTester tester) async { controller.initialize(const YxNetInspectorConfig( - showInDebugMode: true, password: '123', - )); + ),); expect(controller.isUnlocked, isFalse); @@ -61,15 +60,14 @@ void main() { expect(find.byType(PasswordDialog), findsOneWidget); expect(find.text('安全访问'), findsOneWidget); expect( - find.byIcon(Icons.close), findsNothing); // Should hide panel content + find.byIcon(Icons.close), findsNothing,); // Should hide panel content }); testWidgets('Entering correct password should unlock panel', (WidgetTester tester) async { controller.initialize(const YxNetInspectorConfig( - showInDebugMode: true, password: '123', - )); + ),); await tester.pumpWidget( MaterialApp( @@ -97,9 +95,8 @@ void main() { testWidgets('Entering wrong password should show error', (WidgetTester tester) async { controller.initialize(const YxNetInspectorConfig( - showInDebugMode: true, password: '123', - )); + ),); await tester.pumpWidget( MaterialApp(