Skip to content

Commit 949e95e

Browse files
committed
Http: Improve test coverage
1 parent 66b2f87 commit 949e95e

File tree

2 files changed

+181
-62
lines changed

2 files changed

+181
-62
lines changed

tests/unit/Toolkit/Http/HttpHeadersTest.php renamed to tests/unit/Toolkit/Http/HeadersTest.php

Lines changed: 142 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Salient\Tests\Http;
44

5+
use Psr\Http\Message\MessageInterface as PsrMessageInterface;
56
use Salient\Collection\Collection;
67
use Salient\Contract\Collection\CollectionInterface;
8+
use Salient\Contract\Core\Arrayable;
79
use Salient\Contract\Http\Exception\InvalidHeaderException;
810
use Salient\Contract\Http\HasHttpHeader;
911
use Salient\Contract\Http\HasHttpHeaders;
1012
use Salient\Contract\Http\HasMediaType;
1113
use Salient\Http\OAuth2\AccessToken;
1214
use Salient\Http\Headers;
13-
use Salient\Http\HttpUtil;
1415
use Salient\Tests\TestCase;
1516
use Salient\Utility\Arr;
1617
use ArrayIterator;
@@ -19,9 +20,8 @@
1920

2021
/**
2122
* @covers \Salient\Http\Headers
22-
* @covers \Salient\Http\HttpUtil
2323
*/
24-
final class HttpHeadersTest extends TestCase implements
24+
final class HeadersTest extends TestCase implements
2525
HasHttpHeader,
2626
HasHttpHeaders,
2727
HasMediaType
@@ -53,9 +53,9 @@ public static function constructorProvider(): array
5353
['foo' => 'bar', 'Foo' => 'bar'],
5454
],
5555
[
56-
['host' => ['example.com'], 'qux' => ['quux'], 'foo' => ['bar']],
57-
['qux: quux', 'Foo: bar', 'Host: example.com'],
58-
['qux' => 'quux', 'Foo' => 'bar', 'Host' => 'example.com'],
56+
['host' => ['example.com'], 'qux' => ['quux'], 'foo' => ['bar', 'baz']],
57+
['qux: quux', 'Foo: bar', 'Foo: baz', 'Host: example.com'],
58+
['qux' => 'quux', 'Foo' => ['bar', 'baz'], 'Host' => 'example.com'],
5959
],
6060
[
6161
InvalidArgumentException::class . ',Invalid header name: foo bar',
@@ -261,6 +261,28 @@ public static function addLineProvider(): array
261261
];
262262
}
263263

264+
/**
265+
* @dataProvider addLineWithOtherHeadersProvider
266+
*/
267+
public function testAddLineWithOtherHeaders(Headers $headers): void
268+
{
269+
$this->expectException(LogicException::class);
270+
$this->expectExceptionMessage(Headers::class . '::addLine() cannot be used after headers are applied via another method');
271+
$headers->addLine("foo: bar\r\n");
272+
}
273+
274+
/**
275+
* @return array<array{Headers}>
276+
*/
277+
public static function addLineWithOtherHeadersProvider(): array
278+
{
279+
return [
280+
[new Headers([])],
281+
[new Headers(['foo' => 'bar'])],
282+
[(new Headers())->set('foo', ['bar', 'baz'])],
283+
];
284+
}
285+
264286
public function testHasEmptyLine(): void
265287
{
266288
$headers = new Headers();
@@ -409,42 +431,6 @@ public function testGetHeader(): void
409431
$headers->getOnlyHeaderValue('Qux');
410432
}
411433

412-
public function testGetPreferences(): void
413-
{
414-
$this->assertSame([], HttpUtil::getPreferences(new Headers()));
415-
416-
$headers = (new Headers())
417-
->addValue(self::HEADER_PREFER, 'respond-async, WAIT=5; foo=bar, handling=lenient')
418-
->addValue(self::HEADER_PREFER, 'wait=10; baz=qux')
419-
->addValue(self::HEADER_PREFER, 'task_priority=2; baz="foo bar"')
420-
->addValue(self::HEADER_PREFER, 'odata.maxpagesize=100');
421-
422-
$this->assertSame([
423-
'respond-async' => ['value' => '', 'parameters' => []],
424-
'wait' => ['value' => '5', 'parameters' => ['foo' => 'bar']],
425-
'handling' => ['value' => 'lenient', 'parameters' => []],
426-
'task_priority' => ['value' => '2', 'parameters' => ['baz' => 'foo bar']],
427-
'odata.maxpagesize' => ['value' => '100', 'parameters' => []],
428-
], HttpUtil::getPreferences($headers));
429-
}
430-
431-
public function testMergePreferences(): void
432-
{
433-
$this->assertSame('', HttpUtil::mergePreferences([]));
434-
435-
$this->assertSame(
436-
'respond-async, WAIT=5; foo=bar, handling=lenient, task_priority=2; baz="foo bar", odata.maxpagesize=100',
437-
HttpUtil::mergePreferences([
438-
'respond-async' => '',
439-
'WAIT' => ['value' => '5', 'parameters' => ['foo' => 'bar']],
440-
'wait' => '10',
441-
'handling' => ['value' => 'lenient'],
442-
'task_priority' => ['value' => '2', 'parameters' => ['baz' => 'foo bar']],
443-
'odata.maxpagesize' => ['value' => '100', 'parameters' => []],
444-
]),
445-
);
446-
}
447-
448434
public function testImmutability(): void
449435
{
450436
$a = new Headers();
@@ -488,8 +474,9 @@ public function testImmutability(): void
488474
'task_priority=2',
489475
'odata.maxpagesize=100',
490476
]], false);
491-
$j = $i->unset(self::HEADER_PREFER);
477+
$j = $i->merge(new Headers(), true);
492478
$k = $j->unset(self::HEADER_PREFER);
479+
$l = $k->unset(self::HEADER_PREFER);
493480
$this->assertNotSame($b, $a);
494481
$this->assertNotSame($c, $b);
495482
$this->assertSame($d, $c);
@@ -498,10 +485,11 @@ public function testImmutability(): void
498485
$this->assertSame($g, $e);
499486
$this->assertSame($h, $g);
500487
$this->assertNotSame($i, $h);
501-
$this->assertNotSame($j, $i);
488+
$this->assertSame($j, $i);
489+
$this->assertNotSame($k, $j);
502490
$this->assertCount(1, $g);
503-
$this->assertCount(0, $j);
504-
$this->assertSame($j, $k);
491+
$this->assertCount(0, $k);
492+
$this->assertSame($k, $l);
505493
}
506494

507495
public function testEmptyValue(): void
@@ -514,7 +502,7 @@ public function testEmptyValue(): void
514502

515503
public function testNormalise(): void
516504
{
517-
$headers1 = $this->getHeaders()->set('host', 'example.com');
505+
$headers1 = self::getHeaders()->set('host', 'example.com');
518506
$headers2 = $headers1->normalise();
519507
$this->assertNotSame($headers1, $headers2);
520508
$this->assertSame($headers2, $headers2->normalise());
@@ -547,7 +535,7 @@ public function testNormalise(): void
547535

548536
public function testSort(): void
549537
{
550-
$headers = $this->getHeaders()->set('host', 'example.com')->sort();
538+
$headers = self::getHeaders()->set('host', 'example.com')->sort();
551539
$this->assertSame([
552540
'host' => ['example.com'],
553541
'abc' => ['def'],
@@ -574,7 +562,7 @@ public function testSort(): void
574562

575563
public function testReverse(): void
576564
{
577-
$headers = $this->getHeaders()->set('host', 'example.com')->reverse();
565+
$headers = self::getHeaders()->set('host', 'example.com')->reverse();
578566
$this->assertSame([
579567
'host' => ['example.com'],
580568
'qux' => ['quux'],
@@ -601,7 +589,7 @@ public function testReverse(): void
601589

602590
public function testMap(): void
603591
{
604-
$headers = $this->getHeaders();
592+
$headers = self::getHeaders();
605593
$this->assertSame([], $headers->map(fn() => [])->all());
606594
$this->assertSame([
607595
'foo2' => ['*-2'],
@@ -612,17 +600,7 @@ public function testMap(): void
612600
fn($values) =>
613601
array_map(fn($value) => $value . '-2', $values)
614602
)->all());
615-
}
616-
617-
private function getHeaders(): Headers
618-
{
619-
return new Headers([
620-
'Foo2' => '*',
621-
'foo' => 'bar',
622-
'abc' => 'def',
623-
'Foo' => 'baz',
624-
'qux' => 'quux',
625-
]);
603+
$this->assertSame($headers, $headers->map(fn($values) => $values));
626604
}
627605

628606
public function testFilter(): void
@@ -761,6 +739,98 @@ public static function exceptProvider(): array
761739
];
762740
}
763741

742+
/**
743+
* @dataProvider sliceProvider
744+
*
745+
* @param array<string,string[]> $expected
746+
* @param Arrayable<string,string[]|string>|iterable<string,string[]|string>|PsrMessageInterface|string $headersOrPayload
747+
*/
748+
public function testSlice(array $expected, $headersOrPayload, int $offset, ?int $length = null): void
749+
{
750+
$headers = Headers::from($headersOrPayload);
751+
$this->assertSame($expected, $headers->slice($offset, $length)->all());
752+
}
753+
754+
/**
755+
* @return array<array{array<string,string[]>,Arrayable<string,string[]|string>|iterable<string,string[]|string>|PsrMessageInterface|string,int,3?:int|null}>
756+
*/
757+
public static function sliceProvider(): array
758+
{
759+
$headers = self::getHeaders();
760+
return [
761+
[
762+
['qux' => ['quux']],
763+
$headers,
764+
3,
765+
],
766+
[
767+
['foo' => ['bar', 'baz'], 'abc' => ['def']],
768+
$headers,
769+
1,
770+
2,
771+
],
772+
[
773+
['abc' => ['def'], 'qux' => ['quux']],
774+
$headers,
775+
2,
776+
10,
777+
],
778+
[
779+
[],
780+
$headers,
781+
10,
782+
],
783+
[
784+
[],
785+
$headers,
786+
10,
787+
2,
788+
],
789+
];
790+
}
791+
792+
public function testPop(): void
793+
{
794+
$last = null;
795+
796+
$a = new Headers();
797+
$b = $a->pop($last);
798+
$this->assertSame($b, $a);
799+
$this->assertNull($last);
800+
$this->assertSame([], $a->all());
801+
802+
$a = self::getHeaders();
803+
$b = $a->pop($last);
804+
$this->assertNotSame($b, $a);
805+
$this->assertSame(['quux'], $last);
806+
$this->assertSame([
807+
'foo2' => ['*'],
808+
'foo' => ['bar', 'baz'],
809+
'abc' => ['def'],
810+
], $b->all());
811+
}
812+
813+
public function testShift(): void
814+
{
815+
$first = null;
816+
817+
$a = new Headers();
818+
$b = $a->shift($first);
819+
$this->assertSame($b, $a);
820+
$this->assertNull($first);
821+
$this->assertSame([], $a->all());
822+
823+
$a = self::getHeaders();
824+
$b = $a->shift($first);
825+
$this->assertNotSame($b, $a);
826+
$this->assertSame(['*'], $first);
827+
$this->assertSame([
828+
'foo' => ['bar', 'baz'],
829+
'abc' => ['def'],
830+
'qux' => ['quux'],
831+
], $b->all());
832+
}
833+
764834
public function testOffsetSet(): void
765835
{
766836
$headers = new Headers();
@@ -774,4 +844,15 @@ public function testOffsetUnset(): void
774844
$this->expectException(LogicException::class);
775845
unset($headers[self::HEADER_CONTENT_TYPE]);
776846
}
847+
848+
private static function getHeaders(): Headers
849+
{
850+
return new Headers([
851+
'Foo2' => '*',
852+
'foo' => 'bar',
853+
'abc' => 'def',
854+
'Foo' => 'baz',
855+
'qux' => 'quux',
856+
]);
857+
}
777858
}

tests/unit/Toolkit/Http/HttpUtilTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Psr\Http\Message\UriInterface as PsrUriInterface;
66
use Salient\Contract\Core\DateFormatterInterface;
77
use Salient\Contract\Http\Exception\InvalidHeaderException;
8+
use Salient\Contract\Http\HasHttpHeader;
89
use Salient\Contract\Http\HasMediaType;
910
use Salient\Http\Message\Request;
11+
use Salient\Http\Headers;
1012
use Salient\Http\HttpUtil;
1113
use Salient\Tests\TestCase;
1214
use DateTimeImmutable;
@@ -15,8 +17,44 @@
1517
/**
1618
* @covers \Salient\Http\HttpUtil
1719
*/
18-
final class HttpUtilTest extends TestCase implements HasMediaType
20+
final class HttpUtilTest extends TestCase implements HasHttpHeader, HasMediaType
1921
{
22+
public function testGetPreferences(): void
23+
{
24+
$this->assertSame([], HttpUtil::getPreferences(new Headers()));
25+
26+
$headers = (new Headers())
27+
->addValue(self::HEADER_PREFER, 'respond-async, WAIT=5; foo=bar, handling=lenient')
28+
->addValue(self::HEADER_PREFER, 'wait=10; baz=qux')
29+
->addValue(self::HEADER_PREFER, 'task_priority=2; baz="foo bar"')
30+
->addValue(self::HEADER_PREFER, 'odata.maxpagesize=100');
31+
32+
$this->assertSame([
33+
'respond-async' => ['value' => '', 'parameters' => []],
34+
'wait' => ['value' => '5', 'parameters' => ['foo' => 'bar']],
35+
'handling' => ['value' => 'lenient', 'parameters' => []],
36+
'task_priority' => ['value' => '2', 'parameters' => ['baz' => 'foo bar']],
37+
'odata.maxpagesize' => ['value' => '100', 'parameters' => []],
38+
], HttpUtil::getPreferences($headers));
39+
}
40+
41+
public function testMergePreferences(): void
42+
{
43+
$this->assertSame('', HttpUtil::mergePreferences([]));
44+
45+
$this->assertSame(
46+
'respond-async, WAIT=5; foo=bar, handling=lenient, task_priority=2; baz="foo bar", odata.maxpagesize=100',
47+
HttpUtil::mergePreferences([
48+
'respond-async' => '',
49+
'WAIT' => ['value' => '5', 'parameters' => ['foo' => 'bar']],
50+
'wait' => '10',
51+
'handling' => ['value' => 'lenient'],
52+
'task_priority' => ['value' => '2', 'parameters' => ['baz' => 'foo bar']],
53+
'odata.maxpagesize' => ['value' => '100', 'parameters' => []],
54+
]),
55+
);
56+
}
57+
2058
/**
2159
* @dataProvider isRequestMethodProvider
2260
*/

0 commit comments

Comments
 (0)