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';
|
2017-09-24 18:49:18 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
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 {
|
|
|
|
testApp = new Angel();
|
2017-09-24 18:49:18 +00:00
|
|
|
testApp.get('/foo', (req, res) async {
|
|
|
|
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 {
|
2017-09-24 18:49:18 +00:00
|
|
|
print('OUTGOING: ' + new String.fromCharCodes(res.buffer.toBytes()));
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
|
|
|
app = new Angel();
|
|
|
|
app.get('/bar', (req, res) => res.write('normal'));
|
2017-09-24 18:49:18 +00:00
|
|
|
|
|
|
|
var httpClient = new http.Client();
|
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
layer = new Proxy(
|
2017-09-24 18:49:18 +00:00
|
|
|
httpClient,
|
|
|
|
server.address.address,
|
|
|
|
port: server.port,
|
|
|
|
publicPath: '/proxy',
|
|
|
|
);
|
2018-11-02 04:22:32 +00:00
|
|
|
app.all("*", layer.handleRequest);
|
2017-04-22 18:46:00 +00:00
|
|
|
|
|
|
|
app.responseFinalizers.add((req, ResponseContext res) async {
|
2018-11-02 04:22:32 +00:00
|
|
|
print('Normal. Buf: ' + new String.fromCharCodes(res.buffer.toBytes()) + ', 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);
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await client.close();
|
|
|
|
await app.close();
|
|
|
|
await testApp.close();
|
|
|
|
app = null;
|
|
|
|
testApp = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('proxied', () async {
|
2017-09-24 18:49:18 +00:00
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/proxy/foo'))..close();
|
2018-11-02 04:22:32 +00:00
|
|
|
var rqc = await HttpRequestContext.from(rq, app, '/proxy/foo');
|
|
|
|
var rsc = HttpResponseContext(rq.response, app);
|
|
|
|
await app.executeHandler(layer, 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
|
|
|
});
|
|
|
|
}
|