Skip to content

Commit

Permalink
Merge pull request #245 from bearsunday/http-client
Browse files Browse the repository at this point in the history
Format http response headers
  • Loading branch information
koriym authored May 9, 2021
2 parents a35ac54 + 27e2448 commit e269403
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/HalRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function assert;
use function http_build_query;
use function is_array;
use function is_object;
use function is_scalar;
use function json_decode;
use function method_exists;
Expand Down Expand Up @@ -63,6 +64,10 @@ public function renderHal(ResourceObject $ro): void

private function valuateElements(ResourceObject $ro): void
{
if (is_object($ro->body)) {
$ro->body = (array) $ro->body;
}

assert(is_array($ro->body));
/** @var mixed $embeded */
foreach ($ro->body as $key => &$embeded) {
Expand Down
23 changes: 22 additions & 1 deletion src/HttpResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

use function count;
use function is_array;
use function strtoupper;
use function ucwords;

/**
* @method HttpResourceObject get(AbstractUri|string $uri, array<string, mixed> $params = [])
Expand Down Expand Up @@ -56,7 +58,10 @@ public function __get(string $name)
}

if ($name === 'headers') {
return $this->response->getHeaders();
/** @var array<string, array<string>> $headers */
$headers = $this->response->getHeaders();

return $this->formatHeaeder($headers);
}

if ($name === 'body') {
Expand All @@ -70,6 +75,22 @@ public function __get(string $name)
throw new InvalidArgumentException($name);
}

/**
* @param array<string, array<string>> $headers
*
* @return array<string, string|array<string>>
*/
private function formatHeaeder(array $headers): array
{
$formated = [];
foreach ($headers as $key => $header) {
$ucFirstKey = ucwords($key);
$formated[$ucFirstKey] = count($header) === 1 ? $header[0] : $header;
}

return $formated;
}

/**
* @param mixed $value
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class ResourceObject implements AcceptTransferInterface, ArrayAccess, C
/**
* Body
*
* @var array<int|string, mixed>|mixed
* @var mixed
*/
public $body;

Expand Down
10 changes: 5 additions & 5 deletions tests/HttpResourceObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testGet(): HttpResourceObject
{
$response = $this->resource->get('http://httpbin.org/get', ['foo' => 'bar']);
$this->assertSame(200, $response->code);
$this->assertArrayHasKey('access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('Access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('args', $response->body);
$this->assertStringContainsString('"args": {', (string) $response->view);
assert($response instanceof HttpResourceObject);
Expand All @@ -39,7 +39,7 @@ public function testPost(): void
{
$response = $this->resource->post('http://httpbin.org/post', ['foo' => 'bar']);
$this->assertSame(200, $response->code);
$this->assertArrayHasKey('access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('Access-control-allow-credentials', $response->headers);
$body = $response->body;
$this->assertSame('bar', $body['form']['foo']);
$this->assertStringContainsString('"form": {', (string) $response->view);
Expand All @@ -49,7 +49,7 @@ public function testPut(): void
{
$response = $this->resource->put('http://httpbin.org/put', ['foo' => 'bar']);
$this->assertSame(200, $response->code);
$this->assertArrayHasKey('access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('Access-control-allow-credentials', $response->headers);
$body = $response->body;
$this->assertSame('bar', $body['form']['foo']);
$this->assertStringContainsString('"form": {', (string) $response->view);
Expand All @@ -59,7 +59,7 @@ public function testPatch(): void
{
$response = $this->resource->patch('http://httpbin.org/patch', ['foo' => 'bar']);
$this->assertSame(200, $response->code);
$this->assertArrayHasKey('access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('Access-control-allow-credentials', $response->headers);
$body = $response->body;
$this->assertSame('bar', $body['form']['foo']);
$this->assertStringContainsString('"form": {', (string) $response->view);
Expand All @@ -69,7 +69,7 @@ public function testDelete(): void
{
$response = $this->resource->delete('http://httpbin.org/delete', ['foo' => 'bar']);
$this->assertSame(200, $response->code);
$this->assertArrayHasKey('access-control-allow-credentials', $response->headers);
$this->assertArrayHasKey('Access-control-allow-credentials', $response->headers);
$body = $response->body;
$this->assertSame('bar', $body['form']['foo']);
$this->assertStringContainsString('"form": {', (string) $response->view);
Expand Down
11 changes: 11 additions & 0 deletions tests/Renderer/HalRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ public function testRenderNullBody(): void
EOT;
$this->assertSame($expected, $actual);
}

public function testBodyObject(): void
{
$this->ro->body = new class {
/** @var int */
public $a = 1;
};
$actual = (string) $this->ro;

$this->assertStringContainsString('"a": 1,', $actual);
}
}

0 comments on commit e269403

Please # to comment.