2017-06-12 23:53:08 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
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';
|
2019-10-14 18:42:43 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2018-01-08 21:04:52 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2019-10-14 15:28:50 +00:00
|
|
|
import 'package:pretty_logging/pretty_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';
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void main() {
|
2021-06-20 12:37:20 +00:00
|
|
|
late http.Client client;
|
|
|
|
late HttpServer server;
|
2017-06-12 23:53:08 +00:00
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
Uri _path(String p) {
|
2019-10-14 15:28:50 +00:00
|
|
|
return Uri(
|
2021-06-20 12:37:20 +00:00
|
|
|
scheme: 'http',
|
|
|
|
host: server.address.address,
|
|
|
|
port: server.port,
|
|
|
|
path: p);
|
2019-10-14 15:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:53:08 +00:00
|
|
|
setUp(() async {
|
2019-10-14 18:42:43 +00:00
|
|
|
client = http.Client();
|
2019-10-14 15:28:50 +00:00
|
|
|
var handler = shelf.Pipeline().addHandler((shelf.Request request) {
|
|
|
|
if (request.url.path == 'two') {
|
|
|
|
return shelf.Response(200, body: json.encode(2));
|
|
|
|
} else if (request.url.path == 'error') {
|
|
|
|
throw AngelHttpException.notFound();
|
|
|
|
} else if (request.url.path == 'status') {
|
|
|
|
return 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) {
|
2019-10-14 18:42:43 +00:00
|
|
|
print('a');
|
2018-11-11 17:35:37 +00:00
|
|
|
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();
|
2019-10-14 18:42:43 +00:00
|
|
|
print('b');
|
2017-06-12 23:53:08 +00:00
|
|
|
});
|
2021-06-20 12:37:20 +00:00
|
|
|
//} else if (request.url.path == 'throw') {
|
|
|
|
// return null;
|
2019-10-14 15:28:50 +00:00
|
|
|
} else {
|
|
|
|
return shelf.Response.ok('Request for "${request.url}"');
|
|
|
|
}
|
2017-06-12 23:53:08 +00:00
|
|
|
});
|
|
|
|
|
2019-10-14 15:28:50 +00:00
|
|
|
var logger = Logger.detached('angel_shelf')..onRecord.listen(prettyLog);
|
|
|
|
var app = Angel(logger: logger);
|
2019-10-14 18:42:43 +00:00
|
|
|
var httpDriver = AngelHttp(app);
|
2018-11-11 17:35:37 +00:00
|
|
|
app.get('/angel', (req, res) => 'Angel');
|
|
|
|
app.fallback(embedShelf(handler, throwOnNullResponse: true));
|
2017-06-12 23:53:08 +00:00
|
|
|
|
2019-10-14 18:42:43 +00:00
|
|
|
server = await httpDriver.startServer(InternetAddress.loopbackIPv4, 0);
|
2017-06-12 23:53:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
2021-06-20 12:37:20 +00:00
|
|
|
client.close();
|
2017-06-12 23:53:08 +00:00
|
|
|
await server.close(force: true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('expose angel side', () async {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/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 {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/foo'));
|
2017-06-12 23:53:08 +00:00
|
|
|
expect(response, hasStatus(200));
|
|
|
|
expect(response.body, equals('Request for "foo"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('shelf can return arbitrary values', () async {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/two'));
|
2017-06-12 23:53:08 +00:00
|
|
|
expect(response, isJson(2));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('shelf can hijack', () async {
|
|
|
|
try {
|
2019-10-14 15:28:50 +00:00
|
|
|
var client = HttpClient();
|
2021-06-20 12:37:20 +00:00
|
|
|
var rq = await client.openUrl('GET', _path('/hijack'));
|
2017-06-12 23:53:08 +00:00
|
|
|
var rs = await rq.close();
|
2019-06-26 02:50:57 +00:00
|
|
|
var body = await rs.cast<List<int>>().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;
|
|
|
|
}
|
2019-10-14 18:42:43 +00:00
|
|
|
});
|
2017-06-12 23:53:08 +00:00
|
|
|
|
|
|
|
test('shelf can set status code', () async {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/status'));
|
2017-06-12 23:53:08 +00:00
|
|
|
expect(response, allOf(hasStatus(304), hasHeader('foo', 'bar')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('shelf can throw error', () async {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/error'));
|
2017-06-12 23:53:08 +00:00
|
|
|
expect(response, hasStatus(404));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throw on null', () async {
|
2019-10-14 15:28:50 +00:00
|
|
|
var response = await client.get(_path('/throw'));
|
2017-06-12 23:53:08 +00:00
|
|
|
expect(response, hasStatus(500));
|
|
|
|
});
|
|
|
|
}
|