2017-07-10 23:08:05 +00:00
|
|
|
import 'dart:async';
|
2021-03-20 08:11:18 +00:00
|
|
|
import 'dart:io';
|
2021-05-14 10:34:09 +00:00
|
|
|
import 'package:angel3_container/mirrors.dart';
|
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
|
|
|
import 'package:angel3_mock_request/angel3_mock_request.dart';
|
2017-07-10 23:08:05 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
final Uri ENDPOINT = Uri.parse('http://example.com/accept');
|
|
|
|
|
2021-05-14 10:34:09 +00:00
|
|
|
void main() {
|
2017-07-10 23:08:05 +00:00
|
|
|
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 {
|
2021-07-08 02:42:40 +00:00
|
|
|
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', () {
|
2021-03-20 08:11:18 +00:00
|
|
|
late RequestContext req;
|
2017-07-10 23:08:05 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
req = await acceptContentTypes();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws error', () {
|
|
|
|
expect(() => req.accepts(null), throwsArgumentError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<RequestContext> acceptContentTypes(
|
|
|
|
[Iterable<String> contentTypes = const []]) {
|
2021-03-20 08:11:18 +00:00
|
|
|
var headerString =
|
2021-04-10 11:23:57 +00:00
|
|
|
contentTypes.isEmpty ? ContentType.text : contentTypes.join(',');
|
2021-03-20 08:11:18 +00:00
|
|
|
var rq = MockHttpRequest('GET', ENDPOINT, persistentConnection: false);
|
2018-06-23 03:29:38 +00:00
|
|
|
rq.headers.set('accept', headerString);
|
2017-07-10 23:08:05 +00:00
|
|
|
rq.close();
|
2019-05-02 22:48:31 +00:00
|
|
|
var app = Angel(reflector: MirrorsReflector());
|
|
|
|
var http = AngelHttp(app);
|
2018-11-11 01:07:09 +00:00
|
|
|
return http.createRequestContext(rq, rq.response);
|
2017-07-10 23:08:05 +00:00
|
|
|
}
|