Skip to content

Commit

Permalink
Header fix, additional
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jan 8, 2024
1 parent 82a0166 commit 78f61b2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
4 changes: 4 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

> PHP version `^8.0`
### `2.1.1`

* Fix for HTTP headers (@zgrguric, @sirn-se)

### `2.1.0`

* Http & Tick middleware support (@sirn-se)
Expand Down
3 changes: 2 additions & 1 deletion docs/Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ make coverage
* Antonio Mora
* Simon Podlipsky
* etrinh
* zgrguric
* zgrguric
* axklim
7 changes: 6 additions & 1 deletion src/Http/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ private function handleHeader(string $name, string|array $value): void
if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
}
$value = is_array($value) ? $value : [$value];
$value = array_map(function (string|array $item) {
return trim($item);
}, is_array($value) ? $value : [$value]);
if (empty($value)) {
throw new InvalidArgumentException("Invalid header value(s) provided.");
}
foreach ($value as $content) {
$content = trim($content);
if ('' === $content) {
Expand Down
35 changes: 15 additions & 20 deletions tests/suites/http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace WebSocket\Test\Http;

use BadMethodCallException;
use Generator;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Phrity\Net\StreamFactory;
Expand Down Expand Up @@ -215,7 +216,7 @@ public function testWithBodyError(): void
$request->withBody($factory->createStream());
}

public function testHaederNameError(): void
public function testHeaderNameError(): void
{
$request = new Request();
$this->expectException(InvalidArgumentException::class);
Expand All @@ -224,48 +225,42 @@ public function testHaederNameError(): void
$request->withHeader('.', 'invaid name');
}

public function testHaederValueError(): void
{
$request = new Request();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage("Invalid header value(s) provided.");
$request->withHeader('name', '');
}

/**
* @dataProvider provideInvalidHeaderValues
* @
*/
public function testHeaderValueInvalidVariants($value): void
public function testHeaderValueInvalidVariants(mixed $value): void
{
$request = new Request();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage("Invalid header value(s) provided.");
$request->withHeader('name', $value);
}

public static function provideInvalidHeaderValues(): \Generator
public static function provideInvalidHeaderValues(): Generator
{
yield [''];
yield [' '];
yield [['0', '']];
yield [[]];
}

/**
* @dataProvider provideValidHeaderValues
* @
*/
public function testHeaderValueValidVariants($value): void
public function testHeaderValueValidVariants(mixed $value, array $expected): void
{
$request = new Request();
$request = $request->withHeader('name', $value);
$this->assertInstanceOf(Request::class, $request);
$this->assertEquals($expected, $request->getHeaders());
}

public static function provideValidHeaderValues(): \Generator
public static function provideValidHeaderValues(): Generator
{
yield ['null'];
yield ['0'];
yield [' 0'];
yield ['1'];
yield ['null', ['name' => ['null']]];
yield ['0 ', ['name' => ['0']]];
yield [' 0', ['name' => ['0']]];
yield [['0', '1'], ['name' => ['0', '1']]];
}
}

0 comments on commit 78f61b2

Please sign in to comment.