2
2
3
3
namespace Salient \Tests \Http ;
4
4
5
+ use Psr \Http \Message \MessageInterface as PsrMessageInterface ;
5
6
use Salient \Collection \Collection ;
6
7
use Salient \Contract \Collection \CollectionInterface ;
8
+ use Salient \Contract \Core \Arrayable ;
7
9
use Salient \Contract \Http \Exception \InvalidHeaderException ;
8
10
use Salient \Contract \Http \HasHttpHeader ;
9
11
use Salient \Contract \Http \HasHttpHeaders ;
10
12
use Salient \Contract \Http \HasMediaType ;
11
13
use Salient \Http \OAuth2 \AccessToken ;
12
14
use Salient \Http \Headers ;
13
- use Salient \Http \HttpUtil ;
14
15
use Salient \Tests \TestCase ;
15
16
use Salient \Utility \Arr ;
16
17
use ArrayIterator ;
19
20
20
21
/**
21
22
* @covers \Salient\Http\Headers
22
- * @covers \Salient\Http\HttpUtil
23
23
*/
24
- final class HttpHeadersTest extends TestCase implements
24
+ final class HeadersTest extends TestCase implements
25
25
HasHttpHeader,
26
26
HasHttpHeaders,
27
27
HasMediaType
@@ -53,9 +53,9 @@ public static function constructorProvider(): array
53
53
['foo ' => 'bar ' , 'Foo ' => 'bar ' ],
54
54
],
55
55
[
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 ' ],
59
59
],
60
60
[
61
61
InvalidArgumentException::class . ',Invalid header name: foo bar ' ,
@@ -261,6 +261,28 @@ public static function addLineProvider(): array
261
261
];
262
262
}
263
263
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
+
264
286
public function testHasEmptyLine (): void
265
287
{
266
288
$ headers = new Headers ();
@@ -409,42 +431,6 @@ public function testGetHeader(): void
409
431
$ headers ->getOnlyHeaderValue ('Qux ' );
410
432
}
411
433
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
-
448
434
public function testImmutability (): void
449
435
{
450
436
$ a = new Headers ();
@@ -488,8 +474,9 @@ public function testImmutability(): void
488
474
'task_priority=2 ' ,
489
475
'odata.maxpagesize=100 ' ,
490
476
]], false );
491
- $ j = $ i ->unset ( self :: HEADER_PREFER );
477
+ $ j = $ i ->merge ( new Headers (), true );
492
478
$ k = $ j ->unset (self ::HEADER_PREFER );
479
+ $ l = $ k ->unset (self ::HEADER_PREFER );
493
480
$ this ->assertNotSame ($ b , $ a );
494
481
$ this ->assertNotSame ($ c , $ b );
495
482
$ this ->assertSame ($ d , $ c );
@@ -498,10 +485,11 @@ public function testImmutability(): void
498
485
$ this ->assertSame ($ g , $ e );
499
486
$ this ->assertSame ($ h , $ g );
500
487
$ this ->assertNotSame ($ i , $ h );
501
- $ this ->assertNotSame ($ j , $ i );
488
+ $ this ->assertSame ($ j , $ i );
489
+ $ this ->assertNotSame ($ k , $ j );
502
490
$ this ->assertCount (1 , $ g );
503
- $ this ->assertCount (0 , $ j );
504
- $ this ->assertSame ($ j , $ k );
491
+ $ this ->assertCount (0 , $ k );
492
+ $ this ->assertSame ($ k , $ l );
505
493
}
506
494
507
495
public function testEmptyValue (): void
@@ -514,7 +502,7 @@ public function testEmptyValue(): void
514
502
515
503
public function testNormalise (): void
516
504
{
517
- $ headers1 = $ this -> getHeaders ()->set ('host ' , 'example.com ' );
505
+ $ headers1 = self :: getHeaders ()->set ('host ' , 'example.com ' );
518
506
$ headers2 = $ headers1 ->normalise ();
519
507
$ this ->assertNotSame ($ headers1 , $ headers2 );
520
508
$ this ->assertSame ($ headers2 , $ headers2 ->normalise ());
@@ -547,7 +535,7 @@ public function testNormalise(): void
547
535
548
536
public function testSort (): void
549
537
{
550
- $ headers = $ this -> getHeaders ()->set ('host ' , 'example.com ' )->sort ();
538
+ $ headers = self :: getHeaders ()->set ('host ' , 'example.com ' )->sort ();
551
539
$ this ->assertSame ([
552
540
'host ' => ['example.com ' ],
553
541
'abc ' => ['def ' ],
@@ -574,7 +562,7 @@ public function testSort(): void
574
562
575
563
public function testReverse (): void
576
564
{
577
- $ headers = $ this -> getHeaders ()->set ('host ' , 'example.com ' )->reverse ();
565
+ $ headers = self :: getHeaders ()->set ('host ' , 'example.com ' )->reverse ();
578
566
$ this ->assertSame ([
579
567
'host ' => ['example.com ' ],
580
568
'qux ' => ['quux ' ],
@@ -601,7 +589,7 @@ public function testReverse(): void
601
589
602
590
public function testMap (): void
603
591
{
604
- $ headers = $ this -> getHeaders ();
592
+ $ headers = self :: getHeaders ();
605
593
$ this ->assertSame ([], $ headers ->map (fn () => [])->all ());
606
594
$ this ->assertSame ([
607
595
'foo2 ' => ['*-2 ' ],
@@ -612,17 +600,7 @@ public function testMap(): void
612
600
fn ($ values ) =>
613
601
array_map (fn ($ value ) => $ value . '-2 ' , $ values )
614
602
)->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 ));
626
604
}
627
605
628
606
public function testFilter (): void
@@ -761,6 +739,98 @@ public static function exceptProvider(): array
761
739
];
762
740
}
763
741
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
+
764
834
public function testOffsetSet (): void
765
835
{
766
836
$ headers = new Headers ();
@@ -774,4 +844,15 @@ public function testOffsetUnset(): void
774
844
$ this ->expectException (LogicException::class);
775
845
unset($ headers [self ::HEADER_CONTENT_TYPE ]);
776
846
}
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
+ }
777
858
}
0 commit comments