Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce iterable to headers helper #53

Merged
merged 1 commit into from
Sep 4, 2024
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
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ This middleware adds all the headers passed into the constructor to the response
# Usage

```php
$server = new \React\Http\HttpServer([
use React\Http\HttpServer;
use WyriHaximus\React\Http\Middleware\Header;
use WyriHaximus\React\Http\Middleware\WithHeadersMiddleware;
use WyriHaximus\React\Http\Middleware\WithRandomHeadersMiddleware;

$server = new HttpServer([
/** Other middleware */
new WithHeadersMiddleware(
'X-Powered-By' => 'wyrihaximus.net (11.0.33)',
Expand All @@ -39,21 +44,29 @@ Combined with [`wyrihaximus-net/x-headers`](https://github.com/WyriHaximusNet/ph
set of Nerdy headers:

```php
$server = new \React\Http\HttpServer([
use React\Http\HttpServer;
use WyriHaximus\React\Http\Middleware\Headers;
use WyriHaximus\React\Http\Middleware\WithRandomHeadersMiddleware;
use WyriHaximusNet\XHeaders;

$server = new HttpServer([
/** Other middleware */
new WithRandomHeadersMiddleware(
1,
ceil(count(Headers::HEADERS) / 4), // Add up to 25% of the list to it
...(static function (array $headers): iterable {
foreach ($headers as $key => $value) {
yield new Header($key, $value);
}
})(Headers::HEADERS),
ceil(count(XHeaders\Headers::HEADERS) / 4), // Add up to 25% of the list to it
...Headers::fromIterable(XHeaders\Headers::HEADERS),
),
/** Other middleware */
]);
```

This uses the `Headers` helper that can transform key value iterables like the following to an iterable with `Header` objects:
```php
[
'X-Header' => 'contents',
]
```

# License

The MIT License (MIT)
Expand Down
20 changes: 20 additions & 0 deletions src/Headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace WyriHaximus\React\Http\Middleware;

final readonly class Headers
{
/**
* @param iterable<string, string> $headers
*
* @return iterable<Header>
*/
public static function fromIterable(iterable $headers): iterable
{
foreach ($headers as $header => $contents) {
yield new Header($header, $contents);
}
}
}
45 changes: 45 additions & 0 deletions tests/HeadersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace WyriHaximus\React\Tests\Http\Middleware;

use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\React\Http\Middleware\Headers;

final class HeadersTest extends AsyncTestCase
{
/**
* @param iterable<string, string> $headers
*
* @test
* @dataProvider provideHeaderIterables
*/
public function fromIterable(iterable $headers): void
{
$headerObjects = [...Headers::fromIterable($headers)];

self::assertCount(2, $headerObjects);

self::assertSame('X-A', $headerObjects[0]->header);
self::assertSame('a', $headerObjects[0]->contents);

self::assertSame('X-B', $headerObjects[1]->header);
self::assertSame('b', $headerObjects[1]->contents);
}

/** @return iterable<array<iterable<string, string>>> */
public static function provideHeaderIterables(): iterable
{
$headers = [
'X-A' => 'a',
'X-B' => 'b',
];

yield [$headers];

yield [
(static fn (): iterable => yield from $headers)(),
];
}
}
Loading