Skip to content

Commit

Permalink
feat: add error check to getHeaderLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 19, 2023
1 parent d82c850 commit 4c55a28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions system/HTTP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CodeIgniter\HTTP;

use InvalidArgumentException;

/**
* An HTTP message
*
Expand Down Expand Up @@ -112,6 +114,13 @@ public function hasHeader(string $name): bool
*/
public function getHeaderLine(string $name): string
{
if ($this->hasMultipleHeaders($name)) {
throw new InvalidArgumentException(
'The header "' . $name . '" already has multiple headers.'
. ' You cannot use getHeaderLine().'
);
}

$origName = $this->getHeaderName($name);

if (! array_key_exists($origName, $this->headers)) {
Expand Down
19 changes: 19 additions & 0 deletions tests/system/HTTP/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,23 @@ public function testAppendHeaderWithMultipleHeaders(): void

$this->message->appendHeader('Set-Cookie', 'HttpOnly');
}

public function testGetHeaderLineWithMultipleHeaders(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'The header "Set-Cookie" already has multiple headers. You cannot use getHeaderLine().'
);

$this->message->addHeader(
'Set-Cookie',
'logged_in=no; Path=/'
);
$this->message->addHeader(
'Set-Cookie',
'sessid=123456; Path=/'
);

$this->message->getHeaderLine('Set-Cookie');
}
}

0 comments on commit 4c55a28

Please sign in to comment.