platform/test/accepts_test.dart

67 lines
2.1 KiB
Dart
Raw Normal View History

2017-07-10 23:08:05 +00:00
import 'dart:async';
import 'package:angel_framework/angel_framework.dart';
import 'package:mock_request/mock_request.dart';
import 'package:test/test.dart';
final Uri ENDPOINT = Uri.parse('http://example.com/accept');
main() {
test('no content type', () async {
var req = await acceptContentTypes();
expect(req.acceptsAll, isFalse);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.JSON), isFalse);
2017-07-10 23:08:05 +00:00
expect(req.accepts('application/json'), isFalse);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.HTML), isFalse);
2017-07-10 23:08:05 +00:00
expect(req.accepts('text/html'), isFalse);
});
test('wildcard', () async {
var req = await acceptContentTypes(['*/*']);
expect(req.acceptsAll, isTrue);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.JSON), isTrue);
2017-07-10 23:08:05 +00:00
expect(req.accepts('application/json'), isTrue);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.HTML), isTrue);
2017-07-10 23:08:05 +00:00
expect(req.accepts('text/html'), isTrue);
});
2017-09-22 14:03:23 +00:00
test('specific type', () async {
2017-07-10 23:08:05 +00:00
var req = await acceptContentTypes(['text/html']);
expect(req.acceptsAll, isFalse);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.JSON), isFalse);
2017-07-10 23:08:05 +00:00
expect(req.accepts('application/json'), isFalse);
2018-06-23 03:29:38 +00:00
//expect(req.accepts(ContentType.HTML), isTrue);
2017-07-10 23:08:05 +00:00
expect(req.accepts('text/html'), isTrue);
});
2017-11-28 18:14:50 +00:00
test('strict', () async {
var req = await acceptContentTypes(['text/html', "*/*"]);
2018-06-23 03:29:38 +00:00
expect(req.accepts('text/html'), isTrue);
//expect(req.accepts(ContentType.HTML), isTrue);
//expect(req.accepts(ContentType.JSON, strict: true), isFalse);
expect(req.accepts('application/json', strict: true), isFalse);
2017-11-28 18:14:50 +00:00
});
2017-07-10 23:08:05 +00:00
group('disallow null', () {
RequestContext req;
setUp(() async {
req = await acceptContentTypes();
});
test('throws error', () {
expect(() => req.accepts(null), throwsArgumentError);
});
});
}
Future<RequestContext> acceptContentTypes(
[Iterable<String> contentTypes = const []]) {
var headerString = contentTypes.isEmpty ? null : contentTypes.join(',');
var rq = new MockHttpRequest('GET', ENDPOINT);
2018-06-23 03:29:38 +00:00
rq.headers.set('accept', headerString);
2017-07-10 23:08:05 +00:00
rq.close();
var app = new Angel();
2018-02-07 04:34:08 +00:00
var http = new AngelHttp(app);
return http.createRequestContext(rq);
2017-07-10 23:08:05 +00:00
}