Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #567 from weierophinney/hotfix/less-strict-headers
Browse files Browse the repository at this point in the history
Cast int and float to string when creating headers
  • Loading branch information
froschdesign committed May 20, 2015
2 parents d925ec1 + b07f4ea commit 330a4c8
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 44 deletions.
5 changes: 5 additions & 0 deletions library/Zend/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ protected function _validateHeaderValue($value, $recurse = true)
return;
}

// Cast integers and floats to strings for purposes of header representation.
if (is_int($value) || is_float($value)) {
$value = (string) $value;
}

if (! is_string($value) && (! is_object($value) || ! method_exists($value, '__toString'))) {
require_once 'Zend/Http/Exception.php';
throw new Zend_Http_Exception('Invalid header value detected');
Expand Down
8 changes: 7 additions & 1 deletion tests/Zend/Cloud/Infrastructure/Adapter/Ec2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public function setUp()
*/
protected function loadResponse($name)
{
return file_get_contents($name);
$response = file_get_contents($name);

// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
}
/**
* Get Config Array
Expand Down
12 changes: 9 additions & 3 deletions tests/Zend/Cloud/Infrastructure/Adapter/RackspaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function setUp()

if (file_exists($filename)) {
// authentication (from file)
$content = file_get_contents(dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response');
$this->httpClientAdapterTest->setResponse($content);
$content = dirname(__FILE__) . '/_files/'.$shortClassName . '_testAuthenticate.response';
$this->httpClientAdapterTest->setResponse($this->loadResponse($content));
$this->assertTrue($this->infrastructure->getAdapter()->authenticate(),'Authentication failed');

$this->httpClientAdapterTest->setResponse($this->loadResponse($filename));
Expand All @@ -91,7 +91,13 @@ public function setUp()
*/
protected function loadResponse($name)
{
return file_get_contents($name);
$response = file_get_contents($name);

// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
}
/**
* Get Config Array
Expand Down
23 changes: 17 additions & 6 deletions tests/Zend/Gdata/App/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class Zend_Gdata_App_EntryTest extends PHPUnit_Framework_TestCase

public function setUp()
{
$this->enryText = file_get_contents(
'Zend/Gdata/App/_files/EntrySample1.xml',
true);
$this->httpEntrySample = file_get_contents(
'Zend/Gdata/App/_files/EntrySampleHttp1.txt',
true);
$this->enryText = $this->loadResponse(
dirname(__FILE__) . '/../App/_files/EntrySample1.xml'
);
$this->httpEntrySample = $this->loadResponse(
dirname(__FILE__) . '/../App/_files/EntrySampleHttp1.txt'
);
$this->enry = new Zend_Gdata_App_Entry();

$this->adapter = new Test_Zend_Gdata_MockHttpClient();
Expand All @@ -53,6 +53,17 @@ public function setUp()
$this->service = new Zend_Gdata_App($this->client);
}

public function loadResponse($filename)
{
$response = file_get_contents($filename);

// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
}

public function testEmptyEntryShouldHaveEmptyExtensionsList()
{
$this->assertTrue(is_array($this->enry->extensionElements));
Expand Down
35 changes: 23 additions & 12 deletions tests/Zend/Gdata/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,36 @@ public function setUp()
$this->expectedEtag = 'W/"CkcHQH8_fCp7ImA9WxRTGEw."';
$this->expectedMajorProtocolVersion = 1;
$this->expectedMinorProtocolVersion = 2;
$this->httpEntrySample = file_get_contents(
'Zend/Gdata/_files/AppSample1.txt',
true);
$this->httpEntrySampleWithoutVersion = file_get_contents(
'Zend/Gdata/_files/AppSample2.txt',
true);
$this->httpFeedSample = file_get_contents(
'Zend/Gdata/_files/AppSample3.txt',
true);
$this->httpFeedSampleWithoutVersion = file_get_contents(
'Zend/Gdata/_files/AppSample4.txt',
true);
$this->httpEntrySample = $this->loadResponse(
dirname(__FILE__) . '/_files/AppSample1.txt'
);
$this->httpEntrySampleWithoutVersion = $this->loadResponse(
dirname(__FILE__) . '/_files/AppSample2.txt'
);
$this->httpFeedSample = $this->loadResponse(
dirname(__FILE__) . '/_files/AppSample3.txt'
);
$this->httpFeedSampleWithoutVersion = $this->loadResponse(
dirname(__FILE__) . '/_files/AppSample4.txt'
);

$this->adapter = new Test_Zend_Gdata_MockHttpClient();
$this->client = new Zend_Gdata_HttpClient();
$this->client->setAdapter($this->adapter);
$this->service = new Zend_Gdata_App($this->client);
}

public function loadResponse($filename)
{
$response = file_get_contents($filename);

// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $response);
}

public function testImportFile()
{
$feed = Zend_Gdata_App::importFile($this->fileName,
Expand Down
7 changes: 2 additions & 5 deletions tests/Zend/Gdata/AuthSubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@ public function testAuthSubRevokeTokenCatchesHttpClientException()
public function testGetAuthSubTokenInfoReceivesSuccessfulResult()
{
$adapter = new Zend_Http_Client_Adapter_Test();
$adapter->setResponse("HTTP/1.1 200 OK
Target=http://example.com
Scope=http://example.com
Secure=false");
$response = "HTTP/1.1 200 OK\r\n\r\nTarget=http://example.com\nScope=http://example.com\nSecure=false";
$adapter->setResponse($response);

$client = new Zend_Gdata_HttpClient();
$client->setUri('http://example.com/AuthSub');
Expand Down
17 changes: 8 additions & 9 deletions tests/Zend/Http/Client/StaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,14 @@ public function testRedirectWithTrailingSpaceInLocationHeaderZF11283()

$adapter = $this->_client->getAdapter(); /* @var $adapter Zend_Http_Client_Adapter_Test */

$adapter->setResponse(<<<RESPONSE
HTTP/1.1 302 Redirect
Content-Type: text/html; charset=UTF-8
Location: /test
Server: Microsoft-IIS/7.0
Date: Tue, 19 Apr 2011 11:23:48 GMT
RESPONSE
);
$response = "HTTP/1.1 302 Redirect\r\n"
. "Content-Type: text/html; charset=UTF-8\r\n"
. "Location: /test\r\n"
. "Server: Microsoft-IIS/7.0\r\n"
. "Date: Tue, 19 Apr 2011 11:23:48 GMT\r\n\r\n"
. "RESPONSE";

$adapter->setResponse($response);

$res = $this->_client->request('GET');

Expand Down
25 changes: 19 additions & 6 deletions tests/Zend/Http/CookieJarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
*/
class Zend_Http_CookieJarTest extends PHPUnit_Framework_TestCase
{
public function loadResponse($filename)
{
$message = file_get_contents($filename);
// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
}

/**
* Test we can add cookies to the jar
*
Expand Down Expand Up @@ -83,8 +93,9 @@ public function testExceptAddInvalidCookie()
public function testAddCookiesFromResponse()
{
$jar = new Zend_Http_Cookiejar();
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies');
$res_str = $this->loadResponse(
dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
);
$response = Zend_Http_Response::fromString($res_str);

$jar->addCookiesFromResponse($response, 'http://www.example.com');
Expand Down Expand Up @@ -442,8 +453,9 @@ public function testExceptGetMatchingCookiesInvalidUri()
*/
public function testFromResponse()
{
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_single_cookie');
$res_str = $this->loadResponse(
dirname(realpath(__FILE__)) . '/_files/response_with_single_cookie'
);
$response = Zend_Http_Response::fromString($res_str);

$jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
Expand All @@ -457,8 +469,9 @@ public function testFromResponse()
*/
public function testFromResponseMultiHeader()
{
$res_str = file_get_contents(dirname(realpath(__FILE__)) .
DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies');
$res_str = $this->loadResponse(
dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
);
$response = Zend_Http_Response::fromString($res_str);

$jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
Expand Down
7 changes: 6 additions & 1 deletion tests/Zend/Service/Audioscrobbler/AudioscrobblerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public function testCallInterceptMethodsRequireExactlyOneParameterAndThrowExcept

public static function readTestResponse($file)
{
return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $file);
$message = file_get_contents(sprintf('%s/_files/%s', dirname(__FILE__), $file));
// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
}
}
7 changes: 6 additions & 1 deletion tests/Zend/Service/Flickr/OfflineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,12 @@ protected function _saveResponse($name)
*/
protected function _loadResponse($name)
{
return file_get_contents("$this->_filesPath/$name.response");
$message = file_get_contents(sprintf('%s/%s.response', $this->_filesPath, $name));
// Line endings are sometimes an issue inside the canned responses; the
// following is a negative lookbehind assertion, and replaces any \n
// not preceded by \r with the sequence \r\n, ensuring that the message
// is well-formed.
return preg_replace("#(?<!\r)\n#", "\r\n", $message);
}
}

Expand Down

0 comments on commit 330a4c8

Please sign in to comment.