2017-10-10 16:55:42 +00:00
|
|
|
import 'dart:async';
|
2018-08-20 20:43:38 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/mirrors.dart';
|
2017-10-10 16:55:42 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-10-22 15:54:41 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2017-10-10 16:55:42 +00:00
|
|
|
import 'package:mock_request/mock_request.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
Future<String> readResponse(MockHttpResponse rs) {
|
2018-05-16 03:14:29 +00:00
|
|
|
return rs.transform(utf8.decoder).join();
|
2017-10-10 16:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future printResponse(MockHttpResponse rs) {
|
|
|
|
return readResponse(rs).then((text) {
|
|
|
|
print(text.isEmpty ? '<empty response>' : text);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:02:00 +00:00
|
|
|
void main() {
|
2018-08-21 18:50:43 +00:00
|
|
|
group('parameter_meta', parameterMetaTests);
|
2018-07-09 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parameterMetaTests() {
|
2017-10-10 16:55:42 +00:00
|
|
|
Angel app;
|
2018-02-07 04:34:08 +00:00
|
|
|
AngelHttp http;
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2018-08-20 03:46:38 +00:00
|
|
|
app = new Angel(reflector: MirrorsReflector());
|
2018-02-07 04:34:08 +00:00
|
|
|
http = new AngelHttp(app);
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/cookie', ioc((@CookieValue('token') String jwt) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return jwt;
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/header', ioc((@Header('x-foo') String header) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return header;
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/query', ioc((@Query('q') String query) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return query;
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/session', ioc((@Session('foo') String foo) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return foo;
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/match', ioc((@Query('mode', match: 'pos') String mode) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return 'YES $mode';
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/match', ioc((@Query('mode', match: 'neg') String mode) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return 'NO $mode';
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/match', ioc((@Query('mode') String mode) {
|
2017-10-10 16:55:42 +00:00
|
|
|
return 'DEFAULT $mode';
|
2018-08-20 20:43:38 +00:00
|
|
|
}));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
/*app.logger = new Logger('parameter_meta_test')
|
2017-10-10 16:55:42 +00:00
|
|
|
..onRecord.listen((rec) {
|
|
|
|
print(rec);
|
|
|
|
if (rec.error != null) print(rec.error);
|
|
|
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
|
|
|
});
|
2018-06-08 07:06:26 +00:00
|
|
|
*/
|
2017-10-10 16:55:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('injects header or throws', () async {
|
|
|
|
// Invalid request
|
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/header'))..close();
|
|
|
|
var rs = rq.response;
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
await printResponse(rs);
|
|
|
|
expect(rs.statusCode, 400);
|
|
|
|
|
|
|
|
// Valid request
|
|
|
|
rq = new MockHttpRequest('GET', Uri.parse('/header'))
|
|
|
|
..headers.add('x-foo', 'bar')
|
|
|
|
..close();
|
|
|
|
rs = rq.response;
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
var body = await readResponse(rs);
|
|
|
|
print('Body: $body');
|
|
|
|
expect(rs.statusCode, 200);
|
2018-05-16 03:14:29 +00:00
|
|
|
expect(body, json.encode('bar'));
|
2017-10-10 16:55:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('injects session or throws', () async {
|
|
|
|
// Invalid request
|
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/session'))..close();
|
|
|
|
var rs = rq.response;
|
2018-06-23 03:59:41 +00:00
|
|
|
http
|
|
|
|
.handleRequest(rq)
|
|
|
|
.timeout(const Duration(seconds: 5))
|
|
|
|
.catchError((_) => null);
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
await printResponse(rs);
|
|
|
|
expect(rs.statusCode, 500);
|
|
|
|
|
|
|
|
rq = new MockHttpRequest('GET', Uri.parse('/session'));
|
|
|
|
rq.session['foo'] = 'bar';
|
|
|
|
rq.close();
|
|
|
|
rs = rq.response;
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
await printResponse(rs);
|
|
|
|
expect(rs.statusCode, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Originally, the plan was to test cookie, session, header, etc.,
|
|
|
|
// but that behavior has been consolidated into `getValue`. Thus,
|
|
|
|
// they will all function the same way.
|
|
|
|
|
|
|
|
test('pattern matching', () async {
|
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/match?mode=pos'))..close();
|
|
|
|
var rs = rq.response;
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
var body = await readResponse(rs);
|
|
|
|
print('Body: $body');
|
|
|
|
expect(rs.statusCode, 200);
|
2018-05-16 03:14:29 +00:00
|
|
|
expect(body, json.encode('YES pos'));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
rq = new MockHttpRequest('GET', Uri.parse('/match?mode=neg'))..close();
|
|
|
|
rs = rq.response;
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
body = await readResponse(rs);
|
|
|
|
print('Body: $body');
|
|
|
|
expect(rs.statusCode, 200);
|
2018-05-16 03:14:29 +00:00
|
|
|
expect(body, json.encode('NO neg'));
|
2017-10-10 16:55:42 +00:00
|
|
|
|
|
|
|
// Fallback
|
|
|
|
rq = new MockHttpRequest('GET', Uri.parse('/match?mode=ambi'))..close();
|
|
|
|
rs = rq.response;
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
2017-10-10 16:55:42 +00:00
|
|
|
body = await readResponse(rs);
|
|
|
|
print('Body: $body');
|
|
|
|
expect(rs.statusCode, 200);
|
2018-05-16 03:14:29 +00:00
|
|
|
expect(body, json.encode('DEFAULT ambi'));
|
2017-10-10 16:55:42 +00:00
|
|
|
});
|
|
|
|
}
|