Skip to content

Commit

Permalink
Fixing up proxy support implementation for \net\http\Request, and a…
Browse files Browse the repository at this point in the history
…dding test cases.
  • Loading branch information
nateabele committed Sep 18, 2012
1 parent 7052580 commit d42184c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
54 changes: 26 additions & 28 deletions net/http/Request.php
Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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':
Expand Down
27 changes: 25 additions & 2 deletions tests/cases/net/http/RequestTest.php
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit d42184c

Please sign in to comment.