platform/test/server_test.dart

71 lines
1.9 KiB
Dart
Raw Normal View History

2019-09-27 13:28:20 +00:00
import 'dart:async';
2018-11-06 19:19:55 +00:00
import 'dart:convert';
2016-12-26 01:09:24 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-11-02 00:55:28 +00:00
import 'package:angel_framework/http.dart';
2016-12-26 01:09:24 +00:00
import 'package:angel_test/angel_test.dart';
import 'package:angel_validate/server.dart';
2018-06-28 02:34:05 +00:00
import 'package:logging/logging.dart';
import 'package:mock_request/mock_request.dart';
2016-12-26 01:09:24 +00:00
import 'package:test/test.dart';
2019-09-27 13:28:20 +00:00
final Validator echoSchema = Validator({'message*': isString});
2016-12-26 01:09:24 +00:00
2018-06-28 02:34:05 +00:00
void printRecord(LogRecord rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
}
2016-12-26 01:09:24 +00:00
main() {
Angel app;
2018-06-28 02:34:05 +00:00
AngelHttp http;
2016-12-26 01:09:24 +00:00
TestClient client;
setUp(() async {
2019-09-27 13:28:20 +00:00
app = Angel();
http = AngelHttp(app, useZone: false);
2016-12-26 01:09:24 +00:00
2018-11-02 00:55:28 +00:00
app.chain([validate(echoSchema)]).post('/echo',
2016-12-26 01:09:24 +00:00
(RequestContext req, res) async {
2018-12-10 17:51:10 +00:00
await req.parseBody();
res.write('Hello, ${req.bodyAsMap['message']}!');
2016-12-26 01:09:24 +00:00
});
2019-09-27 13:28:20 +00:00
app.logger = Logger('angel')..onRecord.listen(printRecord);
2017-03-29 02:56:51 +00:00
client = await connectTo(app);
2016-12-26 01:09:24 +00:00
});
tearDown(() async {
await client.close();
2018-06-28 02:34:05 +00:00
await http.close();
2016-12-26 01:09:24 +00:00
app = null;
client = null;
});
group('echo', () {
test('validate', () async {
var response = await client.post('/echo',
2018-06-28 02:34:05 +00:00
body: {'message': 'world'}, headers: {'accept': '*/*'});
2016-12-26 01:09:24 +00:00
print('Response: ${response.body}');
2018-06-28 02:34:05 +00:00
expect(response, hasStatus(200));
2016-12-26 01:09:24 +00:00
expect(response.body, equals('Hello, world!'));
});
test('enforce', () async {
2019-09-27 13:28:20 +00:00
var rq = MockHttpRequest('POST', Uri(path: '/echo'))
2018-11-02 00:55:28 +00:00
..headers.add('accept', '*/*')
..headers.add('content-type', 'application/json')
2019-09-27 13:28:20 +00:00
..write(json.encode({'foo': 'bar'}));
scheduleMicrotask(() async {
await rq.close();
await http.handleRequest(rq);
});
2018-06-28 02:34:05 +00:00
var responseBody = await rq.response.transform(utf8.decoder).join();
print('Response: ${responseBody}');
expect(rq.response.statusCode, 400);
2016-12-26 01:09:24 +00:00
});
});
}