File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -21,13 +21,15 @@ private function mergeDefaultheaders(array $headers)
2121 {
2222 $ port = ($ this ->getDefaultPort () === $ this ->getPort ()) ? '' : ": {$ this ->getPort ()}" ;
2323 $ connectionHeaders = ('1.1 ' === $ this ->protocolVersion ) ? array ('Connection ' => 'close ' ) : array ();
24+ $ authHeaders = $ this ->getAuthHeaders ();
2425
2526 return array_merge (
2627 array (
2728 'Host ' => $ this ->getHost ().$ port ,
2829 'User-Agent ' => 'React/alpha ' ,
2930 ),
3031 $ connectionHeaders ,
32+ $ authHeaders ,
3133 $ headers
3234 );
3335 }
@@ -78,4 +80,27 @@ public function __toString()
7880
7981 return $ data ;
8082 }
83+
84+ private function getUrlUserPass ()
85+ {
86+ $ components = parse_url ($ this ->url );
87+
88+ if (isset ($ components ['user ' ])) {
89+ return array (
90+ 'user ' => $ components ['user ' ],
91+ 'pass ' => isset ($ components ['pass ' ]) ? $ components ['pass ' ] : null ,
92+ );
93+ }
94+ }
95+
96+ private function getAuthHeaders ()
97+ {
98+ if (null !== $ auth = $ this ->getUrlUserPass ()) {
99+ return array (
100+ 'Authorization ' => 'Basic ' . base64_encode ($ auth ['user ' ].': ' .$ auth ['pass ' ]),
101+ );
102+ }
103+
104+ return array ();
105+ }
81106}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace React \Tests \HttpClient ;
4+
5+ use React \HttpClient \RequestData ;
6+
7+ class RequestDataTest extends TestCase
8+ {
9+ /** @test */
10+ public function toStringReturnsHTTPRequestMessage ()
11+ {
12+ $ requestData = new RequestData ('GET ' , 'http://www.example.com ' );
13+
14+ $ expected = "GET / HTTP/1.1 \r\n" .
15+ "Host: www.example.com \r\n" .
16+ "User-Agent: React/alpha \r\n" .
17+ "Connection: close \r\n" .
18+ "\r\n" ;
19+
20+ $ this ->assertSame ($ expected , $ requestData ->__toString ());
21+ }
22+
23+ /** @test */
24+ public function toStringUsesUserPassFromURL ()
25+ {
26+ $ requestData =
new RequestData (
'GET ' ,
'http://john:[email protected] ' );
27+
28+ $ expected = "GET / HTTP/1.1 \r\n" .
29+ "Host: www.example.com \r\n" .
30+ "User-Agent: React/alpha \r\n" .
31+ "Connection: close \r\n" .
32+ "Authorization: Basic am9objpkdW1teQ== \r\n" .
33+ "\r\n" ;
34+
35+ $ this ->assertSame ($ expected , $ requestData ->__toString ());
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments