1010
1111namespace chillerlan \HTTPTest \Psr7 ;
1212
13- use chillerlan \HTTP \Psr7 \{Request , Response , Uri };
13+ use chillerlan \HTTP \Psr17 \{RequestFactory , ResponseFactory , StreamFactory , UriFactory };
14+ use Psr \Http \Message \{RequestFactoryInterface , ResponseFactoryInterface , StreamFactoryInterface , UriFactoryInterface };
1415use PHPUnit \Framework \TestCase ;
1516use Psr \Http \Message \MessageInterface ;
1617use TypeError ;
1718
18- use function chillerlan \HTTP \Psr17 \create_stream ;
1919use function chillerlan \HTTP \Psr7 \{
2020 decompress_content , get_json , get_xml , message_to_string , r_rawurlencode ,
2121 uriIsAbsolute , uriIsAbsolutePathReference , uriIsNetworkPathReference ,
2525
2626class MessageHelpersTest extends TestCase{
2727
28+ protected RequestFactoryInterface $ requestFactory ;
29+ protected ResponseFactoryInterface $ responseFactory ;
30+ protected StreamFactoryInterface $ streamFactory ;
31+ protected UriFactoryInterface $ uriFactory ;
32+
33+ protected function setUp ():void {
34+ $ this ->requestFactory = new RequestFactory ;
35+ $ this ->responseFactory = new ResponseFactory ;
36+ $ this ->streamFactory = new StreamFactory ;
37+ $ this ->uriFactory = new UriFactory ;
38+ }
39+
2840 public function rawurlencodeDataProvider ():array {
2941 return [
3042 'null ' => [null , '' ],
@@ -59,7 +71,7 @@ public function testRawurlencodeTypeErrorException():void{
5971
6072 public function testGetJSON ():void {
6173
62- $ r = ( new Response )->withBody (create_stream ('{"foo":"bar"} ' ));
74+ $ r = $ this -> responseFactory -> createResponse ( )->withBody ($ this -> streamFactory -> createStream ('{"foo":"bar"} ' ));
6375
6476 $ this ::assertSame ('bar ' , get_json ($ r )->foo );
6577
@@ -70,7 +82,9 @@ public function testGetJSON():void{
7082
7183 public function testGetXML ():void {
7284
73- $ r = (new Response )->withBody (create_stream ('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root> ' ));
85+ $ r = $ this ->responseFactory
86+ ->createResponse ()
87+ ->withBody ($ this ->streamFactory ->createStream ('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root> ' ));
7488
7589 $ this ::assertSame ('bar ' , get_xml ($ r )->foo ->__toString ());
7690
@@ -79,23 +93,29 @@ public function testGetXML():void{
7993 $ this ::assertSame ('bar ' , get_xml ($ r , true )['foo ' ]);
8094 }
8195
82- public function messageDataProvider ():array {
83- return [
84- 'Request ' => [new Request ('GET ' , 'https://localhost/foo ' ), 'GET /foo HTTP/1.1 ' ."\r\n" .'Host: localhost ' ."\r\n" .'foo: bar ' ."\r\n\r\n" .'testbody ' ],
85- 'Response ' => [new Response , 'HTTP/1.1 200 OK ' ."\r\n" .'foo: bar ' ."\r\n\r\n" .'testbody ' ],
86- ];
87- }
96+ public function testMessageToString ():void {
97+ $ body = $ this ->streamFactory ->createStream ('testbody ' );
98+
99+ $ request = $ this ->requestFactory
100+ ->createRequest ('GET ' , 'https://localhost/foo ' )
101+ ->withAddedHeader ('foo ' , 'bar ' )
102+ ->withBody ($ body )
103+ ;
104+
105+ $ this ::assertSame (
106+ 'GET /foo HTTP/1.1 ' ."\r\n" .'Host: localhost ' ."\r\n" .'foo: bar ' ."\r\n\r\n" .'testbody ' ,
107+ message_to_string ($ request )
108+ );
109+
110+ $ response = $ this ->responseFactory
111+ ->createResponse ()
112+ ->withAddedHeader ('foo ' , 'bar ' )
113+ ->withBody ($ body )
114+ ;
88115
89- /**
90- * @dataProvider messageDataProvider
91- *
92- * @param \Psr\Http\Message\MessageInterface $message
93- * @param string $expected
94- */
95- public function testMessageToString (MessageInterface $ message , string $ expected ):void {
96116 $ this ::assertSame (
97- $ expected ,
98- message_to_string ($ message -> withAddedHeader ( ' foo ' , ' bar ' )-> withBody ( create_stream ( ' testbody ' )) )
117+ ' HTTP/1.1 200 OK ' . "\r\n" . ' foo: bar ' . "\r\n\r\n" . ' testbody ' ,
118+ message_to_string ($ response )
99119 );
100120 }
101121
@@ -113,50 +133,50 @@ public function decompressDataProvider():array{
113133 */
114134 public function testDecompressContent (string $ fn , string $ encoding ):void {
115135 $ data = $ expected = str_repeat ('compressed string ' , 100 );
116- $ response = ( new Response );
136+ $ response = $ this -> responseFactory -> createResponse ( );
117137
118138 if ($ fn ){
119139 $ data = $ fn ($ data );
120140 $ response = $ response ->withHeader ('Content-Encoding ' , $ encoding );
121141 }
122142
123- $ response = $ response ->withBody (create_stream ($ data ));
143+ $ response = $ response ->withBody ($ this -> streamFactory -> createStream ($ data ));
124144
125145 $ this ::assertSame ($ expected , decompress_content ($ response ));
126146 }
127147
128148 public function testUriIsAbsolute ():void {
129- $ this ::assertTrue (uriIsAbsolute (new Uri ('http://example.org ' )));
130- $ this ::assertFalse (uriIsAbsolute (new Uri ('//example.org ' )));
131- $ this ::assertFalse (uriIsAbsolute (new Uri ('/abs-path ' )));
132- $ this ::assertFalse (uriIsAbsolute (new Uri ('rel-path ' )));
149+ $ this ::assertTrue (uriIsAbsolute ($ this -> uriFactory -> createUri ('http://example.org ' )));
150+ $ this ::assertFalse (uriIsAbsolute ($ this -> uriFactory -> createUri ('//example.org ' )));
151+ $ this ::assertFalse (uriIsAbsolute ($ this -> uriFactory -> createUri ('/abs-path ' )));
152+ $ this ::assertFalse (uriIsAbsolute ($ this -> uriFactory -> createUri ('rel-path ' )));
133153 }
134154
135155 public function testUriIsNetworkPathReference ():void {
136- $ this ::assertFalse (uriIsNetworkPathReference (new Uri ('http://example.org ' )));
137- $ this ::assertTrue (uriIsNetworkPathReference (new Uri ('//example.org ' )));
138- $ this ::assertFalse (uriIsNetworkPathReference (new Uri ('/abs-path ' )));
139- $ this ::assertFalse (uriIsNetworkPathReference (new Uri ('rel-path ' )));
156+ $ this ::assertFalse (uriIsNetworkPathReference ($ this -> uriFactory -> createUri ('http://example.org ' )));
157+ $ this ::assertTrue (uriIsNetworkPathReference ($ this -> uriFactory -> createUri ('//example.org ' )));
158+ $ this ::assertFalse (uriIsNetworkPathReference ($ this -> uriFactory -> createUri ('/abs-path ' )));
159+ $ this ::assertFalse (uriIsNetworkPathReference ($ this -> uriFactory -> createUri ('rel-path ' )));
140160 }
141161
142162 public function testUriIsAbsolutePathReference ():void {
143- $ this ::assertFalse (uriIsAbsolutePathReference (new Uri ('http://example.org ' )));
144- $ this ::assertFalse (uriIsAbsolutePathReference (new Uri ('//example.org ' )));
145- $ this ::assertTrue (uriIsAbsolutePathReference (new Uri ('/abs-path ' )));
146- $ this ::assertTrue (uriIsAbsolutePathReference (new Uri ('/ ' )));
147- $ this ::assertFalse (uriIsAbsolutePathReference (new Uri ('rel-path ' )));
163+ $ this ::assertFalse (uriIsAbsolutePathReference ($ this -> uriFactory -> createUri ('http://example.org ' )));
164+ $ this ::assertFalse (uriIsAbsolutePathReference ($ this -> uriFactory -> createUri ('//example.org ' )));
165+ $ this ::assertTrue (uriIsAbsolutePathReference ($ this -> uriFactory -> createUri ('/abs-path ' )));
166+ $ this ::assertTrue (uriIsAbsolutePathReference ($ this -> uriFactory -> createUri ('/ ' )));
167+ $ this ::assertFalse (uriIsAbsolutePathReference ($ this -> uriFactory -> createUri ('rel-path ' )));
148168 }
149169
150170 public function testUriIsRelativePathReference ():void {
151- $ this ::assertFalse (uriIsRelativePathReference (new Uri ('http://example.org ' )));
152- $ this ::assertFalse (uriIsRelativePathReference (new Uri ('//example.org ' )));
153- $ this ::assertFalse (uriIsRelativePathReference (new Uri ('/abs-path ' )));
154- $ this ::assertTrue (uriIsRelativePathReference (new Uri ('rel-path ' )));
155- $ this ::assertTrue (uriIsRelativePathReference (new Uri ('' )));
171+ $ this ::assertFalse (uriIsRelativePathReference ($ this -> uriFactory -> createUri ('http://example.org ' )));
172+ $ this ::assertFalse (uriIsRelativePathReference ($ this -> uriFactory -> createUri ('//example.org ' )));
173+ $ this ::assertFalse (uriIsRelativePathReference ($ this -> uriFactory -> createUri ('/abs-path ' )));
174+ $ this ::assertTrue (uriIsRelativePathReference ($ this -> uriFactory -> createUri ('rel-path ' )));
175+ $ this ::assertTrue (uriIsRelativePathReference ($ this -> uriFactory -> createUri ('' )));
156176 }
157177
158178 public function testUriAddAndRemoveQueryValues ():void {
159- $ uri = new Uri ;
179+ $ uri = $ this -> uriFactory -> createUri () ;
160180
161181 $ uri = uriWithQueryValue ($ uri , 'a ' , 'b ' );
162182 $ uri = uriWithQueryValue ($ uri , 'c ' , 'd ' );
@@ -172,7 +192,7 @@ public function testUriAddAndRemoveQueryValues():void{
172192 }
173193
174194 public function testUriWithQueryValueReplacesSameKeys ():void {
175- $ uri = new Uri ;
195+ $ uri = $ this -> uriFactory -> createUri () ;
176196
177197 $ uri = uriWithQueryValue ($ uri , 'a ' , 'b ' );
178198 $ uri = uriWithQueryValue ($ uri , 'c ' , 'd ' );
@@ -181,37 +201,37 @@ public function testUriWithQueryValueReplacesSameKeys():void{
181201 }
182202
183203 public function testUriWithoutQueryValueRemovesAllSameKeys ():void {
184- $ uri = ( new Uri )->withQuery ('a=b&c=d&a=e ' );
204+ $ uri = $ this -> uriFactory -> createUri ( )->withQuery ('a=b&c=d&a=e ' );
185205
186206 $ uri = uriWithoutQueryValue ($ uri , 'a ' );
187207 $ this ::assertSame ('c=d ' , $ uri ->getQuery ());
188208 }
189209
190210 public function testUriRemoveNonExistingQueryValue ():void {
191- $ uri = new Uri ;
211+ $ uri = $ this -> uriFactory -> createUri () ;
192212 $ uri = uriWithQueryValue ($ uri , 'a ' , 'b ' );
193213 $ uri = uriWithoutQueryValue ($ uri , 'c ' );
194214 $ this ::assertSame ('a=b ' , $ uri ->getQuery ());
195215 }
196216
197217 public function testUriWithQueryValueHandlesEncoding ():void {
198- $ uri = new Uri ;
218+ $ uri = $ this -> uriFactory -> createUri () ;
199219 $ uri = uriWithQueryValue ($ uri , 'E=mc^2 ' , 'ein&stein ' );
200220 $ this ::assertSame ('E%3Dmc%5E2=ein%26stein ' , $ uri ->getQuery (), 'Decoded key/value get encoded ' );
201221
202- $ uri = new Uri ;
222+ $ uri = $ this -> uriFactory -> createUri () ;
203223 $ uri = uriWithQueryValue ($ uri , 'E%3Dmc%5e2 ' , 'ein%26stein ' );
204224 $ this ::assertSame ('E%3Dmc%5e2=ein%26stein ' , $ uri ->getQuery (), 'Encoded key/value do not get double-encoded ' );
205225 }
206226
207227 public function testUriWithoutQueryValueHandlesEncoding ():void {
208228 // It also tests that the case of the percent-encoding does not matter,
209229 // i.e. both lowercase "%3d" and uppercase "%5E" can be removed.
210- $ uri = ( new Uri )->withQuery ('E%3dmc%5E2=einstein&foo=bar ' );
230+ $ uri = $ this -> uriFactory -> createUri ( )->withQuery ('E%3dmc%5E2=einstein&foo=bar ' );
211231 $ uri = uriWithoutQueryValue ($ uri , 'E=mc^2 ' );
212232 $ this ::assertSame ('foo=bar ' , $ uri ->getQuery (), 'Handles key in decoded form ' );
213233
214- $ uri = ( new Uri )->withQuery ('E%3dmc%5E2=einstein&foo=bar ' );
234+ $ uri = $ this -> uriFactory -> createUri ( )->withQuery ('E%3dmc%5E2=einstein&foo=bar ' );
215235 $ uri = uriWithoutQueryValue ($ uri , 'E%3Dmc%5e2 ' );
216236 $ this ::assertSame ('foo=bar ' , $ uri ->getQuery (), 'Handles key in encoded form ' );
217237
0 commit comments