Files
plezy/test/utils/device_identity_test.dart
T
edde746 53535e1678 fix(jellyfin): percent-encode the MediaBrowser auth header
Since real device names started reaching the header, an accented one
made login impossible: dart:io refuses header values above 0x7F, and
CFNetwork puts the raw code unit on the wire as a Latin-1 byte, which
Kestrel rejects as a malformed request with 400 before Jellyfin routes
POST /Users/AuthenticateByName.

Encode every field the way the official Jellyfin SDK does; the server
already reverses it with WebUtility.UrlDecode, so the wire value stays
pure ASCII while the device list shows the real name. Quotes, commas
and `=` no longer need stripping either. sanitizeHeaderValue, which
still guards the Plex headers, now folds Latin letters to their base
form instead of emitting bytes no transport accepts.

close #1685
2026-07-28 05:07:00 +02:00

57 lines
2.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:plezy/utils/device_identity.dart';
void main() {
group('sanitizeHeaderValue', () {
test('passes plain ASCII names through trimmed', () {
expect(sanitizeHeaderValue(' Living Room TV '), 'Living Room TV');
});
// Regression: header values above 0x7F make dart:io throw and CFNetwork
// emit a Latin-1 byte that UTF-8 header parsers reject as a bad request.
test('folds accented Latin letters instead of emitting them', () {
expect(sanitizeHeaderValue("Édouard's Mac"), "Edouard's Mac");
expect(sanitizeHeaderValue('Bjørn stue-TV'), 'Bjorn stue-TV');
expect(sanitizeHeaderValue('Håkons Æra Straße'), 'Hakons AEra Strasse');
expect(sanitizeHeaderValue('Łukasz Đorđe'), 'Lukasz Dorde');
});
test('drops code units that have no ASCII equivalent', () {
expect(sanitizeHeaderValue('📱 Bob\'s iPhone'), "Bob's iPhone");
expect(sanitizeHeaderValue('电视'), isNull);
});
test('strips HTTP control characters', () {
expect(sanitizeHeaderValue('\u0000Living\u001f Room\u007f TV'), 'Living Room TV');
expect(sanitizeHeaderValue('evil\r\nX-Injected: 1'), 'evilX-Injected: 1');
});
test('returns null for null, empty, and whitespace-only input', () {
expect(sanitizeHeaderValue(null), isNull);
expect(sanitizeHeaderValue(''), isNull);
expect(sanitizeHeaderValue(' '), isNull);
});
});
group('DeviceIdentityService.debugOverride', () {
tearDown(() => DeviceIdentityService.debugOverride(null));
test('resolve returns the overridden identity', () async {
const identity = DeviceIdentity(platform: 'TestOS', deviceModel: 'Model-X', deviceName: 'Unit Test', isTv: true);
DeviceIdentityService.debugOverride(identity);
final resolved = await DeviceIdentityService.resolve();
expect(resolved.platform, 'TestOS');
expect(resolved.deviceModel, 'Model-X');
expect(resolved.deviceName, 'Unit Test');
expect(resolved.isTv, isTrue);
});
test('a later override replaces the memoized value', () async {
DeviceIdentityService.debugOverride(const DeviceIdentity(platform: 'First'));
expect((await DeviceIdentityService.resolve()).platform, 'First');
DeviceIdentityService.debugOverride(const DeviceIdentity(platform: 'Second'));
expect((await DeviceIdentityService.resolve()).platform, 'Second');
});
});
}