platform/packages/proxy/test/pub_serve_test.dart

94 lines
2.6 KiB
Dart
Raw Normal View History

2017-04-22 18:46:00 +00:00
import 'dart:convert';
import 'dart:io';
2021-06-10 08:47:05 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_proxy/angel3_proxy.dart';
import 'package:angel3_test/angel3_test.dart';
2018-11-08 22:13:26 +00:00
import 'package:logging/logging.dart';
2021-06-10 08:47:05 +00:00
import 'package:angel3_mock_request/angel3_mock_request.dart';
2017-04-22 18:46:00 +00:00
import 'package:test/test.dart';
2021-06-10 08:47:05 +00:00
void main() {
Protevus? app, testApp;
2021-06-10 08:47:05 +00:00
late TestClient client;
late Proxy layer;
2017-04-22 18:46:00 +00:00
setUp(() async {
testApp = Protevus();
2021-06-10 08:47:05 +00:00
testApp!.get('/foo', (req, res) async {
2018-11-08 22:13:26 +00:00
res.useBuffer();
2017-09-24 18:49:18 +00:00
res.write('pub serve');
});
2021-06-10 08:47:05 +00:00
testApp!.get('/empty', (req, res) => res.close());
2017-09-24 18:49:18 +00:00
2021-06-10 08:47:05 +00:00
testApp!.responseFinalizers.add((req, res) async {
2022-08-27 09:23:41 +00:00
print('OUTGOING: ${String.fromCharCodes(res.buffer!.toBytes())}');
2017-09-24 18:49:18 +00:00
});
2021-06-10 08:47:05 +00:00
testApp!.encoders.addAll({'gzip': gzip.encoder});
2017-09-24 18:49:18 +00:00
var server = await ProtevusHttp(testApp!).startServer();
2017-04-22 18:46:00 +00:00
app = Protevus();
2021-06-10 08:47:05 +00:00
app!.fallback((req, res) {
2018-11-08 22:13:26 +00:00
res.useBuffer();
return true;
});
2021-06-10 08:47:05 +00:00
app!.get('/bar', (req, res) => res.write('normal'));
2017-09-24 18:49:18 +00:00
2019-05-02 23:19:30 +00:00
layer = Proxy(
Uri(scheme: 'http', host: server.address.address, port: server.port),
2017-09-24 18:49:18 +00:00
publicPath: '/proxy',
);
2017-04-22 18:46:00 +00:00
2021-06-10 08:47:05 +00:00
app!.fallback(layer.handleRequest);
2018-11-08 22:13:26 +00:00
2021-06-10 08:47:05 +00:00
app!.responseFinalizers.add((req, res) async {
2022-08-27 09:23:41 +00:00
print(
'Normal. Buf: ${String.fromCharCodes(res.buffer!.toBytes())}, headers: ${res.headers}');
2017-04-22 18:46:00 +00:00
});
2017-09-24 18:49:18 +00:00
2021-06-10 08:47:05 +00:00
app!.encoders.addAll({'gzip': gzip.encoder});
2017-04-22 18:46:00 +00:00
2021-06-10 08:47:05 +00:00
client = await connectTo(app!);
2018-11-08 22:13:26 +00:00
2021-06-10 08:47:05 +00:00
app!.logger = testApp!.logger = Logger('proxy')
2018-11-08 22:13:26 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2017-04-22 18:46:00 +00:00
});
tearDown(() async {
await client.close();
2021-06-10 08:47:05 +00:00
await app!.close();
await testApp!.close();
2017-04-22 18:46:00 +00:00
app = null;
testApp = null;
});
test('proxied', () async {
2019-05-02 23:19:30 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/proxy/foo'));
await rq.close();
2021-06-10 08:47:05 +00:00
var rqc = await HttpRequestContext.from(rq, app!, '/proxy/foo');
2018-11-02 04:22:32 +00:00
var rsc = HttpResponseContext(rq.response, app);
2021-06-10 08:47:05 +00:00
await app!.executeHandler(layer.handleRequest, rqc, rsc);
2018-11-08 22:13:26 +00:00
var response = await rq.response
//.transform(gzip.decoder)
.transform(utf8.decoder)
.join();
2017-09-24 18:49:18 +00:00
expect(response, 'pub serve');
2017-04-22 18:46:00 +00:00
});
test('empty', () async {
2021-06-10 08:47:05 +00:00
var response = await client.get(Uri.parse('/proxy/empty'));
2017-04-26 12:20:14 +00:00
expect(response.body, isEmpty);
2017-04-22 18:46:00 +00:00
});
test('normal', () async {
2021-06-10 08:47:05 +00:00
var response = await client.get(Uri.parse('/bar'));
2017-04-26 12:20:14 +00:00
expect(response, hasBody('normal'));
2017-04-22 18:46:00 +00:00
});
}