2017-04-22 18:46:00 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
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;
|
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
testApp = new Angel();
|
2017-09-24 18:49:18 +00:00
|
|
|
testApp.get('/foo', (req, res) async {
|
|
|
|
res.write('pub serve');
|
|
|
|
});
|
2017-04-22 18:46:00 +00:00
|
|
|
testApp.get('/empty', (req, res) => res.end());
|
2017-09-24 18:49:18 +00:00
|
|
|
|
|
|
|
testApp.responseFinalizers.add((req, ResponseContext res) async {
|
|
|
|
print('OUTGOING: ' + new String.fromCharCodes(res.buffer.toBytes()));
|
|
|
|
});
|
|
|
|
|
|
|
|
testApp.injectEncoders({'gzip': GZIP.encoder});
|
|
|
|
|
2017-04-22 18:46:00 +00:00
|
|
|
var server = await testApp.startServer();
|
|
|
|
|
|
|
|
app = new Angel();
|
|
|
|
app.get('/bar', (req, res) => res.write('normal'));
|
2017-09-24 18:49:18 +00:00
|
|
|
|
|
|
|
var httpClient = new http.Client();
|
|
|
|
|
|
|
|
var layer = new Proxy(
|
|
|
|
app,
|
|
|
|
httpClient,
|
|
|
|
server.address.address,
|
|
|
|
port: server.port,
|
|
|
|
publicPath: '/proxy',
|
|
|
|
);
|
|
|
|
app.use(layer.handleRequest);
|
2017-04-22 18:46:00 +00:00
|
|
|
|
|
|
|
app.responseFinalizers.add((req, ResponseContext res) async {
|
|
|
|
print('Normal. Buf: ' +
|
|
|
|
new String.fromCharCodes(res.buffer.toBytes()) +
|
|
|
|
', headers: ${res.headers}');
|
|
|
|
});
|
2017-09-24 18:49:18 +00:00
|
|
|
|
|
|
|
app.injectEncoders({'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();
|
|
|
|
await app.handleRequest(rq);
|
2017-09-24 18:51:47 +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 {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|