Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementing return handlers in net\http\Service and adding support f…

…or $path and $data to HEAD requests. Closes #640.
  • Loading branch information...
commit 1ccc1b15fb8384063deeb7f44d77a6548d600ffb 1 parent b27096c
@ppadron ppadron authored
Showing with 49 additions and 5 deletions.
  1. +21 −4 net/http/Service.php
  2. +28 −1 tests/cases/net/http/ServiceTest.php
View
25 net/http/Service.php
@@ -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.
@@ -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']; }
+ );
}
/**
@@ -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);
}
/**
@@ -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;
}
/**
View
29 tests/cases/net/http/ServiceTest.php
@@ -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']);
@@ -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());
Please sign in to comment.
Something went wrong with that request. Please try again.