Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for easy connection to an AtSecondaryReverseProxy #194

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,16 @@ class SecondaryUrlFinder {
SecondaryUrlFinder(this._rootDomain, this._rootPort);

Future<String?> findSecondaryUrl(String atSign) async {
return await _findSecondary(atSign);
if (_rootDomain.startsWith("proxy:")) {
// In order to make it easy for clients to connect to a reverse proxy
// instead of doing a root lookup, we adopt the convention that:
// if the rootDomain starts with 'proxy:'
// then the secondary domain name will be deemed to be the portion of rootDomain after 'proxy:'
// and the secondary port will be deemed to be the rootPort
return '${_rootDomain.substring("proxy:".length)}:$_rootPort';
} else {
return await _findSecondary(atSign);
}
}

Future<String?> _findSecondary(String atsign) async {
Expand Down
72 changes: 60 additions & 12 deletions at_lookup/test/secondary_address_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import 'package:mocktail/mocktail.dart';
class MockSecondaryFinder extends Mock implements SecondaryUrlFinder {}

void main() async {
String rootDomain = 'root.atsign.unit.tests';
int rootPort = 64;
SecondaryUrlFinder mockSecondaryFinder = MockSecondaryFinder();

String _addressFromAtSign(String atSign) {
if (atSign.startsWith('@')) {
atSign = atSign.replaceFirst('@', '');
}
return '$atSign.secondaries.unit.tests:1001';
}

group('this should be moved to functional tests', () {
test('look up @cicd1 from root.atsign.wtf:64', () async {
var secondaryAddress =
Expand All @@ -29,7 +18,19 @@ void main() async {
});
});

group('some cache tests', () {
group('some cache tests with a MockSecondaryFinder', () {
String rootDomain = 'root.atsign.unit.tests';
int rootPort = 64;

SecondaryUrlFinder mockSecondaryFinder = MockSecondaryFinder();

String _addressFromAtSign(String atSign) {
if (atSign.startsWith('@')) {
atSign = atSign.replaceFirst('@', '');
}
return '$atSign.secondaries.unit.tests:1001';
}

late CacheableSecondaryAddressFinder cache;

setUp(() {
Expand Down Expand Up @@ -98,6 +99,7 @@ void main() async {
expect((approxExpiry - cache.getCacheExpiryTime(atSign)!) < 100, true);
});

// TODO Why are these tests commented out?
// test('test expiry time - custom cache expiry for registeredAtSign1',
// () async {
// var atSign = 'registeredAtSign1';
Expand All @@ -115,4 +117,50 @@ void main() async {
// expect(cache.cacheContains(atSign), true);
// });
});

group('some cache tests with a real SecondaryUrlFinder but with rootDomain set to proxy:<something>', () {
String proxyHost = 'vip.ve.atsign.zone';
String rootDomain = 'proxy:$proxyHost';
int rootPort = 8443;

String _addressFromAtSign(String atSign) {
return '$proxyHost:$rootPort';
}

late CacheableSecondaryAddressFinder cache;

setUp(() {
cache = CacheableSecondaryAddressFinder(rootDomain, rootPort);
});

test('test simple lookup for @registeredAtSign1', () async {
var atSign = '@registeredAtSign1';
var secondaryAddress = await cache.findSecondary(atSign);
expect(secondaryAddress.port, isNotNull);
expect(secondaryAddress.host, isNotNull);
expect(secondaryAddress.toString(), _addressFromAtSign(atSign));
});
test('test simple lookup for registeredAtSign1', () async {
var atSign = 'registeredAtSign1';
var secondaryAddress = await cache.findSecondary(atSign);
expect(secondaryAddress.port, isNotNull);
expect(secondaryAddress.host, isNotNull);
expect(secondaryAddress.toString(), _addressFromAtSign(atSign));
});
test('test isCached for registeredAtSign1', () async {
var atSign = 'registeredAtSign1';
await cache.findSecondary(atSign);
expect(cache.cacheContains(atSign), true);
});

test('test expiry time - default cache expiry for registeredAtSign1',
() async {
var atSign = 'registeredAtSign1';
await cache.findSecondary(atSign);
final approxExpiry =
DateTime.now().add(Duration(hours: 1)).millisecondsSinceEpoch;
expect(cache.getCacheExpiryTime(atSign), isNotNull);
expect((approxExpiry - cache.getCacheExpiryTime(atSign)!) < 100, true);
});
});
}