Skip to content

Commit

Permalink
Merge pull request UnionOfRAD#641 from ppadron/dev
Browse files Browse the repository at this point in the history
Return handlers in net\http\Service and support for $path and $data in HEAD requests
  • Loading branch information
nateabele committed Sep 14, 2012
2 parents b27096c + 1ccc1b1 commit 5f3c105
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
25 changes: 21 additions & 4 deletions net/http/Service.php
Expand Up @@ -36,7 +36,14 @@ class Service extends \lithium\core\Object {
*
* @var array
*/
protected $_autoConfig = array('classes' => 'merge');
protected $_autoConfig = array('classes' => 'merge', 'responseTypes');

/**
* Array of closures that return various pieces of information about an HTTP response.
*
* @var array
*/
protected $_responseTypes = array();

/**
* Indicates whether `Service` can connect to the HTTP endpoint for which it is configured.
Expand Down Expand Up @@ -90,6 +97,11 @@ protected function _init() {
} catch(ClassNotFoundException $e) {
$this->connection = null;
}
$this->_responseTypes += array(
'headers' => function($response) { return $response->headers; },
'body' => function($response) { return $response->body(); },
'code' => function($response) { return $response->status['code']; }
);
}

/**
Expand All @@ -111,8 +123,9 @@ public function __call($method, $params = array()) {
* @param array $options
* @return string
*/
public function head(array $options = array()) {
return $this->send(__FUNCTION__, null, array(), $options);
public function head($path = null, $data = array(), array $options = array()) {
$defaults = array('return' => 'headers');
return $this->send(__FUNCTION__, $path, $data, $options + $defaults);
}

/**
Expand Down Expand Up @@ -192,7 +205,11 @@ public function send($method, $path = null, $data = array(), array $options = ar
$this->connection->close();
}
$this->last = (object) compact('request', 'response');
return ($options['return'] == 'body' && $response) ? $response->body() : $response;

$handlers = $this->_responseTypes;
$handler = isset($handlers[$options['return']]) ? $handlers[$options['return']] : null;

return $handler ? $handler($response) : $response;
}

/**
Expand Down
29 changes: 28 additions & 1 deletion tests/cases/net/http/ServiceTest.php
Expand Up @@ -65,9 +65,22 @@ public function testRequestPath() {
$this->assertEqual($expected, $result);
}

public function testReturnHandlers() {
$http = new Service($this->_testConfig);
$result = $http->get(null, null, array('return' => 'headers'));
$this->assertEqual('localhost:80', $result['Host']);

$result = $http->get(null, null, array('return' => 'response'));
$this->assertEqual($result, $http->last->response);

$result = $http->get(null, null, array('return' => 'body'));
$this->assertEqual($result, $http->last->response->body());
}

public function testHead() {
$http = new Service($this->_testConfig);
$this->assertEqual('', $http->head());
$result = $http->head();
$this->assertEqual('localhost:80', $result['Host']);
$this->assertEqual('HTTP/1.1', $http->last->response->protocol);
$this->assertEqual('200', $http->last->response->status['code']);
$this->assertEqual('OK', $http->last->response->status['message']);
Expand All @@ -76,6 +89,20 @@ public function testHead() {
$this->assertEqual('', $http->last->response->body());
}

public function testHeadPath() {
$http = new Service($this->_testConfig);
$expected = '/somewhere';
$result = $http->head('/somewhere');
$this->assertEqual($expected, $http->last->request->path);
}

public function testHeadQueryString() {
$http = new Service($this->_testConfig);
$expected = array('foo' => 'bar');
$result = $http->head('/', $expected);
$this->assertEqual($expected, $http->last->request->query);
}

public function testGet() {
$http = new Service($this->_testConfig);
$this->assertEqual('', $http->get());
Expand Down

0 comments on commit 5f3c105

Please sign in to comment.