2017-04-02 02:06:27 +00:00
|
|
|
import 'dart:convert';
|
2016-11-23 22:03:06 +00:00
|
|
|
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';
|
2018-11-08 22:13:26 +00:00
|
|
|
import 'package:http/io_client.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';
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void main() {
|
2021-06-10 08:47:05 +00:00
|
|
|
Angel? app;
|
2019-05-02 23:19:30 +00:00
|
|
|
var client = http.IOClient();
|
2021-06-10 08:47:05 +00:00
|
|
|
//late HttpServer server;
|
|
|
|
late HttpServer testServer;
|
|
|
|
String? url;
|
2016-11-23 22:03:06 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2019-05-02 23:19:30 +00:00
|
|
|
app = Angel();
|
2021-06-10 08:47:05 +00:00
|
|
|
var appHttp = AngelHttp(app!);
|
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
|
|
|
|
2019-05-02 23:19:30 +00:00
|
|
|
var proxy1 = Proxy(
|
|
|
|
Uri(
|
2018-11-09 00:27:50 +00:00
|
|
|
scheme: 'http',
|
|
|
|
host: testServer.address.address,
|
|
|
|
port: testServer.port),
|
2017-09-24 18:49:18 +00:00
|
|
|
publicPath: '/proxy',
|
|
|
|
);
|
2018-11-09 00:27:50 +00:00
|
|
|
|
2019-10-12 15:19:57 +00:00
|
|
|
var proxy2 = Proxy(proxy1.baseUrl.replace(path: '/foo'));
|
2018-11-09 00:27:50 +00:00
|
|
|
print('Proxy 1 on: ${proxy1.baseUrl}');
|
|
|
|
print('Proxy 2 on: ${proxy2.baseUrl}');
|
2016-11-23 22:03:06 +00:00
|
|
|
|
2021-06-10 08:47:05 +00:00
|
|
|
app!.all('/proxy/*', proxy1.handleRequest);
|
|
|
|
app!.all('*', proxy2.handleRequest);
|
2017-09-24 18:49:18 +00:00
|
|
|
|
2021-06-10 08:47:05 +00:00
|
|
|
app!.fallback((req, res) {
|
2017-09-24 18:49:18 +00:00
|
|
|
print('Intercepting empty from ${req.uri}');
|
|
|
|
res.write('intercept empty');
|
|
|
|
});
|
|
|
|
|
2021-06-10 08:47:05 +00:00
|
|
|
app!.logger = Logger('angel');
|
2017-09-24 18:49:18 +00:00
|
|
|
|
|
|
|
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();
|
2018-12-09 22:39:11 +00:00
|
|
|
url = appHttp.uri.toString();
|
2016-11-23 22:03:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
2021-06-10 08:47:05 +00:00
|
|
|
await testServer.close(force: true);
|
|
|
|
//await server.close(force: true);
|
2016-11-23 22:03:06 +00:00
|
|
|
app = null;
|
|
|
|
url = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('publicPath', () async {
|
2021-06-10 08:47:05 +00:00
|
|
|
final response = await client.get(Uri.parse('$url/proxy/hello'));
|
2016-11-23 22:03:06 +00:00
|
|
|
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 {
|
2021-06-10 08:47:05 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/proxy/empty'));
|
2017-04-22 18:46:00 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(response.body, 'intercept empty');
|
|
|
|
});
|
|
|
|
|
2016-11-23 22:03:06 +00:00
|
|
|
test('mapTo', () async {
|
2021-06-10 08:47:05 +00:00
|
|
|
final response = await client.get(Uri.parse('$url/bar'));
|
2016-11-23 22:03:06 +00:00
|
|
|
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 {
|
2021-06-10 08:47:05 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/proxy/body'),
|
2018-11-08 22:13:26 +00:00
|
|
|
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
|
|
|
}
|