Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ private function arrayToHeaderValue(array $headers = []): array
* @var array<array-key, string> $value
*/
foreach ($headers as $key => $value) {
$result[$key] = new HeaderValue(['value' => $value]);
/** @psalm-suppress DocblockTypeContradiction */
$value = \array_filter(\is_array($value) ? $value : [$value], static fn (mixed $v): bool => \is_string($v));
if ($value !== []) {
$result[$key] = new HeaderValue(['value' => $value]);
}
}

return $result;
Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/HttpWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ public function testRespondWithProtoCodec(): void
$worker->respond(200, 'foo', ['Content-Type' => ['application/x-www-form-urlencoded']]);
}

#[DataProvider('headersDataProvider')]
public function testRespondWithProtoCodecWithHeaders(array $headers, array $expected): void
{
$expectedHeader = new Response(['status' => 200, 'headers' => $expected]);

$worker = $this->createMock(WorkerInterface::class);
$worker->expects($this->once())
->method('respond')
->with(new Payload('foo', $expectedHeader->serializeToString()), Frame::CODEC_PROTO);

(new \ReflectionProperty(HttpWorker::class, 'codec'))->setValue(Frame::CODEC_PROTO);
$worker = new HttpWorker($worker);

$worker->respond(200, 'foo', $headers);
}

public function testRespondWithJsonCodec(): void
{
$worker = $this->createMock(WorkerInterface::class);
Expand Down Expand Up @@ -262,6 +278,39 @@ public static function emptyRequestDataProvider(): \Traversable
yield [new Payload(null, null)];
}

public static function headersDataProvider(): \Traversable
{
yield [
['Content-Type' => ['application/x-www-form-urlencoded']],
['Content-Type' => new HeaderValue(['value' => ['application/x-www-form-urlencoded']])]
];
yield [
['Content-Type' => ['application/x-www-form-urlencoded'], 'X-Test' => ['foo', 'bar']],
[
'Content-Type' => new HeaderValue(['value' => ['application/x-www-form-urlencoded']]),
'X-Test' => new HeaderValue(['value' => ['foo', 'bar']]),
]
];
yield [['Content-Type' => [null]], []];
yield [['Content-Type' => [1]], []];
yield [['Content-Type' => [true]], []];
yield [['Content-Type' => [false]], []];
yield [['Content-Type' => [new \stdClass()]], []];
yield [['Content-Type' => [1.5]], []];
yield [
['X-Test' => ['foo', 'bar'], 'X-Test2' => ['foo', null], 'X-Test3' => [null, 1]],
[
'X-Test' => new HeaderValue(['value' => ['foo', 'bar']]),
'X-Test2' => new HeaderValue(['value' => ['foo']]),
]
];
yield [
['Content-Type' => 'application/x-www-form-urlencoded'],
['Content-Type' => new HeaderValue(['value' => ['application/x-www-form-urlencoded']])]
];
yield [['Content-Type' => new \stdClass()], []];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't cover the change. It's better to use (object)['foo' => 'bar'] instead of stdClass

}

private static function createProtoRequest(array $values): \RoadRunner\HTTP\DTO\V1\Request
{
$toHeaderValue = static function (string $key, bool $wrap = true) use (&$values): void {
Expand Down