Files
loongyan/test/unit/middleware/exception_handler_test.dart
lzw-723 120278aa85
Some checks failed
Dart CI / build (push) Failing after 11s
Docker Build / docker (push) Successful in 9m6s
格式化代码
2026-04-04 13:36:58 +08:00

134 lines
4.3 KiB
Dart

import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:test/test.dart';
import '../../../bin/middleware/exception_handler.dart';
void main() {
group('exceptionHandler middleware', () {
late Middleware middleware;
setUp(() {
middleware = exceptionHandler();
});
test('should pass through successful requests', () async {
final handler = middleware((request) {
return Response.ok('Hello');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 200);
expect(await response.readAsString(), 'Hello');
});
test('should return 400 for FormatException', () async {
final handler = middleware((request) {
throw FormatException('invalid format');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 400);
final body = await response.readAsString();
expect(body, contains('Invalid parameter format'));
expect(body, contains('invalid format'));
expect(response.headers['content-type'], 'text/plain');
});
test('should return 404 for PathNotFoundException', () async {
final handler = middleware((request) {
throw const PathNotFoundException('/some/path', OSError('not found'));
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 404);
final body = await response.readAsString();
expect(body, contains('Resource not found'));
});
test('should return 500 for FileSystemException', () async {
final handler = middleware((request) {
throw const FileSystemException('disk error');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 500);
final body = await response.readAsString();
expect(body, contains('File system error'));
expect(body, contains('disk error'));
});
test('should return 500 for generic Exception', () async {
final handler = middleware((request) {
throw Exception('something went wrong');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 500);
final body = await response.readAsString();
expect(body, contains('Internal server error'));
expect(body, contains('something went wrong'));
});
test('should return 500 for unknown errors', () async {
final handler = middleware((request) {
// Throw an Error (not Exception) to test the catch-all
throw StateError('critical failure');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 500);
final body = await response.readAsString();
expect(body, 'An unexpected error occurred');
});
test('should handle async exceptions', () async {
final handler = middleware((request) async {
await Future.delayed(Duration.zero);
throw FormatException('async format error');
});
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 400);
final body = await response.readAsString();
expect(body, contains('async format error'));
});
test('should handle nested middleware handlers', () async {
final innerHandler = (Request request) async {
return Response.ok('nested');
};
final wrappedHandler = _addProcessingHeader(innerHandler);
final handler = middleware(wrappedHandler);
final request = Request('GET', Uri.parse('http://localhost/test'));
final response = await handler(request);
expect(response.statusCode, 200);
expect(response.headers['x-processed'], 'true');
});
});
}
Handler _addProcessingHeader(Handler inner) {
return (Request request) async {
final response = await inner(request);
return response.change(headers: {'x-processed': 'true'});
};
}