Skip to content

Commit

Permalink
Release/3.0.2 - (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Feb 4, 2024
1 parent cdd43d7 commit 827a8c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
17 changes: 15 additions & 2 deletions src/HttpHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public static function build(): HttpHeaders
return new HttpHeaders();
}

public function addFrom(string $key, mixed $value): HttpHeaders
{
$this->values[$key][] = $value;

return $this;
}

public function addFromCode(HttpCode $code): HttpHeaders
{
$template = 'HTTP/1.1 %s';
$this->values['Status'][] = sprintf($template, $code->message());
$this->values['Status'][] = $code->message();

return $this;
}
Expand All @@ -33,6 +39,13 @@ public function addFromContentType(Header $header): HttpHeaders
return $this;
}

public function removeFrom(string $key): HttpHeaders
{
unset($this->values[$key]);

return $this;
}

public function getHeader(string $key): array
{
return $this->values[$key] ?? [];
Expand Down
8 changes: 6 additions & 2 deletions src/Internal/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterf

public function withHeader(string $name, mixed $value): MessageInterface
{
throw new BadMethodCall(method: __METHOD__);
$this->headers->addFrom(key: $name, value: $value);

return $this;
}

public function withoutHeader(string $name): MessageInterface
{
throw new BadMethodCall(method: __METHOD__);
$this->headers->removeFrom(key: $name);

return $this;
}

public function withAddedHeader(string $name, mixed $value): MessageInterface
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function providerData(): array
private function defaultHeaderFrom(HttpCode $code): array
{
return [
'Status' => [sprintf('HTTP/1.1 %s', $code->message())],
'Status' => [$code->message()],
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
];
}
Expand Down
33 changes: 17 additions & 16 deletions tests/Internal/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testDefaultHeaders(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$expected = [
'Status' => [sprintf('HTTP/1.1 %s', HttpCode::OK->message())],
'Status' => [HttpCode::OK->message()],
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
];

Expand Down Expand Up @@ -57,44 +57,45 @@ public function testGetHeaderLine(): void
self::assertEquals(HttpContentType::APPLICATION_JSON->value, $response->getHeaderLine(name: 'Content-Type'));
}

public function testExceptionWhenBadMethodCallOnWithBody(): void
public function testWithHeader(): void
{
$value = '2850bf62-8383-4e9f-b237-d41247a1df3b';
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$response->withHeader(name: 'Token', value: $value);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withBody> cannot be used.');
$expected = [$value];

$response->withBody(body: StreamFactory::from(data: []));
self::assertEquals($expected, $response->getHeader(name: 'Token'));
}

public function testExceptionWhenBadMethodCallOnWithStatus(): void
public function testWithoutHeader(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$response->withoutHeader('Status');
$expected = [HttpContentType::APPLICATION_JSON->value];

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withStatus> cannot be used.');

$response->withStatus(code: HttpCode::OK->value);
self::assertEmpty($response->getHeader(name: 'Status'));
self::assertEquals($expected, $response->getHeader(name: 'Content-Type'));
}

public function testExceptionWhenBadMethodCallOnWithHeader(): void
public function testExceptionWhenBadMethodCallOnWithBody(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withHeader> cannot be used.');
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withBody> cannot be used.');

$response->withHeader(name: '', value: '');
$response->withBody(body: StreamFactory::from(data: []));
}

public function testExceptionWhenBadMethodCallOnWithoutHeader(): void
public function testExceptionWhenBadMethodCallOnWithStatus(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withoutHeader> cannot be used.');
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withStatus> cannot be used.');

$response->withoutHeader(name: '');
$response->withStatus(code: HttpCode::OK->value);
}

public function testExceptionWhenBadMethodCallOnWithAddedHeader(): void
Expand Down

0 comments on commit 827a8c3

Please sign in to comment.