@@ -574,14 +574,16 @@ class Tester
574
574
* @param array $headers
575
575
* @param string|null $uri
576
576
* @param string|null $scriptFilename
577
+ * @param string|null $stdin
577
578
*
578
579
* @return array
579
580
*/
580
581
private function getRequestParams (
581
582
string $ query = '' ,
582
583
array $ headers = [],
583
584
string $ uri = null ,
584
- string $ scriptFilename = null
585
+ string $ scriptFilename = null ,
586
+ ?string $ stdin = null
585
587
): array {
586
588
if (is_null ($ uri )) {
587
589
$ uri = $ this ->makeSourceFile ();
@@ -590,7 +592,7 @@ class Tester
590
592
$ params = array_merge (
591
593
[
592
594
'GATEWAY_INTERFACE ' => 'FastCGI/1.0 ' ,
593
- 'REQUEST_METHOD ' => 'GET ' ,
595
+ 'REQUEST_METHOD ' => is_null ( $ stdin ) ? 'GET ' : ' POST ' ,
594
596
'SCRIPT_FILENAME ' => $ scriptFilename ?: $ uri ,
595
597
'SCRIPT_NAME ' => $ uri ,
596
598
'QUERY_STRING ' => $ query ,
@@ -605,7 +607,7 @@ class Tester
605
607
'SERVER_PROTOCOL ' => 'HTTP/1.1 ' ,
606
608
'DOCUMENT_ROOT ' => __DIR__ ,
607
609
'CONTENT_TYPE ' => '' ,
608
- 'CONTENT_LENGTH ' => 0
610
+ 'CONTENT_LENGTH ' => strlen ( $ stdin ?? "" ) // Default to 0
609
611
],
610
612
$ headers
611
613
);
@@ -615,21 +617,86 @@ class Tester
615
617
});
616
618
}
617
619
620
+ /**
621
+ * Parse stdin and generate data for multipart config.
622
+ *
623
+ * @param array $stdin
624
+ * @param array $headers
625
+ *
626
+ * @return void
627
+ * @throws \Exception
628
+ */
629
+ private function parseStdin (array $ stdin , array &$ headers )
630
+ {
631
+ $ parts = $ stdin ['parts ' ] ?? null ;
632
+ if (empty ($ parts )) {
633
+ throw new \Exception ('The stdin array needs to contain parts ' );
634
+ }
635
+ $ boundary = $ stdin ['boundary ' ] ?? 'AaB03x ' ;
636
+ if ( ! isset ($ headers ['CONTENT_TYPE ' ])) {
637
+ $ headers ['CONTENT_TYPE ' ] = 'multipart/form-data; boundary= ' . $ boundary ;
638
+ }
639
+ $ count = $ parts ['count ' ] ?? null ;
640
+ if ( ! is_null ($ count )) {
641
+ $ dispositionType = $ parts ['disposition ' ] ?? 'form-data ' ;
642
+ $ dispositionParam = $ parts ['param ' ] ?? 'name ' ;
643
+ $ namePrefix = $ parts ['prefix ' ] ?? 'f ' ;
644
+ $ nameSuffix = $ parts ['suffix ' ] ?? '' ;
645
+ $ value = $ parts ['value ' ] ?? 'test ' ;
646
+ $ parts = [];
647
+ for ($ i = 0 ; $ i < $ count ; $ i ++) {
648
+ $ parts [] = [
649
+ 'disposition ' => $ dispositionType ,
650
+ 'param ' => $ dispositionParam ,
651
+ 'name ' => "$ namePrefix$ i$ nameSuffix " ,
652
+ 'value ' => $ value
653
+ ];
654
+ }
655
+ }
656
+ $ out = '' ;
657
+ $ nl = "\r\n" ;
658
+ foreach ($ parts as $ part ) {
659
+ if (!is_array ($ part )) {
660
+ $ part = ['name ' => $ part ];
661
+ } elseif ( ! isset ($ part ['name ' ])) {
662
+ throw new \Exception ('Each part has to have a name ' );
663
+ }
664
+ $ name = $ part ['name ' ];
665
+ $ dispositionType = $ part ['disposition ' ] ?? 'form-data ' ;
666
+ $ dispositionParam = $ part ['param ' ] ?? 'name ' ;
667
+ $ value = $ part ['value ' ] ?? 'test ' ;
668
+ $ partHeaders = $ part ['headers ' ] ?? [];
669
+
670
+ $ out .= "-- $ boundary$ nl " ;
671
+ $ out .= "Content-disposition: $ dispositionType; $ dispositionParam= \"$ name \"$ nl " ;
672
+ foreach ($ partHeaders as $ headerName => $ headerValue ) {
673
+ $ out .= "$ headerName: $ headerValue$ nl " ;
674
+ }
675
+ $ out .= $ nl ;
676
+ $ out .= "$ value$ nl " ;
677
+ }
678
+ $ out .= "-- $ boundary-- $ nl " ;
679
+
680
+ return $ out ;
681
+ }
682
+
618
683
/**
619
684
* Execute request.
620
685
*
621
- * @param string $query
622
- * @param array $headers
623
- * @param string|null $uri
624
- * @param string|null $address
625
- * @param string|null $successMessage
626
- * @param string|null $errorMessagereadLimit
627
- * @param bool $connKeepAlive
628
- * @param string|null $scriptFilename = null
629
- * @param bool $expectError
630
- * @param int $readLimit
686
+ * @param string $query
687
+ * @param array $headers
688
+ * @param string|null $uri
689
+ * @param string|null $address
690
+ * @param string|null $successMessage
691
+ * @param string|null $errorMessage
692
+ * @param bool $connKeepAlive
693
+ * @param string|null $scriptFilename = null
694
+ * @param string|array|null $stdin = null
695
+ * @param bool $expectError
696
+ * @param int $readLimit
631
697
*
632
698
* @return Response
699
+ * @throws \Exception
633
700
*/
634
701
public function request (
635
702
string $ query = '' ,
@@ -640,19 +707,24 @@ class Tester
640
707
string $ errorMessage = null ,
641
708
bool $ connKeepAlive = false ,
642
709
string $ scriptFilename = null ,
710
+ string |array $ stdin = null ,
643
711
bool $ expectError = false ,
644
712
int $ readLimit = -1 ,
645
713
): Response {
646
714
if ($ this ->hasError ()) {
647
715
return new Response (null , true );
648
716
}
649
717
650
- $ params = $ this ->getRequestParams ($ query , $ headers , $ uri , $ scriptFilename );
718
+ if (is_array ($ stdin )) {
719
+ $ stdin = $ this ->parseStdin ($ stdin , $ headers );
720
+ }
721
+
722
+ $ params = $ this ->getRequestParams ($ query , $ headers , $ uri , $ scriptFilename , $ stdin );
651
723
$ this ->trace ('Request params ' , $ params );
652
724
653
725
try {
654
726
$ this ->response = new Response (
655
- $ this ->getClient ($ address , $ connKeepAlive )->request_data ($ params , false , $ readLimit )
727
+ $ this ->getClient ($ address , $ connKeepAlive )->request_data ($ params , $ stdin , $ readLimit )
656
728
);
657
729
if ($ expectError ) {
658
730
$ this ->error ('Expected request error but the request was successful ' );
0 commit comments