Skip to content

Commit

Permalink
feat: 增加 radar 插件 headers 的获取 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Jan 9, 2025
1 parent 13b7758 commit bad726c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ function get_radar_body(?Collection $payload): mixed
{
return $payload?->get('_body') ?? null;
}

function get_radar_headers(?Collection $payload): mixed
{
return $payload?->get('_headers') ?? null;
}
3 changes: 2 additions & 1 deletion src/Plugin/AddRadarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yansongda\Artful\Rocket;

use function Yansongda\Artful\get_radar_body;
use function Yansongda\Artful\get_radar_headers;
use function Yansongda\Artful\get_radar_method;
use function Yansongda\Artful\get_radar_url;

Expand All @@ -25,7 +26,7 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
$rocket->setRadar(new Request(
get_radar_method($payload) ?? 'POST',
get_radar_url($payload),
$this->getHeaders(),
get_radar_headers($payload) ?? $this->getHeaders(),
get_radar_body($payload),
));

Expand Down
9 changes: 9 additions & 0 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function Yansongda\Artful\get_direction;
use function Yansongda\Artful\get_packer;
use function Yansongda\Artful\get_radar_body;
use function Yansongda\Artful\get_radar_headers;
use function Yansongda\Artful\get_radar_method;
use function Yansongda\Artful\get_radar_url;
use function Yansongda\Artful\should_do_http_request;
Expand Down Expand Up @@ -112,4 +113,12 @@ public function testGetRadarBody()

self::assertEquals('https://yansongda.cn', get_radar_body(new Collection(['_body' => 'https://yansongda.cn'])));
}

public function testGetRadarHeaders()
{
self::assertNull(get_radar_headers(new Collection([])));
self::assertNull(get_radar_headers(null));

self::assertEquals(['foo' => 'bar'], get_radar_headers(new Collection(['_headers' => ['foo' => 'bar']])));
}
}
4 changes: 4 additions & 0 deletions tests/Plugin/AddRadarPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function testNormal()
$payload = new Collection([
'_url' => 'https://pay.yansongda.cn',
'_body' => '123',
'_headers' => [
'name' => 'yansongda',
]
]);

$rocket = (new Rocket())->setParams($params)->setPayload($payload);
Expand All @@ -34,5 +37,6 @@ public function testNormal()
self::assertEquals('123', (string) $radar->getBody());
self::assertEquals('POST', $radar->getMethod());
self::assertEquals('https://pay.yansongda.cn', (string) $radar->getUri());
self::assertEquals('yansongda', $radar->getHeaderLine('name'));
}
}
6 changes: 6 additions & 0 deletions web/docs/v1/appendix/special-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@
## `_http`

http 客户端的配置文件,将在 httpFactory 创建 http 客户端时使用

## `_headers`

指定请求头。

将在组装 `Radar` 请求时使用,具体可参考:`\Yansongda\Artful\Plugin\AddRadarPlugin`

0 comments on commit bad726c

Please sign in to comment.