Skip to content

Commit 0eaf650

Browse files
committed
Test for new HEAD function inside HttpSocket
1 parent ad4dbdc commit 0eaf650

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

lib/Cake/Network/CakeSocket.php

100755100644
File mode changed.

lib/Cake/Network/Http/HttpSocket.php

100755100644
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,32 @@ public function get($uri = null, $query = array(), $request = array()) {
463463
return $this->request($request);
464464
}
465465

466+
/**
467+
* Issues a HEAD request to the specified URI, query, and request.
468+
*
469+
* By definition HEAD request are identical to GET request except they return no response body. This means that all
470+
* information and examples relevant to GET also applys to HEAD.
471+
*
472+
* @param string|array $uri URI to request. Either a string uri, or a uri array, see HttpSocket::_parseUri()
473+
* @param array $query Querystring parameters to append to URI
474+
* @param array $request An indexed array with indexes such as 'method' or uri
475+
* @return mixed Result of request, either false on failure or the response to the request.
476+
*/
477+
public function head($uri = null, $query = array(), $request = array()) {
478+
if (!empty($query)) {
479+
$uri = $this->_parseUri($uri, $this->config['request']['uri']);
480+
if (isset($uri['query'])) {
481+
$uri['query'] = array_merge($uri['query'], $query);
482+
} else {
483+
$uri['query'] = $query;
484+
}
485+
$uri = $this->_buildUri($uri);
486+
}
487+
488+
$request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri), $request);
489+
return $this->request($request);
490+
}
491+
466492
/**
467493
* Issues a POST request to the specified URI, query, and request.
468494
*
@@ -524,19 +550,6 @@ public function delete($uri = null, $data = array(), $request = array()) {
524550
return $this->request($request);
525551
}
526552

527-
/**
528-
* Issues a HEAD request to the specified URI, query, and request.
529-
*
530-
* @param string|array $uri URI to request (see {@link _parseUri()})
531-
* @param array $data Array of request body data keys and values.
532-
* @param array $request An indexed array with indexes such as 'method' or uri
533-
* @return mixed Result of request
534-
*/
535-
public function head($uri = null, $data = array(), $request = array()) {
536-
$request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri, 'body' => $data), $request);
537-
return $this->request($request);
538-
}
539-
540553
/**
541554
* Normalizes URLs into a $uriTemplate. If no template is provided
542555
* a default one will be used. Will generate the URL using the

lib/Cake/Test/Case/Network/CakeSocketTest.php

100755100644
File mode changed.

lib/Cake/Test/Case/Network/Http/HttpSocketTest.php

100755100644
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,56 @@ public function testGet() {
10491049
));
10501050
}
10511051

1052+
/**
1053+
* testHead method
1054+
*
1055+
* @return void
1056+
*/
1057+
public function testHead() {
1058+
$this->RequestSocket->reset();
1059+
$this->RequestSocket->expects($this->at(0))
1060+
->method('request')
1061+
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/'));
1062+
1063+
$this->RequestSocket->expects($this->at(1))
1064+
->method('request')
1065+
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
1066+
1067+
$this->RequestSocket->expects($this->at(2))
1068+
->method('request')
1069+
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
1070+
1071+
$this->RequestSocket->expects($this->at(3))
1072+
->method('request')
1073+
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
1074+
1075+
$this->RequestSocket->expects($this->at(4))
1076+
->method('request')
1077+
->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
1078+
1079+
$this->RequestSocket->expects($this->at(5))
1080+
->method('request')
1081+
->with(array('method' => 'HEAD', 'uri' => 'https://secure.example.com/test.php?one=two'));
1082+
1083+
$this->RequestSocket->expects($this->at(6))
1084+
->method('request')
1085+
->with(array('method' => 'HEAD', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
1086+
1087+
$this->RequestSocket->head('http://www.google.com/');
1088+
$this->RequestSocket->head('http://www.google.com/', array('foo' => 'bar'));
1089+
$this->RequestSocket->head('http://www.google.com/', 'foo=bar');
1090+
$this->RequestSocket->head('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
1091+
$this->RequestSocket->head('http://www.google.com/', null, array('version' => '1.0'));
1092+
$this->RequestSocket->head('https://secure.example.com/test.php', array('one' => 'two'));
1093+
$this->RequestSocket->head('https://example.com/oauth/access', array(
1094+
'clientid' => '123',
1095+
'redirect_uri' => 'http://example.com',
1096+
'code' => 456
1097+
));
1098+
}
1099+
1100+
1101+
10521102
/**
10531103
* Test authentication
10541104
*

0 commit comments

Comments
 (0)