halkyon/packages/halkyon_app/test/instance/instance_test.dart

105 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/halkyon_app.dart';
void main() {
test('reads an address as people type it, HTTPS unless they say not', () {
for (final address in [
'learn.example.org',
'https://learn.example.org',
' HTTPS://Learn.Example.org/ ',
]) {
expect(
Instance.parse(address)?.url,
Uri.parse('https://learn.example.org/'),
reason: address,
);
}
expect(
Instance.parse('http://localhost:8080')?.url,
Uri.parse('http://localhost:8080/'),
);
expect(
Instance.parse('example.org/learn?x=1#top')?.url,
Uri.parse('https://example.org/learn/'),
);
});
test('reaches this machine over HTTP where no scheme is typed, as a '
"server running on it has no certificate (the developer's review, "
'9 Nov 2026)', () {
for (final (address, url) in [
('localhost:8080', 'http://localhost:8080/'),
('LOCALHOST', 'http://localhost/'),
('127.0.0.1:8080', 'http://127.0.0.1:8080/'),
('[::1]:8080', 'http://[::1]:8080/'),
('https://localhost:8443', 'https://localhost:8443/'),
]) {
expect(Instance.parse(address)?.url, Uri.parse(url), reason: address);
}
});
test('takes an IP address as a host', () {
expect(
Instance.parse('192.168.1.10:8080')?.url,
Uri.parse('https://192.168.1.10:8080/'),
);
expect(
Instance.parse('http://[2001:db8::1]')?.url,
Uri.parse('http://[2001:db8::1]/'),
);
});
test('refuses what is no address', () {
for (final address in [
'',
' ',
'learn example org',
'ftp://example.org',
'https://',
'https://someone@example.org',
// A host of one word that isn't this machine, which no instance on
// the internet has: refused as typed rather than looked for
// (the developer's review, 9 Nov 2026).
'todo',
'https://learn',
'learn.',
'.org',
]) {
expect(Instance.parse(address), isNull, reason: address);
}
});
test('is named by its host, and its port if it has one', () {
expect(Instance.parse('learn.halkyon.eu')!.name, 'learn.halkyon.eu');
expect(Instance.parse('localhost:8080')!.name, 'localhost:8080');
});
test('is the same instance however its address was written', () {
expect(
Instance.parse('learn.example.org'),
Instance.parse('https://LEARN.example.org/'),
);
});
test('is reached securely over HTTPS, or over HTTP on this machine only', () {
for (final address in [
'learn.example.org',
'https://learn.example.org:8443/learn',
'http://localhost:8080',
'http://127.0.0.1:8080',
'http://127.1.2.3',
'http://[::1]:8080',
]) {
expect(Instance.parse(address)!.isSecure, isTrue, reason: address);
}
for (final address in [
'http://learn.example.org',
'http://10.0.2.2:8080',
'http://192.168.1.10:8080',
'http://localhost.example.org',
'http://[::2]',
]) {
expect(Instance.parse(address)!.isSecure, isFalse, reason: address);
}
});
}