Permalink
Browse files

Fixing up proxy support implementation for `\net\http\Request`, and a…

…dding test cases.
  • Loading branch information...
1 parent 7052580 commit d42184ca5aec329c768779853b1a2f8ca5935aaf @nateabele nateabele committed Sep 13, 2012
Showing with 51 additions and 30 deletions.
  1. +26 −28 net/http/Request.php
  2. +25 −2 tests/cases/net/http/RequestTest.php
View
@@ -77,7 +77,9 @@ public function __construct(array $config = array()) {
'body' => null,
'auth' => null,
'method' => 'GET',
- 'proxy' => null
+ 'proxy' => null,
+ 'ignoreErrors' => true,
+ 'followLocation' => true
);
$config += $defaults;
parent::__construct($config);
@@ -99,13 +101,11 @@ public function __construct(array $config = array()) {
*/
protected function _encode($body) {
$media = $this->_classes['media'];
+
if ($type = $media::type($this->_type)) {
$body = $media::encode($this->_type, $body) ?: $body;
}
- if (is_array($body)) {
- $body = join("\r\n", $body);
- }
- return $body;
+ return is_array($body) ? join("\r\n", $body) : $body;
}
/**
@@ -145,31 +145,29 @@ public function queryString($params = array(), $format = null) {
if (!$format) {
$result[] = http_build_query($query);
+ continue;
}
- if ($format) {
- $q = null;
+ $q = null;
- foreach ($params as $key => $value) {
- if (is_array($value)) {
- foreach ($value as $val) {
- $q .= String::insert($format, array(
- 'key' => urlencode("{$key}[]"),
- 'value' => urlencode($val)
- ));
- }
- continue;
- }
+ foreach ($params as $key => $value) {
+ if (!is_array($value)) {
$q .= String::insert($format, array(
'key' => urlencode($key),
'value' => urlencode($value)
));
+ continue;
+ }
+ foreach ($value as $val) {
+ $q .= String::insert($format, array(
+ 'key' => urlencode("{$key}[]"),
+ 'value' => urlencode($val)
+ ));
}
- $result[] = substr($q, 0, -1);
}
-
+ $result[] = substr($q, 0, -1);
}
$result = array_filter($result);
- return (!empty($result)) ? "?" . join("&", $result) : null;
+ return $result ? "?" . join("&", $result) : null;
}
/**
@@ -212,18 +210,16 @@ public function to($format, array $options = array()) {
'username' => $this->username,
'password' => $this->password,
'headers' => array(),
- 'proxy' => $this->proxy,
+ 'proxy' => $this->_config['proxy'],
'body' => null,
'version' => $this->version,
- 'ignore_errors' => isset($this->_config['ignore_errors'])
- ? $this->_config['ignore_errors'] : true,
- 'follow_location' => isset($this->_config['follow_location'])
- ? $this->_config['follow_location'] : true,
- 'request_fulluri' => isset($this->proxy)
+ 'ignore_errors' => $this->_config['ignoreErrors'],
+ 'follow_location' => $this->_config['followLocation'],
+ 'request_fulluri' => (boolean) $this->_config['proxy']
);
$options += $defaults;
- if (!empty($options['auth'])) {
+ if ($options['auth']) {
$data = array();
if (is_array($options['auth']) && !empty($options['auth']['nonce'])) {
@@ -256,7 +252,9 @@ public function to($format, array $options = array()) {
'header' => $this->headers($options['headers']),
'protocol_version' => $options['version'],
'ignore_errors' => $options['ignore_errors'],
- 'follow_location' => $options['follow_location']
+ 'follow_location' => $options['follow_location'],
+ 'request_fulluri' => $options['request_fulluri'],
+ 'proxy' => $options['proxy']
);
return array('http' => array_diff_key($options, $defaults) + $base);
case 'string':
@@ -166,7 +166,9 @@ public function testToContextWithAuth() {
'content' => '',
'protocol_version' => '1.1',
'ignore_errors' => true,
- 'follow_location' => true
+ 'follow_location' => true,
+ 'request_fulluri' => false,
+ 'proxy' => null
));
$this->assertEqual($expected, $request->to('context'));
}
@@ -210,6 +212,25 @@ public function testToArray() {
$this->assertEqual($expected, $result);
}
+ /**
+ * Tests that creating a `Request` with a proxy configuration correctly modifies the results
+ * of exporting the `Request` to a stream context configuration.
+ */
+ public function testWithProxy() {
+ $request = new Request(array('proxy' => 'tcp://proxy.example.com:5100'));
+ $expected = array('http' => array(
+ 'content' => '',
+ 'method' => 'GET',
+ 'header' => array('Host: localhost', 'Connection: Close', 'User-Agent: Mozilla/5.0'),
+ 'protocol_version' => '1.1',
+ 'ignore_errors' => true,
+ 'follow_location' => true,
+ 'request_fulluri' => true,
+ 'proxy' => 'tcp://proxy.example.com:5100'
+ ));
+ $this->assertEqual($expected, $request->to('context'));
+ }
+
public function testToUrl() {
$expected = 'http://localhost/';
$result = $this->request->to('url');
@@ -232,7 +253,9 @@ public function testToContext() {
),
'protocol_version' => '1.1',
'ignore_errors' => true,
- 'follow_location' => true
+ 'follow_location' => true,
+ 'request_fulluri' => false,
+ 'proxy' => null
));
$result = $this->request->to('context');
$this->assertEqual($expected, $result);

0 comments on commit d42184c

Please sign in to comment.