149 lines
4.6 KiB
Dart
149 lines
4.6 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
class ApiTester {
|
|
final String baseUrl;
|
|
final HttpClient httpClient;
|
|
|
|
ApiTester(this.baseUrl) : httpClient = HttpClient();
|
|
|
|
Future<Map<String, dynamic>> makeRequest(String method, String path, [Map<String, dynamic>? body]) async {
|
|
final uri = Uri.parse('$baseUrl$path');
|
|
print('\n🔍 $method $uri');
|
|
|
|
late HttpClientRequest request;
|
|
switch (method.toUpperCase()) {
|
|
case 'GET':
|
|
request = await httpClient.getUrl(uri);
|
|
break;
|
|
case 'POST':
|
|
request = await httpClient.postUrl(uri);
|
|
break;
|
|
case 'PUT':
|
|
request = await httpClient.putUrl(uri);
|
|
break;
|
|
case 'DELETE':
|
|
request = await httpClient.deleteUrl(uri);
|
|
break;
|
|
default:
|
|
throw ArgumentError('Unsupported HTTP method: $method');
|
|
}
|
|
|
|
request.headers.set('Content-Type', 'application/json');
|
|
|
|
if (body != null) {
|
|
final jsonBody = jsonEncode(body);
|
|
print('📤 Request Body: $jsonBody');
|
|
request.write(jsonBody);
|
|
}
|
|
|
|
final response = await request.close();
|
|
final responseBody = await response.transform(utf8.decoder).join();
|
|
|
|
print('📊 Status: ${response.statusCode}');
|
|
print('📥 Response: $responseBody');
|
|
|
|
try {
|
|
return {
|
|
'statusCode': response.statusCode,
|
|
'body': jsonDecode(responseBody),
|
|
};
|
|
} catch (e) {
|
|
return {
|
|
'statusCode': response.statusCode,
|
|
'body': responseBody,
|
|
};
|
|
}
|
|
}
|
|
|
|
Future<void> testAllEndpoints() async {
|
|
print('🚀 Starting API Tests for $baseUrl');
|
|
print('=' * 50);
|
|
|
|
try {
|
|
// Test 1: Create a peer
|
|
print('\n📋 TEST 1: Create a new peer');
|
|
final createResponse = await makeRequest('POST', '/api/peers');
|
|
|
|
if (createResponse['statusCode'] != 200 || createResponse['body']['success'] != true) {
|
|
print('❌ Failed to create peer');
|
|
return;
|
|
}
|
|
|
|
final peer = createResponse['body']['peer'] as Map<String, dynamic>;
|
|
final publicKey = peer['publicKey'] as String;
|
|
final peerIP = peer['ip'] as String;
|
|
|
|
print('✅ Created peer successfully');
|
|
print(' Public Key: $publicKey');
|
|
print(' IP: $peerIP');
|
|
|
|
// Test 2: Set speed limit
|
|
e print('\n📋 TEST 2: Set speed limit (62500 bytes/s = 500 kbps)');
|
|
final speedResponse = await makeRequest('POST', '/api/peers/speed-limit', {
|
|
'publicKey': publicKey,
|
|
'bytesPerSecond': 62500, // 500 kbps = 500000 bits/s = 62500 bytes/s
|
|
});
|
|
|
|
if (speedResponse['statusCode'] == 200 && speedResponse['body']['success'] == true) {
|
|
print('✅ Speed limit set successfully');
|
|
} else {
|
|
print('❌ Speed limit failed: ${speedResponse['body']}');
|
|
}
|
|
|
|
// Test 3: Set data cap
|
|
print('\n📋 TEST 3: Set data cap (1073741824 bytes = 1024 MB)');
|
|
final dataCapResponse = await makeRequest('POST', '/api/peers/data-cap', {
|
|
'publicKey': publicKey,
|
|
'quotaBytes': 1073741824, // 1024 * 1024 * 1024 bytes
|
|
});
|
|
|
|
if (dataCapResponse['statusCode'] == 200 && dataCapResponse['body']['success'] == true) {
|
|
print('✅ Data cap set successfully');
|
|
} else {
|
|
print('❌ Data cap failed: ${dataCapResponse['body']}');
|
|
}
|
|
|
|
// Test 4: Get peer config
|
|
print('\n📋 TEST 4: Get peer config');
|
|
final configResponse = await makeRequest('POST', '/api/peers/config', {
|
|
'publicKey': publicKey,
|
|
});
|
|
|
|
if (configResponse['statusCode'] == 404) {
|
|
print('✅ Config endpoint working (returns not implemented as expected)');
|
|
} else {
|
|
print('❓ Unexpected config response: ${configResponse['body']}');
|
|
}
|
|
|
|
// Test 5: Delete peer
|
|
print('\n📋 TEST 5: Delete peer');
|
|
final deleteResponse = await makeRequest('POST', '/api/peers/delete', {
|
|
'publicKey': publicKey,
|
|
});
|
|
|
|
if (deleteResponse['statusCode'] == 200 && deleteResponse['body']['success'] == true) {
|
|
print('✅ Peer deleted successfully');
|
|
} else {
|
|
print('❌ Delete failed: ${deleteResponse['body']}');
|
|
}
|
|
|
|
print('\n🎉 All tests completed!');
|
|
|
|
} catch (e) {
|
|
print('❌ Test failed with error: $e');
|
|
} finally {
|
|
httpClient.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
void main(List<String> args) async {
|
|
final serverUrl = args.isNotEmpty ? args[0] : 'http://51.38.64.5:3002';
|
|
|
|
print('🧪 Waylume Server API Tester');
|
|
print('Server: $serverUrl');
|
|
|
|
final tester = ApiTester(serverUrl);
|
|
await tester.testAllEndpoints();
|
|
} |