Skip to content

Commit 4c55a28

Browse files
committed
feat: add error check to getHeaderLine()
1 parent d82c850 commit 4c55a28

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

system/HTTP/Message.php

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace CodeIgniter\HTTP;
1313

14+
use InvalidArgumentException;
15+
1416
/**
1517
* An HTTP message
1618
*
@@ -112,6 +114,13 @@ public function hasHeader(string $name): bool
112114
*/
113115
public function getHeaderLine(string $name): string
114116
{
117+
if ($this->hasMultipleHeaders($name)) {
118+
throw new InvalidArgumentException(
119+
'The header "' . $name . '" already has multiple headers.'
120+
. ' You cannot use getHeaderLine().'
121+
);
122+
}
123+
115124
$origName = $this->getHeaderName($name);
116125

117126
if (! array_key_exists($origName, $this->headers)) {

tests/system/HTTP/MessageTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,23 @@ public function testAppendHeaderWithMultipleHeaders(): void
355355

356356
$this->message->appendHeader('Set-Cookie', 'HttpOnly');
357357
}
358+
359+
public function testGetHeaderLineWithMultipleHeaders(): void
360+
{
361+
$this->expectException(InvalidArgumentException::class);
362+
$this->expectExceptionMessage(
363+
'The header "Set-Cookie" already has multiple headers. You cannot use getHeaderLine().'
364+
);
365+
366+
$this->message->addHeader(
367+
'Set-Cookie',
368+
'logged_in=no; Path=/'
369+
);
370+
$this->message->addHeader(
371+
'Set-Cookie',
372+
'sessid=123456; Path=/'
373+
);
374+
375+
$this->message->getHeaderLine('Set-Cookie');
376+
}
358377
}

0 commit comments

Comments
 (0)