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';
|
|
|
|
|
|
|
|
final Validator echoSchema = new Validator({'message*': isString});
|
|
|
|
|
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 {
|
|
|
|
app = new Angel();
|
2018-06-28 02:34:05 +00:00
|
|
|
http = new 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
|
|
|
});
|
|
|
|
|
2018-06-28 02:34:05 +00:00
|
|
|
app.logger = new 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 {
|
2018-06-28 02:34:05 +00:00
|
|
|
var rq = new MockHttpRequest('POST', new Uri(path: '/echo'))
|
2018-11-02 00:55:28 +00:00
|
|
|
..headers.add('accept', '*/*')
|
|
|
|
..headers.add('content-type', 'application/json')
|
|
|
|
..write(json.encode({'foo': 'bar'}))
|
|
|
|
..close();
|
2018-06-28 02:34:05 +00:00
|
|
|
http.handleRequest(rq);
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|