2017-04-02 02:06:27 +00:00
|
|
|
import 'dart:convert';
|
2016-11-23 22:03:06 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-11-02 04:22:32 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2016-11-23 22:03:06 +00:00
|
|
|
import 'package:angel_proxy/angel_proxy.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2017-09-24 18:49:18 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2016-11-23 22:03:06 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
Angel app;
|
|
|
|
http.Client client = new http.Client();
|
|
|
|
HttpServer server, testServer;
|
|
|
|
String url;
|
|
|
|
|
|
|
|
setUp(() async {
|
2018-11-02 04:22:32 +00:00
|
|
|
app = new Angel()..keepRawRequestBuffers = true;
|
|
|
|
var appHttp = AngelHttp(app);
|
2017-09-24 18:49:18 +00:00
|
|
|
var httpClient = new http.Client();
|
2016-11-23 22:03:06 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
testServer = await startTestServer();
|
2017-01-12 00:19:15 +00:00
|
|
|
|
2017-09-24 18:49:18 +00:00
|
|
|
var proxy1 = new Proxy(
|
|
|
|
app,
|
|
|
|
httpClient,
|
|
|
|
testServer.address.address,
|
|
|
|
port: testServer.port,
|
|
|
|
publicPath: '/proxy',
|
|
|
|
);
|
|
|
|
var proxy2 = new Proxy(
|
|
|
|
app,
|
|
|
|
httpClient,
|
|
|
|
testServer.address.address,
|
|
|
|
port: testServer.port,
|
|
|
|
mapTo: '/foo',
|
|
|
|
);
|
2016-11-23 22:03:06 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
app.all("/proxy/*", proxy1.handleRequest);
|
|
|
|
app.all("*", proxy2.handleRequest);
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
app.fallback((req, res) {
|
2017-09-24 18:49:18 +00:00
|
|
|
print('Intercepting empty from ${req.uri}');
|
|
|
|
res.write('intercept empty');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.shutdownHooks.add((_) async {
|
|
|
|
httpClient.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.logger = new Logger('angel');
|
|
|
|
|
|
|
|
Logger.root.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
|
|
|
|
2018-11-02 04:22:32 +00:00
|
|
|
await appHttp.startServer();
|
|
|
|
|
|
|
|
server = appHttp.httpServer;
|
2016-11-23 22:03:06 +00:00
|
|
|
url = 'http://${server.address.address}:${server.port}';
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await testServer.close(force: true);
|
|
|
|
await server.close(force: true);
|
|
|
|
app = null;
|
|
|
|
url = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('publicPath', () async {
|
|
|
|
final response = await client.get('$url/proxy/hello');
|
|
|
|
print('Response: ${response.body}');
|
2018-11-02 04:22:32 +00:00
|
|
|
expect(response.body, equals('world'));
|
2016-11-23 22:03:06 +00:00
|
|
|
});
|
|
|
|
|
2017-04-22 18:46:00 +00:00
|
|
|
test('empty', () async {
|
|
|
|
var response = await client.get('$url/proxy/empty');
|
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(response.body, 'intercept empty');
|
|
|
|
});
|
|
|
|
|
2016-11-23 22:03:06 +00:00
|
|
|
test('mapTo', () async {
|
|
|
|
final response = await client.get('$url/bar');
|
|
|
|
print('Response: ${response.body}');
|
2018-11-02 04:22:32 +00:00
|
|
|
expect(response.body, equals('baz'));
|
2016-11-23 22:03:06 +00:00
|
|
|
});
|
2017-04-02 02:06:27 +00:00
|
|
|
|
|
|
|
test('original buffer', () async {
|
2018-11-02 04:22:32 +00:00
|
|
|
var response = await client
|
|
|
|
.post('$url/proxy/body', body: json.encode({'foo': 'bar'}), headers: {'content-type': 'application/json'});
|
2017-04-02 02:06:27 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(response.body, isNotEmpty);
|
2017-09-24 18:49:18 +00:00
|
|
|
expect(response.body, isNot('intercept empty'));
|
2017-04-02 02:06:27 +00:00
|
|
|
});
|
2017-01-12 00:12:23 +00:00
|
|
|
}
|