platform/packages/validate/test/server_test.dart

70 lines
2 KiB
Dart
Raw Normal View History

2021-02-21 02:47:23 +00:00
import 'dart:async';
import 'dart:convert';
2021-05-14 11:47:44 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
2021-03-07 16:47:49 +00:00
//import 'package:angel_test/angel_test.dart';
2021-05-14 11:47:44 +00:00
import 'package:angel3_validate/server.dart';
2021-02-21 02:47:23 +00:00
import 'package:logging/logging.dart';
2021-05-14 11:47:44 +00:00
import 'package:angel3_mock_request/angel3_mock_request.dart';
2021-02-21 02:47:23 +00:00
import 'package:test/test.dart';
final Validator echoSchema = Validator({'message*': isString});
void printRecord(LogRecord rec) {
2021-07-09 05:39:39 +00:00
print('${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
2021-02-21 02:47:23 +00:00
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
}
void main() {
2023-10-29 06:03:23 +00:00
late Angel app;
2021-04-10 12:42:55 +00:00
late AngelHttp http;
2021-03-07 16:47:49 +00:00
//TestClient client;
2021-02-21 02:47:23 +00:00
setUp(() async {
app = Angel();
2023-10-29 06:03:23 +00:00
http = AngelHttp(app, useZone: false);
2021-02-21 02:47:23 +00:00
2023-10-29 06:03:23 +00:00
app.chain([validate(echoSchema)]).post('/echo',
2021-02-21 02:47:23 +00:00
(RequestContext req, res) async {
await req.parseBody();
res.write('Hello, ${req.bodyAsMap['message']}!');
});
2023-10-29 06:03:23 +00:00
app.logger = Logger('angel3')..onRecord.listen(printRecord);
2021-03-07 16:47:49 +00:00
//client = await connectTo(app);
2021-02-21 02:47:23 +00:00
});
tearDown(() async {
2021-03-07 16:47:49 +00:00
//await client.close();
2021-02-21 02:47:23 +00:00
await http.close();
});
group('echo', () {
2021-03-07 16:47:49 +00:00
//test('validate', () async {
// var response = await client.post('/echo',
// body: {'message': 'world'}, headers: {'accept': '*/*'});
// print('Response: ${response.body}');
// expect(response, hasStatus(200));
// expect(response.body, equals('Hello, world!'));
//});
2021-02-21 02:47:23 +00:00
test('enforce', () async {
var rq = MockHttpRequest('POST', Uri(path: '/echo'))
..headers.add('accept', '*/*')
..headers.add('content-type', 'application/json')
..write(json.encode({'foo': 'bar'}));
scheduleMicrotask(() async {
await rq.close();
await http.handleRequest(rq);
});
var responseBody = await rq.response.transform(utf8.decoder).join();
2021-03-07 16:47:49 +00:00
print('Response: $responseBody');
2021-02-21 02:47:23 +00:00
expect(rq.response.statusCode, 400);
});
});
}