refactor(streaming): extract appendHeaderArgument from startStream

The X-XC_VM-Detect / X-XC_VM-Prebuffer header injection was duplicated three
times in startStream: each block looped the argument list to append the header
to an existing 'headers' entry and added a new one if absent. Collapse the three
copies into a single appendHeaderArgument() helper and drop the shared
$rProcessed flag they threaded through (it leaked state between the blocks).

Behaviour is unchanged: the helper uses a local applied flag, and the three call
sites produce the same argument lists as before. Add unit tests covering
append-to-existing, create-when-absent, append-to-every-headers-entry, and the
empty-list case.

Verified: phpstan level 5 green, phpunit 373/373, make gates green.
This commit is contained in:
Divarion_D
2026-08-07 20:32:51 +03:00
parent a1c1d64139
commit 8973f5580f
2 changed files with 71 additions and 31 deletions
@@ -185,4 +185,47 @@ final class StreamProcessMovieOutputTest extends TestCase {
public function testCurrentLastLeavesOrderUnchanged(): void {
$this->assertSame(['a', 'b', 'c', 'd'], $this->call('rotateSourcesPastCurrent', ['a', 'b', 'c', 'd'], 0, 'd'));
}
// ── appendHeaderArgument (startStream header injection) ─────
public function testHeaderAppendedToExistingHeadersArg(): void {
$args = [
['argument_key' => 'user_agent', 'value' => 'VLC'],
['argument_key' => 'headers', 'value' => 'X-A:1'],
];
$out = $this->call('appendHeaderArgument', $args, 'X-XC_VM-Detect:1');
$this->assertCount(2, $out, 'appended, not added');
$this->assertSame("X-A:1\r\nX-XC_VM-Detect:1", $out[1]['value']);
$this->assertSame('VLC', $out[0]['value'], 'other args untouched');
}
public function testHeaderArgCreatedWhenAbsent(): void {
$args = [['argument_key' => 'user_agent', 'value' => 'VLC']];
$out = $this->call('appendHeaderArgument', $args, 'X-XC_VM-Prebuffer:1');
$this->assertCount(2, $out);
$new = $out[array_key_last($out)];
$this->assertSame('headers', $new['argument_key']);
$this->assertSame('X-XC_VM-Prebuffer:1', $new['value']);
$this->assertSame('fetch', $new['argument_cat']);
$this->assertSame("-headers '%s\r\n'", $new['argument_cmd']);
}
public function testHeaderAppendedToEveryHeadersArg(): void {
// mirrors the original loop: every 'headers' entry receives the line.
$args = [
['argument_key' => 'headers', 'value' => 'A'],
['argument_key' => 'headers', 'value' => 'B'],
];
$out = $this->call('appendHeaderArgument', $args, 'X:1');
$this->assertSame("A\r\nX:1", $out[0]['value']);
$this->assertSame("B\r\nX:1", $out[1]['value']);
$this->assertCount(2, $out, 'no new arg added');
}
public function testEmptyArgumentsGetsNewHeaderArg(): void {
$out = $this->call('appendHeaderArgument', [], 'X:1');
$this->assertCount(1, $out);
$this->assertSame('headers', $out[0]['argument_key']);
$this->assertSame('X:1', $out[0]['value']);
}
}