2017-04-22 18:46:00 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-11-02 04:22:32 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2017-04-22 18:46:00 +00:00
|
|
|
import 'package:angel_proxy/angel_proxy.dart';
|
|
|
|
import 'package:angel_test/angel_test.dart';
|
2018-11-08 22:13:26 +00:00
|
|
|
import 'package:http/io_client.dart' as http;
|
|
|
|
import 'package:logging/logging.dart';
|
2017-09-24 18:49:18 +00:00
|
|
|
import 'package:mock_request/mock_request.dart';
|
2017-04-22 18:46:00 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
Angel app, testApp;
|
|
|
|
TestClient client;
|
2018-11-02 04:22:32 +00:00
|
|
|
Proxy layer;
|
2017-04-22 18:46:00 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2019-05-02 23:19:30 +00:00
|
|
|
testApp = Angel();
|
2017-09-24 18:49:18 +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');
|
|
|
|
});
|
2018-11-02 04:22:32 +00:00
|
|
|
testApp.get('/empty', (req, res) => res.close());
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
testApp.responseFinalizers.add((req, res) async {
|
2019-05-02 23:19:30 +00:00
|
|
|
print('OUTGOING: ' + String.fromCharCodes(res.buffer.toBytes()));
|
2017-09-24 18:49:18 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
testApp.encoders.addAll({'gzip': gzip.encoder});
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
var server = await AngelHttp(testApp).startServer();
|
2017-04-22 18:46:00 +00:00
|
|
|
|
2019-05-02 23:19:30 +00:00
|
|
|
app = Angel();
|
2018-11-08 22:13:26 +00:00
|
|
|
app.fallback((req, res) {
|
|
|
|
res.useBuffer();
|
|
|
|
return true;
|
|
|
|
});
|
2017-04-22 18:46:00 +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
|
|
|
var httpClient = http.IOClient();
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2019-05-02 23:19:30 +00:00
|
|
|
layer = Proxy(
|
2017-09-24 18:49:18 +00:00
|
|
|
httpClient,
|
2019-05-02 23:19:30 +00:00
|
|
|
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
|
|
|
|
2018-11-08 22:13:26 +00:00
|
|
|
app.fallback(layer.handleRequest);
|
|
|
|
|
|
|
|
app.responseFinalizers.add((req, res) async {
|
|
|
|
print('Normal. Buf: ' +
|
2019-05-02 23:19:30 +00:00
|
|
|
String.fromCharCodes(res.buffer.toBytes()) +
|
2018-11-08 22:13:26 +00:00
|
|
|
', headers: ${res.headers}');
|
2017-04-22 18:46:00 +00:00
|
|
|
});
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
app.encoders.addAll({'gzip': gzip.encoder});
|
2017-04-22 18:46:00 +00:00
|
|
|
|
|
|
|
client = await connectTo(app);
|
2018-11-08 22:13:26 +00:00
|
|
|
|
2019-05-02 23:19:30 +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();
|
|
|
|
await app.close();
|
|
|
|
await testApp.close();
|
|
|
|
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();
|
2018-11-02 04:22:32 +00:00
|
|
|
var rqc = await HttpRequestContext.from(rq, app, '/proxy/foo');
|
|
|
|
var rsc = HttpResponseContext(rq.response, app);
|
2018-11-08 22:13:26 +00:00
|
|
|
await app.executeHandler(layer.handleRequest, rqc, rsc);
|
|
|
|
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 {
|
|
|
|
var response = await client.get('/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 {
|
|
|
|
var response = await client.get('/bar');
|
2017-04-26 12:20:14 +00:00
|
|
|
expect(response, hasBody('normal'));
|
2017-04-22 18:46:00 +00:00
|
|
|
});
|
|
|
|
}
|