Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Multiple headers of the same type #189

Merged
merged 1 commit into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,19 @@ public function toArray()
{
$headers = [];
/* @var $header Header\HeaderInterface */
foreach ($this->headers as $header) {
foreach ($this->headers as $index => $header) {
if (is_array($header)) {
$header = $this->lazyLoadHeader($index);
}

if ($header instanceof Header\MultipleHeaderInterface) {
$name = $header->getFieldName();
if (! isset($headers[$name])) {
$headers[$name] = [];
}
$headers[$name][] = $header->getFieldValue();
} elseif ($header instanceof Header\HeaderInterface) {
$headers[$header->getFieldName()] = $header->getFieldValue();
} else {
$matches = null;
preg_match('/^(?P<name>[^()><@,;:\"\\/\[\]?=}{ \t]+):\s*(?P<value>.*)$/', $header['line'], $matches);
if ($matches) {
$headers[$matches['name']] = $matches['value'];
}
$headers[$header->getFieldName()] = $header->getFieldValue();
}
}
return $headers;
Expand Down
4 changes: 2 additions & 2 deletions test/Client/TestAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ public static function validHttpResponseProvider()
'HTTP/1.1 404 Not Found' . "\r\n"
. 'Date: Sun, 14 Jun 2009 10:40:06 GMT' . "\r\n"
. 'Server: Apache/2.2.3 (CentOS)' . "\r\n"
. 'Content-length: 281' . "\r\n"
. 'Content-Length: 281' . "\r\n"
. 'Connection: close' . "\r\n"
. 'Content-type: text/html; charset=iso-8859-1' . "\r\n\r\n"
. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n\r\n"
. '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n"
. '<html><head>' . "\n"
. '<title>404 Not Found</title>' . "\n"
Expand Down
116 changes: 116 additions & 0 deletions test/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZendTest\Http;

use ArrayIterator;
use Countable;
use Iterator;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -316,6 +317,121 @@ public function testCRLFAttack()
Headers::fromString("Fake: foo-bar\r\n\r\nevilContent");
}

public function testAddHeaderLineMultipleHeadersGet()
{
$headers = new Headers();
$headers->addHeaderLine('Set-Cookie: cookie1=value1');
$headers->addHeaderLine('Set-Cookie', 'cookie2=value2');

$result = $headers->get('Set-Cookie');
self::assertInstanceOf(ArrayIterator::class, $result);
self::assertCount(2, $result);
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
}

public function testAddHeaderLineMultipleHeadersToString()
{
$headers = new Headers();
$headers->addHeaderLine('Set-Cookie: cookie1=value1');
$headers->addHeaderLine('Set-Cookie', 'cookie2=value2');

self::assertSame(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2' . "\r\n",
$headers->toString()
);
}

public function testAddHeaderMultipleHeadersGet()
{
$headers = new Headers();
$headers->addHeader(new Header\SetCookie('cookie1', 'value1'));
$headers->addHeader(new Header\SetCookie('cookie2', 'value2'));

$result = $headers->get('Set-Cookie');
self::assertInstanceOf(ArrayIterator::class, $result);
self::assertCount(2, $result);
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
}

public function testAddHeaderMultipleHeadersToString()
{
$headers = new Headers();
$headers->addHeader(new Header\SetCookie('cookie1', 'value1'));
$headers->addHeader(new Header\SetCookie('cookie2', 'value2'));

self::assertSame(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2' . "\r\n",
$headers->toString()
);
}

public function testAddHeadersMultipleHeadersGet()
{
$headers = new Headers();
$headers->addHeaders([
new Header\SetCookie('cookie1', 'value1'),
['Set-Cookie', 'cookie2=value2'],
['Set-Cookie' => 'cookie3=value3'],
'Set-Cookie: cookie4=value4',
'Set-Cookie' => 'cookie5=value5',
]);

$result = $headers->get('Set-Cookie');
self::assertInstanceOf(ArrayIterator::class, $result);
self::assertCount(5, $result);
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
}

public function testAddHeadersMultipleHeadersToString()
{
$headers = new Headers();
$headers->addHeaders([
new Header\SetCookie('cookie1', 'value1'),
['Set-Cookie', 'cookie2=value2'],
['Set-Cookie' => 'cookie3=value3'],
'Set-Cookie: cookie4=value4',
'Set-Cookie' => 'cookie5=value5',
]);

self::assertSame(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2' . "\r\n"
. 'Set-Cookie: cookie3=value3' . "\r\n"
. 'Set-Cookie: cookie4=value4' . "\r\n"
. 'Set-Cookie: cookie5=value5' . "\r\n",
$headers->toString()
);
}

public function testFromStringMultipleHeadersGet()
{
$headers = Headers::fromString(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2'
);

$result = $headers->get('Set-Cookie');
self::assertInstanceOf(ArrayIterator::class, $result);
self::assertCount(2, $result);
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
}

public function testFromStringHeadersToString()
{
$headers = Headers::fromString(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2'
);

self::assertSame(
'Set-Cookie: cookie1=value1' . "\r\n"
. 'Set-Cookie: cookie2=value2' . "\r\n",
$headers->toString()
);
}

public function testThrowExceptionOnInvalidHeader()
{
$headers = new Headers();
Expand Down