HTTP 探测改为填写网址,缺本机引擎时立刻失败

HTTP 模式不再单独填端口。完整网址沿用其中的协议、端口和路径;只写主机名或 IP 时按 http 和 80 端口。超时默认改为 5000 毫秒。本机找不到或启动不了 netquality-reflector 时直接说明原因,并建议改用远程节点,不再记成一次全部失败。

Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
Cursor Agentandyanjiu0721 committed 2026-11-22 07:56:16 +00:00
1 parent 846e40c023
commit 918ec8f190
20 files changed
+807 -131

No files matched your search

+214
View File
@@ -0,0 +1,214 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:netquality/models/http_target.dart';
import 'package:netquality/models/port_field.dart';
import 'package:netquality/models/session_config.dart';
import 'package:netquality/probe/http_helper.dart';
import 'package:netquality/probe/probe_controller.dart';
void main() {
test(
'bare hosts become http on port 80 and full URLs keep their own parts',
() {
final bare = parseHttpTarget('rxsend.cn');
expect(bare, isNotNull);
expect(bare!.url, 'http://rxsend.cn/');
expect(bare.port, 80);
expect(bare.scheme, 'http');
final path = parseHttpTarget('example.com/path?q=1');
expect(path!.url, 'http://example.com/path?q=1');
expect(path.port, 80);
final explicit = parseHttpTarget('example.com:8080/a');
expect(explicit!.url, 'http://example.com:8080/a');
expect(explicit.port, 8080);
expect(explicit.scheme, 'http');
final full = parseHttpTarget('https://www.cause-by-siky.top:2025/');
expect(full!.url, 'https://www.cause-by-siky.top:2025/');
expect(full.port, 2025);
expect(full.scheme, 'https');
expect(full.host, 'www.cause-by-siky.top');
final httpsDefault = parseHttpTarget('https://example.com/path');
expect(httpsDefault!.url, 'https://example.com/path');
expect(httpsDefault.port, 443);
final httpDefault = parseHttpTarget('http://example.com/path');
expect(httpDefault!.url, 'http://example.com/path');
expect(httpDefault.port, 80);
final v6 = parseHttpTarget('[2001:db8::1]');
expect(v6!.url, 'http://[2001:db8::1]/');
expect(v6.port, 80);
expect(v6.host, '2001:db8::1');
final v6Port = parseHttpTarget('[2001:db8::1]:8080/a');
expect(v6Port!.url, 'http://[2001:db8::1]:8080/a');
expect(v6Port.port, 8080);
final bareV6 = parseHttpTarget('2001:db8::1');
expect(bareV6!.url, 'http://[2001:db8::1]/');
expect(bareV6.port, 80);
},
);
test('invalid HTTP targets explain the problem in Chinese', () {
expect(httpTargetError(''), '请填写目标地址');
expect(httpTargetError(' '), '请填写目标地址');
expect(httpTargetError('ftp://example.com'), contains('http/https'));
expect(httpTargetError('https://example.com:99999/'), contains('端口'));
expect(httpTargetError('example.com:70000'), contains('端口'));
expect(parseHttpTarget('ftp://example.com'), isNull);
});
test('HTTP validation ignores the separate port and other profiles still require one', () {
final url = _config(
host: 'https://www.cause-by-siky.top:2025/path',
port: 0,
);
expect(url.validate(), isNull);
expect(url.httpTarget!.port, 2025);
expect(url.httpTarget!.url, 'https://www.cause-by-siky.top:2025/path');
final bare = _config(host: 'rxsend.cn', port: 19090);
expect(bare.validate(), isNull);
expect(bare.httpTarget!.url, 'http://rxsend.cn/');
expect(bare.httpTarget!.port, 80);
final bad = _config(host: 'ftp://example.com', port: 443);
expect(bad.validate(), contains('http/https'));
final family = _config(
host: 'https://203.0.113.9/path',
port: 0,
allow4: false,
);
expect(family.validate(), contains('IPv4'));
final tcp = _config(host: '203.0.113.8', port: 0, profileId: 'tcp-connect');
expect(tcp.validate(), contains('端口'));
expect(tcp.httpTarget, isNull);
});
test('HTTP timeout default is 5 seconds and a typed timeout stays', () {
expect(timeoutDefaultFor('http'), 5000);
expect(timeoutDefaultFor('nq-tcp'), 1000);
expect(
timeoutAfterProfileChange(
current: '1000',
previousDefault: 1000,
nextDefault: 5000,
),
'5000',
);
expect(
timeoutAfterProfileChange(
current: '2000',
previousDefault: 1000,
nextDefault: 5000,
),
'2000',
);
expect(
timeoutAfterProfileChange(
current: '5000',
previousDefault: 5000,
nextDefault: 1000,
),
'1000',
);
expect(
timeoutAfterProfileChange(
current: '',
previousDefault: 1000,
nextDefault: 5000,
),
'5000',
);
});
test(
'starting local HTTP without a helper does not open a loss run',
() async {
locateProbeHelperOverride = () => null;
addTearDown(() => locateProbeHelperOverride = null);
final controller = ProbeController();
addTearDown(controller.dispose);
await controller.start(
_config(host: 'rxsend.cn', port: 443),
nodeToken: '',
echoToken: '',
);
expect(controller.view.running, isFalse);
expect(controller.view.phase, '无法开始');
expect(controller.view.banner, helperMissingNote);
expect(controller.view.banner, contains('远程节点'));
expect(controller.view.samples, isEmpty);
expect(controller.view.summary.completed, 0);
expect(controller.view.report, isNull);
final started = DateTime.now();
await controller.start(
_config(host: 'https://www.cause-by-siky.top:2025/', port: 9),
nodeToken: '',
echoToken: '',
);
expect(DateTime.now().difference(started).inSeconds, lessThan(2));
expect(controller.view.running, isFalse);
expect(controller.view.report, isNull);
expect(controller.view.summary.completed, 0);
},
);
test(
'a missing local helper fails immediately and names the remote source',
() async {
final message = await localHttpBlockedReason(
binary: '/no/such/netquality-reflector',
);
expect(message, helperMissingNote);
expect(message, contains('远程节点'));
expect(message, contains('没有开始'));
},
);
test('a helper that cannot be executed fails immediately', () async {
final dir = Directory.systemTemp.createTempSync('nq-helper');
addTearDown(() => dir.deleteSync(recursive: true));
final file = File('${dir.path}/netquality-reflector')
..writeAsStringSync('not a binary');
final message = await localHttpBlockedReason(binary: file.path);
expect(message, helperStartFailedNote);
expect(message, contains('远程节点'));
});
}
SessionConfig _config({
required String host,
required int port,
String profileId = 'http',
bool allow4 = true,
}) {
return SessionConfig(
remote: false,
nodeId: '',
nodes: const [],
host: host,
port: port,
profileId: profileId,
intervalMs: 300,
timeoutMs: 5000,
durationSec: 60,
packetSize: 128,
dnsName: 'example.com',
payloadHex: '',
expectHex: '',
allowIPv4: allow4,
);
}
+32
View File
@@ -6,6 +6,7 @@ import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:netquality/metrics/sample_book.dart';
import 'package:netquality/probe/game_codec.dart';
import 'package:netquality/probe/http_helper.dart';
import 'package:netquality/probe/probe_loop.dart';
import 'package:netquality/probe/remote_client.dart';
@@ -389,6 +390,37 @@ void main() {
expect(samples.single.detail['httpVersion'], '1.1');
expect(samples.single.summary.failurePct, 0);
});
test('a missing local http helper records no loss sample', () async {
final samples = <ProbeSample>[];
final loop = ProbeLoop(helperPath: '/no/such/netquality-reflector');
await expectLater(
loop.run(
request: const ProbeRequest(
host: 'http://127.0.0.1/health',
port: 80,
profileId: 'http',
interval: Duration(milliseconds: 200),
timeout: Duration(seconds: 1),
packetSize: 64,
echoToken: '',
dnsName: '',
payloadHex: '',
expectHex: '',
),
onSample: samples.add,
cancelled: () => false,
),
throwsA(
isA<LocalHttpHelperException>().having(
(error) => error.message,
'message',
contains('远程节点'),
),
),
);
expect(samples, isEmpty);
});
}
Future<List<ProbeSample>> _collect(
+37
View File
@@ -81,6 +81,43 @@ void main() {
},
);
testWidgets('HTTP mode uses a URL, a 5 second timeout, and no port field', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
tester.view.physicalSize = const Size(1280, 1800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(const NetQualityApp());
await tester.pumpAndSettle();
await tester.ensureVisible(find.byKey(const Key('profile')));
await tester.tap(find.byKey(const Key('profile')));
await tester.pumpAndSettle();
await tester.tap(find.text('HTTP').last);
await tester.pumpAndSettle();
expect(find.byKey(const Key('port-field')), findsNothing);
expect(find.text('HTTP 默认 5000 毫秒'), findsOneWidget);
expect(find.text('5000'), findsWidgets);
expect(
find.text('只填主机名或 IP 时按 http:// 和端口 80。完整网址使用其中的协议、端口和路径。'),
findsOneWidget,
);
expect(find.byKey(const Key('target-field')), findsOneWidget);
await tester.enterText(find.byKey(const Key('timeout-field')), '2000');
await tester.tap(find.byKey(const Key('profile')));
await tester.pumpAndSettle();
await tester.tap(find.text('合作反射 UDP').last);
await tester.pumpAndSettle();
expect(find.byKey(const Key('port-field')), findsOneWidget);
expect(find.text('19091'), findsWidgets);
expect(find.text('2000'), findsOneWidget);
expect(find.text('HTTP 默认 5000 毫秒'), findsNothing);
});
testWidgets('editing a node keeps its id and does not reveal the token', (
tester,
) async {