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

Commit 178e869

Browse files
committed
Merge branch 'hotfix/189' into develop
Forward port #189
2 parents 74d36ca + 94b0a5d commit 178e869

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ All notable changes to this project will be documented in this file, in reverse
5050

5151
- [#188](https://github.com/zendframework/zend-http/pull/188) fixes `Client::setCookies` method to properly handle array of `SetCookie` objects. Per [documentation](https://docs.zendframework.com/zend-http/client/cookies/#usage) it should be allowed.
5252

53+
- [#189](https://github.com/zendframework/zend-http/pull/189) fixes `Headers::toArray` method to properly handle headers of the same type. Behaviour was different depends how header has been attached (`addHeader` or `addHeaderLine` broken before).
54+
5355
## 2.10.0 - 2019-02-19
5456

5557
### Added

src/Headers.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,19 @@ public function toArray()
420420
{
421421
$headers = [];
422422
/* @var $header Header\HeaderInterface */
423-
foreach ($this->headers as $header) {
423+
foreach ($this->headers as $index => $header) {
424+
if (is_array($header)) {
425+
$header = $this->lazyLoadHeader($index);
426+
}
427+
424428
if ($header instanceof Header\MultipleHeaderInterface) {
425429
$name = $header->getFieldName();
426430
if (! isset($headers[$name])) {
427431
$headers[$name] = [];
428432
}
429433
$headers[$name][] = $header->getFieldValue();
430-
} elseif ($header instanceof Header\HeaderInterface) {
431-
$headers[$header->getFieldName()] = $header->getFieldValue();
432434
} else {
433-
$matches = null;
434-
preg_match('/^(?P<name>[^()><@,;:\"\\/\[\]?=}{ \t]+):\s*(?P<value>.*)$/', $header['line'], $matches);
435-
if ($matches) {
436-
$headers[$matches['name']] = $matches['value'];
437-
}
435+
$headers[$header->getFieldName()] = $header->getFieldValue();
438436
}
439437
}
440438
return $headers;

test/Client/TestAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ public static function validHttpResponseProvider()
214214
'HTTP/1.1 404 Not Found' . "\r\n"
215215
. 'Date: Sun, 14 Jun 2009 10:40:06 GMT' . "\r\n"
216216
. 'Server: Apache/2.2.3 (CentOS)' . "\r\n"
217-
. 'Content-length: 281' . "\r\n"
217+
. 'Content-Length: 281' . "\r\n"
218218
. 'Connection: close' . "\r\n"
219-
. 'Content-type: text/html; charset=iso-8859-1' . "\r\n\r\n"
219+
. 'Content-Type: text/html; charset=iso-8859-1' . "\r\n\r\n"
220220
. '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n"
221221
. '<html><head>' . "\n"
222222
. '<title>404 Not Found</title>' . "\n"

test/HeadersTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace ZendTest\Http;
99

10+
use ArrayIterator;
1011
use Countable;
1112
use Iterator;
1213
use PHPUnit\Framework\TestCase;
@@ -316,6 +317,121 @@ public function testCRLFAttack()
316317
Headers::fromString("Fake: foo-bar\r\n\r\nevilContent");
317318
}
318319

320+
public function testAddHeaderLineMultipleHeadersGet()
321+
{
322+
$headers = new Headers();
323+
$headers->addHeaderLine('Set-Cookie: cookie1=value1');
324+
$headers->addHeaderLine('Set-Cookie', 'cookie2=value2');
325+
326+
$result = $headers->get('Set-Cookie');
327+
self::assertInstanceOf(ArrayIterator::class, $result);
328+
self::assertCount(2, $result);
329+
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
330+
}
331+
332+
public function testAddHeaderLineMultipleHeadersToString()
333+
{
334+
$headers = new Headers();
335+
$headers->addHeaderLine('Set-Cookie: cookie1=value1');
336+
$headers->addHeaderLine('Set-Cookie', 'cookie2=value2');
337+
338+
self::assertSame(
339+
'Set-Cookie: cookie1=value1' . "\r\n"
340+
. 'Set-Cookie: cookie2=value2' . "\r\n",
341+
$headers->toString()
342+
);
343+
}
344+
345+
public function testAddHeaderMultipleHeadersGet()
346+
{
347+
$headers = new Headers();
348+
$headers->addHeader(new Header\SetCookie('cookie1', 'value1'));
349+
$headers->addHeader(new Header\SetCookie('cookie2', 'value2'));
350+
351+
$result = $headers->get('Set-Cookie');
352+
self::assertInstanceOf(ArrayIterator::class, $result);
353+
self::assertCount(2, $result);
354+
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
355+
}
356+
357+
public function testAddHeaderMultipleHeadersToString()
358+
{
359+
$headers = new Headers();
360+
$headers->addHeader(new Header\SetCookie('cookie1', 'value1'));
361+
$headers->addHeader(new Header\SetCookie('cookie2', 'value2'));
362+
363+
self::assertSame(
364+
'Set-Cookie: cookie1=value1' . "\r\n"
365+
. 'Set-Cookie: cookie2=value2' . "\r\n",
366+
$headers->toString()
367+
);
368+
}
369+
370+
public function testAddHeadersMultipleHeadersGet()
371+
{
372+
$headers = new Headers();
373+
$headers->addHeaders([
374+
new Header\SetCookie('cookie1', 'value1'),
375+
['Set-Cookie', 'cookie2=value2'],
376+
['Set-Cookie' => 'cookie3=value3'],
377+
'Set-Cookie: cookie4=value4',
378+
'Set-Cookie' => 'cookie5=value5',
379+
]);
380+
381+
$result = $headers->get('Set-Cookie');
382+
self::assertInstanceOf(ArrayIterator::class, $result);
383+
self::assertCount(5, $result);
384+
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
385+
}
386+
387+
public function testAddHeadersMultipleHeadersToString()
388+
{
389+
$headers = new Headers();
390+
$headers->addHeaders([
391+
new Header\SetCookie('cookie1', 'value1'),
392+
['Set-Cookie', 'cookie2=value2'],
393+
['Set-Cookie' => 'cookie3=value3'],
394+
'Set-Cookie: cookie4=value4',
395+
'Set-Cookie' => 'cookie5=value5',
396+
]);
397+
398+
self::assertSame(
399+
'Set-Cookie: cookie1=value1' . "\r\n"
400+
. 'Set-Cookie: cookie2=value2' . "\r\n"
401+
. 'Set-Cookie: cookie3=value3' . "\r\n"
402+
. 'Set-Cookie: cookie4=value4' . "\r\n"
403+
. 'Set-Cookie: cookie5=value5' . "\r\n",
404+
$headers->toString()
405+
);
406+
}
407+
408+
public function testFromStringMultipleHeadersGet()
409+
{
410+
$headers = Headers::fromString(
411+
'Set-Cookie: cookie1=value1' . "\r\n"
412+
. 'Set-Cookie: cookie2=value2'
413+
);
414+
415+
$result = $headers->get('Set-Cookie');
416+
self::assertInstanceOf(ArrayIterator::class, $result);
417+
self::assertCount(2, $result);
418+
self::assertContainsOnlyInstancesOf(Header\SetCookie::class, $result);
419+
}
420+
421+
public function testFromStringHeadersToString()
422+
{
423+
$headers = Headers::fromString(
424+
'Set-Cookie: cookie1=value1' . "\r\n"
425+
. 'Set-Cookie: cookie2=value2'
426+
);
427+
428+
self::assertSame(
429+
'Set-Cookie: cookie1=value1' . "\r\n"
430+
. 'Set-Cookie: cookie2=value2' . "\r\n",
431+
$headers->toString()
432+
);
433+
}
434+
319435
public function testThrowExceptionOnInvalidHeader()
320436
{
321437
$headers = new Headers();

0 commit comments

Comments
 (0)