Skip to content

Commit 827a8c3

Browse files
authored
Release/3.0.2 - (#26)
1 parent cdd43d7 commit 827a8c3

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

src/HttpHeaders.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ public static function build(): HttpHeaders
1818
return new HttpHeaders();
1919
}
2020

21+
public function addFrom(string $key, mixed $value): HttpHeaders
22+
{
23+
$this->values[$key][] = $value;
24+
25+
return $this;
26+
}
27+
2128
public function addFromCode(HttpCode $code): HttpHeaders
2229
{
23-
$template = 'HTTP/1.1 %s';
24-
$this->values['Status'][] = sprintf($template, $code->message());
30+
$this->values['Status'][] = $code->message();
2531

2632
return $this;
2733
}
@@ -33,6 +39,13 @@ public function addFromContentType(Header $header): HttpHeaders
3339
return $this;
3440
}
3541

42+
public function removeFrom(string $key): HttpHeaders
43+
{
44+
unset($this->values[$key]);
45+
46+
return $this;
47+
}
48+
3649
public function getHeader(string $key): array
3750
{
3851
return $this->values[$key] ?? [];

src/Internal/Response.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterf
4040

4141
public function withHeader(string $name, mixed $value): MessageInterface
4242
{
43-
throw new BadMethodCall(method: __METHOD__);
43+
$this->headers->addFrom(key: $name, value: $value);
44+
45+
return $this;
4446
}
4547

4648
public function withoutHeader(string $name): MessageInterface
4749
{
48-
throw new BadMethodCall(method: __METHOD__);
50+
$this->headers->removeFrom(key: $name);
51+
52+
return $this;
4953
}
5054

5155
public function withAddedHeader(string $name, mixed $value): MessageInterface

tests/HttpResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function providerData(): array
150150
private function defaultHeaderFrom(HttpCode $code): array
151151
{
152152
return [
153-
'Status' => [sprintf('HTTP/1.1 %s', $code->message())],
153+
'Status' => [$code->message()],
154154
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
155155
];
156156
}

tests/Internal/ResponseTest.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testDefaultHeaders(): void
1515
{
1616
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
1717
$expected = [
18-
'Status' => [sprintf('HTTP/1.1 %s', HttpCode::OK->message())],
18+
'Status' => [HttpCode::OK->message()],
1919
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
2020
];
2121

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

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

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

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

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

74-
self::expectException(BadMethodCall::class);
75-
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withStatus> cannot be used.');
76-
77-
$response->withStatus(code: HttpCode::OK->value);
77+
self::assertEmpty($response->getHeader(name: 'Status'));
78+
self::assertEquals($expected, $response->getHeader(name: 'Content-Type'));
7879
}
7980

80-
public function testExceptionWhenBadMethodCallOnWithHeader(): void
81+
public function testExceptionWhenBadMethodCallOnWithBody(): void
8182
{
8283
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
8384

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

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

90-
public function testExceptionWhenBadMethodCallOnWithoutHeader(): void
91+
public function testExceptionWhenBadMethodCallOnWithStatus(): void
9192
{
9293
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
9394

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

97-
$response->withoutHeader(name: '');
98+
$response->withStatus(code: HttpCode::OK->value);
9899
}
99100

100101
public function testExceptionWhenBadMethodCallOnWithAddedHeader(): void

0 commit comments

Comments
 (0)