Files
platforms/apps/user_app/test/data/api_client_test.dart

55 lines
1.7 KiB
Dart
Raw Normal View History

2026-09-02 22:48:47 +08:00
// 功能描述:验证受保护头像二进制接口的鉴权读取和异常处理。
// 版本1.1.0
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:user_app/data/services/api_client.dart';
/// 验证头像字节、鉴权请求头和资源不存在状态。
void main() {
test('头像接口携带令牌并返回二进制内容', () async {
final expectedBytes = Uint8List.fromList(const [1, 2, 3]);
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async {
expect(request.url.path, '/heqi/client/v1/user/auth/avatar');
expect(request.headers['authorization'], 'JWT test-token');
return http.Response.bytes(expectedBytes, 200);
}),
);
expect(
await client.getBytes('/heqi/client/v1/user/auth/avatar'),
expectedBytes,
);
});
test('头像不存在时返回空值', () async {
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async => http.Response('', 404)),
);
expect(await client.getBytes('/avatar'), isNull);
});
test('头像接口异常时返回中文错误', () async {
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async => http.Response('', 500)),
);
expect(
() => client.getBytes('/avatar'),
throwsA(
isA<ApiException>().having((error) => error.message, 'message', '头像加载失败'),
),
);
});
}