Skip to content
Permalink
Browse files

Fix #12168 - Stream restores error handler even if wrapper throws exc…

…eption
  • Loading branch information...
snelg committed Jun 1, 2018
1 parent d9a61c3 commit fe97f0650ae8388a372b7855a3a85e7aa347afa2
Showing with 35 additions and 2 deletions.
  1. +5 −2 src/Http/Client/Adapter/Stream.php
  2. +30 −0 tests/TestCase/Http/Client/Adapter/StreamTest.php
@@ -306,8 +306,11 @@ protected function _open($url)
set_error_handler(function ($code, $message) {
$this->_connectionErrors[] = $message;
});
$this->_stream = fopen($url, 'rb', false, $this->_context);
restore_error_handler();
try {
$this->_stream = fopen($url, 'rb', false, $this->_context);
} finally {
restore_error_handler();
}
if (!$this->_stream || !empty($this->_connectionErrors)) {
throw new Exception(implode("\n", $this->_connectionErrors));
@@ -35,6 +35,10 @@ class CakeStreamWrapper implements \ArrayAccess
public function stream_open($path, $mode, $options, &$openedPath)
{
if ($path == 'http://throw_exception/') {
throw new \Exception;
}
$query = parse_url($path, PHP_URL_QUERY);
if ($query) {
parse_str($query, $this->_query);
@@ -151,6 +155,32 @@ public function testSendByUsingCakephpProtocol()
$this->assertEquals(20000, strlen($responses[0]->body()));
}
/**
* Test stream error_handler cleanup when wrapper throws exception
*
* @return void
*/
public function testSendWrapperException()
{
$stream = new Stream();
$request = new Request('http://throw_exception/');
$currentHandler = set_error_handler(function () {
});
restore_error_handler();
try {
$stream->send($request, []);
} catch (\Exception $e) {
}
$newHandler = set_error_handler(function () {
});
restore_error_handler();
$this->assertEquals($currentHandler, $newHandler);
}
/**
* Test building the context headers
*

0 comments on commit fe97f06

Please sign in to comment.
You can’t perform that action at this time.