2021-09-11 04:03:14 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_html/angel3_html.dart';
|
|
|
|
import 'package:angel3_test/angel3_test.dart';
|
|
|
|
import 'package:belatuk_html_builder/elements.dart';
|
2017-07-23 15:50:37 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
void main() {
|
2024-10-12 10:35:14 +00:00
|
|
|
Protevus app;
|
2021-06-20 12:37:20 +00:00
|
|
|
late TestClient client;
|
2017-07-23 15:50:37 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
app = Protevus();
|
2017-07-23 15:50:37 +00:00
|
|
|
|
2019-01-06 18:11:11 +00:00
|
|
|
app.fallback(renderHtml());
|
2017-07-23 15:50:37 +00:00
|
|
|
|
2019-01-06 18:11:11 +00:00
|
|
|
app.get('/html', (req, res) {
|
2017-07-23 15:50:37 +00:00
|
|
|
return html(c: [
|
|
|
|
head(c: [
|
|
|
|
title(c: [text('ok')])
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-01-06 18:11:11 +00:00
|
|
|
app.get(
|
|
|
|
'/strict',
|
|
|
|
chain([
|
|
|
|
renderHtml(
|
|
|
|
enforceAcceptHeader: true,
|
2021-06-20 12:37:20 +00:00
|
|
|
renderer: StringRenderer(
|
|
|
|
//doctype: null,
|
2019-01-06 18:11:11 +00:00
|
|
|
pretty: false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(req, res) {
|
|
|
|
return div(c: [text('strict')]);
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
);
|
2017-07-23 15:50:37 +00:00
|
|
|
client = await connectTo(app);
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => client.close());
|
|
|
|
|
|
|
|
test('sets content type and body', () async {
|
2021-06-20 12:37:20 +00:00
|
|
|
var response = await client.get(Uri.parse('/html'));
|
2017-07-23 15:50:37 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
response,
|
2019-01-06 18:11:11 +00:00
|
|
|
allOf(
|
|
|
|
hasContentType('text/html'),
|
|
|
|
hasBody(
|
|
|
|
'<!DOCTYPE html><html><head><title>ok</title></head></html>')));
|
2017-07-23 15:50:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('enforce accept header', () {
|
|
|
|
test('sends if correct accept or wildcard', () async {
|
2021-06-20 12:37:20 +00:00
|
|
|
var response =
|
|
|
|
await client.get(Uri.parse('/strict'), headers: {'accept': '*/*'});
|
2017-07-23 15:50:37 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(response,
|
|
|
|
allOf(hasContentType('text/html'), hasBody('<div>strict</div>')));
|
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
response = await client.get(Uri.parse('/strict'),
|
2017-07-23 15:50:37 +00:00
|
|
|
headers: {'accept': 'text/html,application/json,text/xml'});
|
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(response,
|
|
|
|
allOf(hasContentType('text/html'), hasBody('<div>strict</div>')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws if incorrect or no accept', () async {
|
2021-06-20 12:37:20 +00:00
|
|
|
var response = await client.get(Uri.parse('/strict'));
|
|
|
|
print('Response: ${response.statusCode} ${response.body}');
|
2019-01-06 18:11:11 +00:00
|
|
|
expect(response, hasStatus(406));
|
2017-07-23 15:50:37 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
response = await client.get(Uri.parse('/strict'),
|
|
|
|
headers: {'accept': 'application/json,text/xml'});
|
2017-07-23 15:50:37 +00:00
|
|
|
print('Response: ${response.body}');
|
2024-10-12 10:35:14 +00:00
|
|
|
expect(
|
|
|
|
response,
|
|
|
|
isProtevusHttpException(
|
|
|
|
statusCode: 406, message: '406 Not Acceptable'));
|
2017-07-23 15:50:37 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|