fix(plex): tolerate transient resource failures

This commit is contained in:
edde746
2026-05-16 22:54:53 +02:00
parent 3798a0c332
commit cfd4aa307d
7 changed files with 342 additions and 7 deletions
+69
View File
@@ -0,0 +1,69 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:plezy/exceptions/media_server_exceptions.dart';
import 'package:plezy/services/plex_auth_service.dart';
import 'package:plezy/utils/media_server_http_client.dart';
void main() {
group('PlexAuthService', () {
test('fetchServers retries resources through plex.tv after clients host transport failure', () async {
final hosts = <String>[];
final client = MediaServerHttpClient(
client: MockClient((request) async {
hosts.add(request.url.host);
if (request.url.host == 'clients.plex.tv') {
throw http.ClientException('DNS failed', request.url);
}
return http.Response(jsonEncode([_serverJson()]), 200, headers: {'content-type': 'application/json'});
}),
);
addTearDown(client.close);
final auth = PlexAuthService.forTesting(http: client);
final servers = await auth.fetchServers('token');
expect(hosts, ['clients.plex.tv', 'plex.tv']);
expect(servers.single.clientIdentifier, 'srv-1');
});
test('fetchServers does not retry canonical host for HTTP auth failures', () async {
final hosts = <String>[];
final client = MediaServerHttpClient(
client: MockClient((request) async {
hosts.add(request.url.host);
return http.Response('{"errors":[]}', 401, headers: {'content-type': 'application/json'});
}),
);
addTearDown(client.close);
final auth = PlexAuthService.forTesting(http: client);
await expectLater(
auth.fetchServers('bad-token'),
throwsA(isA<MediaServerHttpException>().having((e) => e.statusCode, 'statusCode', 401)),
);
expect(hosts, ['clients.plex.tv']);
});
});
}
Map<String, dynamic> _serverJson() => {
'name': 'Home Server',
'clientIdentifier': 'srv-1',
'accessToken': 'server-token',
'owned': true,
'provides': 'server',
'connections': [
{
'protocol': 'https',
'address': '192.168.1.3',
'port': 32400,
'uri': 'https://192-168-1-3.machine.plex.direct:32400',
'local': true,
'relay': false,
'IPv6': false,
},
],
};
@@ -146,5 +146,37 @@ void main() {
expect(urls.first, preferred);
expect(urls, contains(localPlexDirect));
});
test('does not treat Tailscale CGNAT preferred endpoint as remote', () {
const preferred = 'https://100.90.80.70:32400';
const localPlexDirect = 'https://192-168-1-50.abc.plex.direct:32400';
final server = PlexServer.fromJson(
_serverJsonWithConnections([
_connectionJson(protocol: 'https', address: '192.168.1.50', port: 32400, uri: localPlexDirect, local: true),
]),
);
final urls = server.prioritizedEndpointUrls(preferredFirst: preferred);
expect(server.networkClassForUrl(preferred), PlexNetworkClass.unknown);
expect(urls.first, preferred);
expect(urls, contains(localPlexDirect));
});
test('does not treat Tailscale MagicDNS preferred endpoint as remote', () {
const preferred = 'https://plex.tailnet.ts.net:32400';
const localPlexDirect = 'https://192-168-1-50.abc.plex.direct:32400';
final server = PlexServer.fromJson(
_serverJsonWithConnections([
_connectionJson(protocol: 'https', address: '192.168.1.50', port: 32400, uri: localPlexDirect, local: true),
]),
);
final urls = server.prioritizedEndpointUrls(preferredFirst: preferred);
expect(server.networkClassForUrl(preferred), PlexNetworkClass.unknown);
expect(urls.first, preferred);
expect(urls, contains(localPlexDirect));
});
});
}