platform/packages/html/test/all_test.dart

84 lines
2.2 KiB
Dart
Raw Normal View History

2017-07-23 15:50:37 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_html/angel_html.dart';
import 'package:angel_test/angel_test.dart';
import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart';
import 'package:test/test.dart';
main() {
Angel app;
TestClient client;
setUp(() async {
app = new Angel();
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,
renderer: new StringRenderer(
doctype: null,
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 {
var response = await client.get('/html');
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 {
var response = await client.get('/strict', headers: {'accept': '*/*'});
print('Response: ${response.body}');
expect(response,
allOf(hasContentType('text/html'), hasBody('<div>strict</div>')));
response = await client.get('/strict',
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 {
var response = await client.get('/strict');
print('Response: ${response.body}');
2019-01-06 18:11:11 +00:00
expect(response, hasStatus(406));
2017-07-23 15:50:37 +00:00
response = await client
.get('/strict', headers: {'accept': 'application/json,text/xml'});
print('Response: ${response.body}');
expect(response,
isAngelHttpException(statusCode: 406, message: '406 Not Acceptable'));
});
});
}