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