Skip to content

Commit

Permalink
Add DELETE method, fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccue committed Feb 8, 2012
1 parent c1f049b commit 329432f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
14 changes: 14 additions & 0 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class Requests {
*/
const HEAD = 'HEAD';

/**
* DELETE method
*
* @var string
*/
const DELETE = 'DELETE';

/**
* Current version of Requests
*
Expand Down Expand Up @@ -185,6 +192,13 @@ public static function get($url, $headers = array(), $options = array()) {
public static function head($url, $headers = array(), $options = array()) {
return self::request($url, $headers, null, self::HEAD, $options);
}

/**
* Send a DELETE request
*/
public static function delete($url, $headers = array(), $options = array()) {
return self::request($url, $headers, null, self::DELETE, $options);
}
/**#@-*/

/**#@+
Expand Down
5 changes: 4 additions & 1 deletion library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar
$options['hooks']->dispatch('curl.before_request', array(&$this->fp));

$headers = Requests::flattern($headers);
if (($options['type'] === Requests::HEAD || $options['type'] === Requests::GET) & !empty($data)) {
if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
$url = self::format_get($url, $data);
}

Expand All @@ -93,6 +93,9 @@ public function request($url, $headers = array(), $data = array(), $options = ar
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
break;
case Requests::DELETE:
curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case Requests::HEAD:
curl_setopt($this->fp, CURLOPT_NOBODY, true);
break;
Expand Down
8 changes: 3 additions & 5 deletions library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar
}
break;
case Requests::HEAD:
$head = self::format_get($url_parts, $data);
$out = "HEAD $head HTTP/1.0\r\n";
break;
default:
case Requests::GET:
case Requests::DELETE:
$get = self::format_get($url_parts, $data);
$out = "GET $get HTTP/1.0\r\n";
$out = $options['type'] . " $get HTTP/1.0\r\n";
break;
}
$out .= "Host: {$url_parts['host']}\r\n";
Expand Down
22 changes: 22 additions & 0 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ public function testPUTWithArray() {
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
}

public function testDELETE() {
$request = Requests::delete('http://httpbin.org/delete', array(), $this->getOptions());
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals('http://httpbin.org/delete', $result['url']);
$this->assertEmpty($result['args']);
}

public function testDELETEWithData() {
$data = array(
'test' => 'true',
'test2' => 'test',
);
$request = Requests::request('http://httpbin.org/delete', array(), $data, Requests::DELETE, $this->getOptions());
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals('http://httpbin.org/delete?test=true&test2=test', $result['url']);
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
}

public function testRedirects() {
$request = Requests::get('http://httpbin.org/redirect/6', array(), $this->getOptions());
$this->assertEquals(200, $request->status_code);
Expand Down

0 comments on commit 329432f

Please sign in to comment.