Skip to content

Commit

Permalink
No longer preserving CURLOPT_NOBODY after a HEAD.
Browse files Browse the repository at this point in the history
Fixes #47
Fixes fruux/sabre-dav#649
  • Loading branch information
evert committed May 11, 2015
1 parent c55cbc1 commit 6b06c03
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ChangeLog
=========

3.0.5 (2015-05-11)
------------------

* #47: When re-using the client and doing any request after a `HEAD` request,
the client discards the body.


3.0.4 (2014-12-10)
------------------

Expand Down
1 change: 1 addition & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function __construct() {
$this->curlSettings = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => false,
];

}
Expand Down
2 changes: 1 addition & 1 deletion lib/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Version {
/**
* Full version number
*/
const VERSION = '3.0.4';
const VERSION = '3.0.5';

}
54 changes: 52 additions & 2 deletions tests/HTTP/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function testCreateCurlSettingsArrayGET() {
CURLOPT_HEADER => true,
CURLOPT_POSTREDIR => 0,
CURLOPT_HTTPHEADER => ['X-Foo: bar'],
CURLOPT_NOBODY => false,
CURLOPT_URL => 'http://example.org/',
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_POSTFIELDS => null,
Expand Down Expand Up @@ -49,7 +50,42 @@ function testCreateCurlSettingsArrayHEAD() {
CURLOPT_CUSTOMREQUEST => 'HEAD',
CURLOPT_HTTPHEADER => ['X-Foo: bar'],
CURLOPT_URL => 'http://example.org/',
CURLOPT_POSTFIELDS => null,
CURLOPT_POSTFIELDS => '',
CURLOPT_PUT => false,
];

// FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
// at least if this unit test fails in the future we know it is :)
if(defined('HHVM_VERSION') === false) {
$settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
$settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
}

$this->assertEquals($settings, $client->createCurlSettingsArray($request));

}

function testCreateCurlSettingsArrayGETAfterHEAD() {

$client = new ClientMock();
$request = new Request('HEAD','http://example.org/', ['X-Foo' => 'bar']);

// Parsing the settings for this method, and discarding the result.
// This will cause the client to automatically persist previous
// settings and will help us detect problems.
$client->createCurlSettingsArray($request);

// This is the real request.
$request = new Request('GET','http://example.org/', ['X-Foo' => 'bar']);

$settings = [
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => ['X-Foo: bar'],
CURLOPT_NOBODY => false,
CURLOPT_URL => 'http://example.org/',
CURLOPT_POSTFIELDS => '',
CURLOPT_PUT => false,
];

Expand Down Expand Up @@ -77,6 +113,7 @@ function testCreateCurlSettingsArrayPUTStream() {
CURLOPT_HEADER => true,
CURLOPT_PUT => true,
CURLOPT_INFILE => $h,
CURLOPT_NOBODY => false,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_HTTPHEADER => ['X-Foo: bar'],
CURLOPT_URL => 'http://example.org/',
Expand All @@ -101,6 +138,7 @@ function testCreateCurlSettingsArrayPUTString() {
$settings = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => false,
CURLOPT_POSTFIELDS => 'boo',
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_HTTPHEADER => ['X-Foo: bar'],
Expand Down Expand Up @@ -337,12 +375,24 @@ function testDoRequestCurlError() {

class ClientMock extends Client {

protected $persistedSettings = [];

/**
* Making this method public.
*
* We are also going to persist all settings this method generates. While
* the underlying object doesn't behave exactly the same, it helps us
* simulate what curl does internally, and helps us identify problems with
* settings that are set by _some_ methods and not correctly reset by other
* methods after subsequent use.
* forces
*/
public function createCurlSettingsArray(RequestInterface $request) {

return parent::createCurlSettingsArray($request);
$settings = parent::createCurlSettingsArray($request);
$settings = $settings + $this->persistedSettings;
$this->persistedSettings = $settings;
return $settings;

}
/**
Expand Down

0 comments on commit 6b06c03

Please sign in to comment.