diff --git a/src/HeaderSecurity.php b/src/HeaderSecurity.php index d12486ae..b3495f49 100644 --- a/src/HeaderSecurity.php +++ b/src/HeaderSecurity.php @@ -148,16 +148,16 @@ public static function assertValid(mixed $value): void */ public static function assertValidName(mixed $name): void { - if (! is_string($name)) { + if (! is_string($name) && ! is_numeric($name)) { throw new Exception\InvalidArgumentException(sprintf( - 'Invalid header name type; expected string; received %s', + 'Invalid header name type; expected string or numeric value; received %s', is_object($name) ? $name::class : gettype($name) )); } - if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name)) { + if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', (string) $name)) { throw new Exception\InvalidArgumentException(sprintf( '"%s" is not valid header name', - $name + (string) $name )); } } diff --git a/test/HeaderSecurityTest.php b/test/HeaderSecurityTest.php index ca7c49d9..53acf6ce 100644 --- a/test/HeaderSecurityTest.php +++ b/test/HeaderSecurityTest.php @@ -130,4 +130,21 @@ public function testAssertValidNameRaisesExceptionForInvalidName(string $value): HeaderSecurity::assertValidName($value); } + + /** @psalm-return non-empty-array */ + public function provideValidNumericHeaderNameValues(): array + { + return [ + 'zero' => [0], + 'int' => [1], + 'zero-float' => [0.0], + 'float' => [1.1], + ]; + } + + /** @dataProvider provideValidNumericHeaderNameValues */ + public function testAssertValidNameDoesNotRaiseExceptionForValidNumericValues(int|float $value): void + { + $this->assertNull(HeaderSecurity::assertValidName($value)); + } }