Skip to content

Commit 468980b

Browse files
committed
Fix php-curl-class#24: Add support for HEAD and OPTIONS request methods
1 parent 6a69b12 commit 468980b

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/Curl.class.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ public function delete($url, $data = array())
123123
return $this->exec();
124124
}
125125

126+
public function head($url, $data = array())
127+
{
128+
$this->setOpt(CURLOPT_URL, $this->buildURL($url, $data));
129+
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD');
130+
$this->setOpt(CURLOPT_NOBODY, true);
131+
return $this->exec();
132+
}
133+
134+
public function options($url, $data = array())
135+
{
136+
$this->setOpt(CURLOPT_URL, $this->buildURL($url, $data));
137+
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
138+
return $this->exec();
139+
}
140+
126141
public function setBasicAuthentication($username, $password)
127142
{
128143
$this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ public function testDelete() {
192192
)) === 'delete');
193193
}
194194

195+
public function testHeadRequestMethod() {
196+
$test = new Test();
197+
$test->server('request_method', 'HEAD', array(
198+
'key' => 'REQUEST_METHOD',
199+
));
200+
$this->assertEquals($test->curl->response_headers['X-REQUEST-METHOD'], 'HEAD');
201+
$this->assertEmpty($test->curl->response);
202+
}
203+
204+
public function testOptionsRequestMethod() {
205+
$test = new Test();
206+
$test->server('request_method', 'OPTIONS', array(
207+
'key' => 'REQUEST_METHOD',
208+
));
209+
$this->assertEquals($test->curl->response_headers['X-REQUEST-METHOD'], 'OPTIONS');
210+
}
211+
195212
public function testBasicHttpAuth401Unauthorized() {
196213
$test = new Test();
197214
$this->assertTrue($test->server('http_basic_auth', 'GET') === 'canceled');
@@ -638,6 +655,8 @@ public function testRequestMethodSuccessiveGetRequests() {
638655
test($test, 'GET', 'PUT');
639656
test($test, 'GET', 'PATCH');
640657
test($test, 'GET', 'DELETE');
658+
test($test, 'GET', 'HEAD');
659+
test($test, 'GET', 'OPTIONS');
641660
}
642661

643662
public function testRequestMethodSuccessivePostRequests() {
@@ -646,6 +665,8 @@ public function testRequestMethodSuccessivePostRequests() {
646665
test($test, 'POST', 'PUT');
647666
test($test, 'POST', 'PATCH');
648667
test($test, 'POST', 'DELETE');
668+
test($test, 'POST', 'HEAD');
669+
test($test, 'POST', 'OPTIONS');
649670
}
650671

651672
public function testRequestMethodSuccessivePutRequests() {
@@ -654,6 +675,8 @@ public function testRequestMethodSuccessivePutRequests() {
654675
test($test, 'PUT', 'POST');
655676
test($test, 'PUT', 'PATCH');
656677
test($test, 'PUT', 'DELETE');
678+
test($test, 'PUT', 'HEAD');
679+
test($test, 'PUT', 'OPTIONS');
657680
}
658681

659682
public function testRequestMethodSuccessivePatchRequests() {
@@ -662,6 +685,8 @@ public function testRequestMethodSuccessivePatchRequests() {
662685
test($test, 'PATCH', 'POST');
663686
test($test, 'PATCH', 'PUT');
664687
test($test, 'PATCH', 'DELETE');
688+
test($test, 'PATCH', 'HEAD');
689+
test($test, 'PATCH', 'OPTIONS');
665690
}
666691

667692
public function testRequestMethodSuccessiveDeleteRequests() {
@@ -670,5 +695,27 @@ public function testRequestMethodSuccessiveDeleteRequests() {
670695
test($test, 'DELETE', 'POST');
671696
test($test, 'DELETE', 'PUT');
672697
test($test, 'DELETE', 'PATCH');
698+
test($test, 'DELETE', 'HEAD');
699+
test($test, 'DELETE', 'OPTIONS');
700+
}
701+
702+
public function testRequestMethodSuccessiveHeadRequests() {
703+
$test = new Test();
704+
test($test, 'HEAD', 'GET');
705+
test($test, 'HEAD', 'POST');
706+
test($test, 'HEAD', 'PUT');
707+
test($test, 'HEAD', 'PATCH');
708+
test($test, 'HEAD', 'DELETE');
709+
test($test, 'HEAD', 'OPTIONS');
710+
}
711+
712+
public function testRequestMethodSuccessiveOptionsRequests() {
713+
$test = new Test();
714+
test($test, 'OPTIONS', 'GET');
715+
test($test, 'OPTIONS', 'POST');
716+
test($test, 'OPTIONS', 'PUT');
717+
test($test, 'OPTIONS', 'PATCH');
718+
test($test, 'OPTIONS', 'DELETE');
719+
test($test, 'OPTIONS', 'HEAD');
673720
}
674721
}

tests/PHPCurlClass/helper.inc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function server($test, $request_method, $data=array()) {
1919

2020
function test($instance, $before, $after) {
2121
$instance->server('request_method', $before);
22-
PHPUnit_Framework_Assert::assertTrue($instance->curl->response === $before);
22+
PHPUnit_Framework_Assert::assertEquals($instance->curl->response_headers['X-REQUEST-METHOD'], $before);
2323
$instance->server('request_method', $after);
24-
PHPUnit_Framework_Assert::assertTrue($instance->curl->response === $after);
24+
PHPUnit_Framework_Assert::assertEquals($instance->curl->response_headers['X-REQUEST-METHOD'], $after);
2525
}
2626

2727
function create_png() {

tests/PHPCurlClass/server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
exit;
6969
}
7070
else if ($test === 'request_method') {
71+
header('X-REQUEST-METHOD: ' . $request_method);
7172
echo $request_method;
7273
exit;
7374
}

0 commit comments

Comments
 (0)