60 lines
2.0 KiB
Dart
60 lines
2.0 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:integration_test/integration_test.dart';
|
|
|
|
void main() {
|
|
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
const enabled = bool.fromEnvironment('RUN_LIVE_SSE_TEST');
|
|
const baseUrl = String.fromEnvironment('LIVE_API_BASE_URL');
|
|
const username = String.fromEnvironment('LIVE_TEST_USERNAME');
|
|
const password = String.fromEnvironment('LIVE_TEST_PASSWORD');
|
|
|
|
test('live SSE streams to completion when explicitly enabled', () async {
|
|
if (!enabled) return;
|
|
expect(baseUrl, isNotEmpty, reason: 'LIVE_API_BASE_URL is required');
|
|
expect(username, isNotEmpty, reason: 'LIVE_TEST_USERNAME is required');
|
|
expect(password, isNotEmpty, reason: 'LIVE_TEST_PASSWORD is required');
|
|
|
|
final dio = Dio(
|
|
BaseOptions(
|
|
baseUrl: baseUrl,
|
|
connectTimeout: const Duration(seconds: 10),
|
|
),
|
|
);
|
|
final login = await dio.post<Map<String, dynamic>>(
|
|
'/api/auth/login',
|
|
data: {'username': username, 'password': password},
|
|
);
|
|
final token = login.data!['token'] as String;
|
|
|
|
final client = HttpClient()
|
|
..connectionTimeout = const Duration(seconds: 10);
|
|
addTearDown(() => client.close(force: true));
|
|
|
|
final request = await client.postUrl(
|
|
Uri.parse('$baseUrl/api/chat/messages/stream'),
|
|
);
|
|
request.headers.set('content-type', 'application/json; charset=utf-8');
|
|
request.headers.set('authorization', 'Bearer $token');
|
|
request.add(utf8.encode(jsonEncode({'content': '你好'})));
|
|
|
|
final response = await request.close();
|
|
expect(response.statusCode, HttpStatus.ok);
|
|
|
|
var completed = false;
|
|
await for (final line
|
|
in response.transform(utf8.decoder).transform(const LineSplitter())) {
|
|
if (!line.startsWith('data: ')) continue;
|
|
final event = jsonDecode(line.substring(6));
|
|
if (event is Map && event['done'] == true) {
|
|
completed = true;
|
|
}
|
|
}
|
|
expect(completed, isTrue);
|
|
});
|
|
}
|