154 lines
5.0 KiB
Dart
154 lines
5.0 KiB
Dart
// 2026-04-16 | test_action_api.dart | 动作接口测试 | 验证api_action.php GET/POST请求
|
||
// 运行: dart run scripts/test_action_api.dart
|
||
|
||
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
const String baseUrl = 'https://eat.wktyl.com/api';
|
||
|
||
Future<void> main() async {
|
||
print('=== api_action.php 接口测试 ===\n');
|
||
print('目标: 验证 GET 和 POST 两种请求方式是否都能正常工作\n');
|
||
|
||
const testId = 32892;
|
||
|
||
// 1. IP状态查询 (GET)
|
||
print('━━━ 1. IP状态查询 (GET) ━━━');
|
||
await testGet('ip_status');
|
||
|
||
// 2. 点赞 - GET方式
|
||
print('\n━━━ 2. 点赞 - GET方式 ━━━');
|
||
await testGet('like', params: {'type': 'recipe', 'id': '$testId', 'action': 'like'});
|
||
|
||
// 3. 点赞 - POST方式 (JSON body)
|
||
print('\n━━━ 3. 点赞 - POST方式 (JSON body) ━━━');
|
||
await testPost('like', body: {'type': 'recipe', 'id': testId, 'action': 'like'});
|
||
|
||
// 4. 取消点赞 - GET方式
|
||
print('\n━━━ 4. 取消点赞 - GET方式 ━━━');
|
||
await testGet('like', params: {'type': 'recipe', 'id': '$testId', 'action': 'unlike'});
|
||
|
||
// 5. 评分 - GET方式
|
||
print('\n━━━ 5. 评分 - GET方式 ━━━');
|
||
await testGet('rate', params: {'type': 'recipe', 'id': '$testId', 'score': '4'});
|
||
|
||
// 6. 评分 - POST方式 (JSON body)
|
||
print('\n━━━ 6. 评分 - POST方式 (JSON body) ━━━');
|
||
await testPost('rate', body: {'type': 'recipe', 'id': testId, 'score': 5});
|
||
|
||
// 7. 浏览量 - POST方式
|
||
print('\n━━━ 7. 浏览量 - POST方式 ━━━');
|
||
await testPost('view', body: {'type': 'recipe', 'id': testId, 'count': 1});
|
||
|
||
// 8. CORS预检 - OPTIONS
|
||
print('\n━━━ 8. CORS预检 (OPTIONS) ━━━');
|
||
await testOptions();
|
||
|
||
print('\n=== 测试完成 ===');
|
||
}
|
||
|
||
Future<void> testGet(String act, {Map<String, String>? params}) async {
|
||
final client = HttpClient();
|
||
try {
|
||
final queryParams = {'act': act, ...?params};
|
||
final queryString = queryParams.entries.map((e) => '${e.key}=${Uri.encodeComponent(e.value)}').join('&');
|
||
final url = Uri.parse('$baseUrl/api_action.php?$queryString');
|
||
print(' GET $url');
|
||
|
||
final request = await client.getUrl(url);
|
||
request.headers.set('Accept', 'application/json');
|
||
final response = await request.close();
|
||
final body = await response.transform(utf8.decoder).join();
|
||
|
||
_printResult(response.statusCode, body);
|
||
} catch (e) {
|
||
print(' ❌ 请求失败: $e');
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
Future<void> testPost(String act, {Map<String, dynamic>? body}) async {
|
||
final client = HttpClient();
|
||
try {
|
||
final url = Uri.parse('$baseUrl/api_action.php?act=$act');
|
||
print(' POST $url');
|
||
if (body != null) {
|
||
print(' Body: ${jsonEncode(body)}');
|
||
}
|
||
|
||
final request = await client.postUrl(url);
|
||
request.headers.set('Accept', 'application/json');
|
||
request.headers.set('Content-Type', 'application/json');
|
||
if (body != null) {
|
||
request.write(jsonEncode(body));
|
||
}
|
||
|
||
final response = await request.close();
|
||
final responseBody = await response.transform(utf8.decoder).join();
|
||
|
||
_printResult(response.statusCode, responseBody);
|
||
} catch (e) {
|
||
print(' ❌ 请求失败: $e');
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
Future<void> testOptions() async {
|
||
final client = HttpClient();
|
||
try {
|
||
final url = Uri.parse('$baseUrl/api_action.php');
|
||
print(' OPTIONS $url');
|
||
|
||
final request = await client.openUrl('OPTIONS', url);
|
||
request.headers.set('Accept', 'application/json');
|
||
request.headers.set('Access-Control-Request-Method', 'POST');
|
||
request.headers.set('Origin', 'http://localhost');
|
||
|
||
final response = await request.close();
|
||
print(' 状态码: ${response.statusCode}');
|
||
print(' CORS头: Access-Control-Allow-Methods = ${response.headers.value('access-control-allow-methods') ?? '未设置'}');
|
||
print(' CORS头: Access-Control-Allow-Origin = ${response.headers.value('access-control-allow-origin') ?? '未设置'}');
|
||
|
||
if (response.statusCode == 204) {
|
||
print(' ✅ CORS预检通过');
|
||
} else {
|
||
print(' ⚠️ 预期204,实际${response.statusCode}');
|
||
}
|
||
} catch (e) {
|
||
print(' ❌ 请求失败: $e');
|
||
} finally {
|
||
client.close();
|
||
}
|
||
}
|
||
|
||
void _printResult(int statusCode, String body) {
|
||
try {
|
||
final json = jsonDecode(body) as Map<String, dynamic>;
|
||
final code = json['code'];
|
||
final message = json['message'];
|
||
final data = json['data'];
|
||
|
||
if (code == 200) {
|
||
print(' ✅ HTTP $statusCode | code: $code | $message');
|
||
if (data != null) {
|
||
final dataStr = jsonEncode(data);
|
||
if (dataStr.length > 200) {
|
||
print(' data: ${dataStr.substring(0, 200)}...');
|
||
} else {
|
||
print(' data: $dataStr');
|
||
}
|
||
}
|
||
} else {
|
||
print(' ⚠️ HTTP $statusCode | code: $code | $message');
|
||
}
|
||
} catch (e) {
|
||
if (body.length > 200) {
|
||
print(' HTTP $statusCode | ${body.substring(0, 200)}...');
|
||
} else {
|
||
print(' HTTP $statusCode | $body');
|
||
}
|
||
}
|
||
}
|