From 0eaf650d9f1d4e79c4cc46436db6c9d04a162c53 Mon Sep 17 00:00:00 2001 From: Melvin Ross Date: Mon, 14 Jul 2014 14:34:27 -0500 Subject: [PATCH] Test for new HEAD function inside HttpSocket --- lib/Cake/Network/CakeSocket.php | 0 lib/Cake/Network/Http/HttpSocket.php | 39 ++++++++++----- lib/Cake/Test/Case/Network/CakeSocketTest.php | 0 .../Test/Case/Network/Http/HttpSocketTest.php | 50 +++++++++++++++++++ 4 files changed, 76 insertions(+), 13 deletions(-) mode change 100755 => 100644 lib/Cake/Network/CakeSocket.php mode change 100755 => 100644 lib/Cake/Network/Http/HttpSocket.php mode change 100755 => 100644 lib/Cake/Test/Case/Network/CakeSocketTest.php mode change 100755 => 100644 lib/Cake/Test/Case/Network/Http/HttpSocketTest.php diff --git a/lib/Cake/Network/CakeSocket.php b/lib/Cake/Network/CakeSocket.php old mode 100755 new mode 100644 diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php old mode 100755 new mode 100644 index cdbc1055757..ed33b65ccc4 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -463,6 +463,32 @@ public function get($uri = null, $query = array(), $request = array()) { return $this->request($request); } +/** + * Issues a HEAD request to the specified URI, query, and request. + * + * By definition HEAD request are identical to GET request except they return no response body. This means that all + * information and examples relevant to GET also applys to HEAD. + * + * @param string|array $uri URI to request. Either a string uri, or a uri array, see HttpSocket::_parseUri() + * @param array $query Querystring parameters to append to URI + * @param array $request An indexed array with indexes such as 'method' or uri + * @return mixed Result of request, either false on failure or the response to the request. + */ + public function head($uri = null, $query = array(), $request = array()) { + if (!empty($query)) { + $uri = $this->_parseUri($uri, $this->config['request']['uri']); + if (isset($uri['query'])) { + $uri['query'] = array_merge($uri['query'], $query); + } else { + $uri['query'] = $query; + } + $uri = $this->_buildUri($uri); + } + + $request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri), $request); + return $this->request($request); + } + /** * Issues a POST request to the specified URI, query, and request. * @@ -524,19 +550,6 @@ public function delete($uri = null, $data = array(), $request = array()) { return $this->request($request); } -/** - * Issues a HEAD request to the specified URI, query, and request. - * - * @param string|array $uri URI to request (see {@link _parseUri()}) - * @param array $data Array of request body data keys and values. - * @param array $request An indexed array with indexes such as 'method' or uri - * @return mixed Result of request - */ - public function head($uri = null, $data = array(), $request = array()) { - $request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri, 'body' => $data), $request); - return $this->request($request); - } - /** * Normalizes URLs into a $uriTemplate. If no template is provided * a default one will be used. Will generate the URL using the diff --git a/lib/Cake/Test/Case/Network/CakeSocketTest.php b/lib/Cake/Test/Case/Network/CakeSocketTest.php old mode 100755 new mode 100644 diff --git a/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php b/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php old mode 100755 new mode 100644 index ac506942f2d..ac57ab686ac --- a/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php +++ b/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php @@ -1049,6 +1049,56 @@ public function testGet() { )); } +/** + * testHead method + * + * @return void + */ + public function testHead() { + $this->RequestSocket->reset(); + $this->RequestSocket->expects($this->at(0)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/')); + + $this->RequestSocket->expects($this->at(1)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar')); + + $this->RequestSocket->expects($this->at(2)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar')); + + $this->RequestSocket->expects($this->at(3)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=23&foobar=42')); + + $this->RequestSocket->expects($this->at(4)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/', 'version' => '1.0')); + + $this->RequestSocket->expects($this->at(5)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'https://secure.example.com/test.php?one=two')); + + $this->RequestSocket->expects($this->at(6)) + ->method('request') + ->with(array('method' => 'HEAD', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456')); + + $this->RequestSocket->head('http://www.google.com/'); + $this->RequestSocket->head('http://www.google.com/', array('foo' => 'bar')); + $this->RequestSocket->head('http://www.google.com/', 'foo=bar'); + $this->RequestSocket->head('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23')); + $this->RequestSocket->head('http://www.google.com/', null, array('version' => '1.0')); + $this->RequestSocket->head('https://secure.example.com/test.php', array('one' => 'two')); + $this->RequestSocket->head('https://example.com/oauth/access', array( + 'clientid' => '123', + 'redirect_uri' => 'http://example.com', + 'code' => 456 + )); + } + + + /** * Test authentication *