platform/test/embed_shelf_test.dart

107 lines
3.4 KiB
Dart
Raw Normal View History

2017-06-12 23:53:08 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:angel_client/io.dart' as c;
import 'package:angel_framework/angel_framework.dart';
2018-11-11 17:35:37 +00:00
import 'package:angel_framework/http.dart';
2017-06-12 23:53:08 +00:00
import 'package:angel_shelf/angel_shelf.dart';
import 'package:angel_test/angel_test.dart';
import 'package:charcode/charcode.dart';
2018-01-08 21:04:52 +00:00
import 'package:logging/logging.dart';
2017-06-12 23:53:08 +00:00
import 'package:shelf/shelf.dart' as shelf;
2018-11-11 17:35:37 +00:00
import 'package:stream_channel/stream_channel.dart';
2017-06-12 23:53:08 +00:00
import 'package:test/test.dart';
main() {
c.Angel client;
HttpServer server;
String url;
setUp(() async {
var handler = new shelf.Pipeline().addHandler((shelf.Request request) {
if (request.url.path == 'two')
2018-11-11 17:35:37 +00:00
return new shelf.Response(200, body: json.encode(2));
2017-06-12 23:53:08 +00:00
else if (request.url.path == 'error')
throw new AngelHttpException.notFound();
else if (request.url.path == 'status')
return new shelf.Response.notModified(headers: {'foo': 'bar'});
else if (request.url.path == 'hijack') {
2018-11-11 17:35:37 +00:00
request.hijack((StreamChannel<List<int>> channel) {
var sink = channel.sink;
sink.add(utf8.encode('HTTP/1.1 200 OK\r\n'));
2017-06-12 23:53:08 +00:00
sink.add([$lf]);
2018-11-11 17:35:37 +00:00
sink.add(utf8.encode(json.encode({'error': 'crime'})));
2017-06-12 23:53:08 +00:00
sink.close();
});
} else if (request.url.path == 'throw')
return null;
else
return new shelf.Response.ok('Request for "${request.url}"');
});
2018-11-11 17:35:37 +00:00
var app = new Angel();
var http = new AngelHttp(app);
app.get('/angel', (req, res) => 'Angel');
app.fallback(embedShelf(handler, throwOnNullResponse: true));
app.logger = new Logger.detached('angel_shelf')
..onRecord.listen((rec) {
stdout.writeln(rec);
if (rec.error != null) stdout.writeln(rec.error);
if (rec.stackTrace != null) stdout.writeln(rec.stackTrace);
});
2017-06-12 23:53:08 +00:00
2018-11-11 17:35:37 +00:00
server = await http.startServer(InternetAddress.loopbackIPv4, 0);
client = new c.Rest(url = http.uri.toString());
2017-06-12 23:53:08 +00:00
});
tearDown(() async {
await client.close();
await server.close(force: true);
});
test('expose angel side', () async {
var response = await client.get('/angel');
2018-11-11 17:35:37 +00:00
expect(json.decode(response.body), equals('Angel'));
2017-06-12 23:53:08 +00:00
});
test('expose shelf side', () async {
var response = await client.get('/foo');
expect(response, hasStatus(200));
expect(response.body, equals('Request for "foo"'));
});
test('shelf can return arbitrary values', () async {
var response = await client.get('/two');
expect(response, isJson(2));
});
test('shelf can hijack', () async {
try {
var client = new HttpClient();
var rq = await client.openUrl('GET', Uri.parse('$url/hijack'));
var rs = await rq.close();
2018-11-11 17:35:37 +00:00
var body = await rs.transform(utf8.decoder).join();
2017-06-12 23:53:08 +00:00
print('Response: $body');
2018-11-11 17:35:37 +00:00
expect(json.decode(body), {'error': 'crime'});
2017-06-12 23:53:08 +00:00
} on HttpException catch (e, st) {
print('HTTP Exception: ' + e.message);
print(st);
rethrow;
}
2018-11-11 17:35:37 +00:00
}, skip: '');
2017-06-12 23:53:08 +00:00
test('shelf can set status code', () async {
var response = await client.get('/status');
expect(response, allOf(hasStatus(304), hasHeader('foo', 'bar')));
});
test('shelf can throw error', () async {
var response = await client.get('/error');
expect(response, hasStatus(404));
});
test('throw on null', () async {
var response = await client.get('/throw');
expect(response, hasStatus(500));
});
}