-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suppress exception in case of empty curl response #79
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,11 @@ public function setServiceClass($serviceName, $className) | |
$className = '\\' . $className; | ||
} | ||
|
||
// Flush outdated service cache | ||
if (isset($this->serviceClasses[$serviceName]) && $this->serviceClasses[$serviceName] !== $className) { | ||
unset($this->services[$serviceName]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What version of php requires this? Looks like a refcount hack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not PHP version specific; ServiceFactory::getService() would ignore any updated serviceClass if an outdated service exists for any prior value. This is necessary to force the I would replace with setService() to allow a mock to be directly injected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I understand what's going on now. I was skimming the code from my phone earlier. |
||
} | ||
|
||
$this->serviceClasses[$serviceName] = $className; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Test\WebDriver; | ||
|
||
use WebDriver\Service\CurlServiceInterface; | ||
|
||
/** | ||
* HTTP 200 response for any request: | ||
* - /session returns invalid json | ||
* - any other request returns empty body | ||
*/ | ||
class TestCurlService implements CurlServiceInterface | ||
{ | ||
public function execute($requestMethod, $url, $parameters = null, $extraOptions = array()) | ||
{ | ||
$info = array( | ||
'url' => $url, | ||
'request_method' => $requestMethod, | ||
'http_code' => 200, | ||
); | ||
if (preg_match('#.*session$#', $url)) { | ||
$result = 'some invalid json'; | ||
} else { | ||
$result = ''; | ||
} | ||
return array($result, $info); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
namespace Test\WebDriver; | ||
|
||
use WebDriver\ServiceFactory; | ||
use WebDriver\WebDriver; | ||
|
||
/** | ||
|
@@ -42,6 +43,8 @@ class WebDriverTest extends \PHPUnit_Framework_TestCase | |
*/ | ||
protected function setUp() | ||
{ | ||
require_once __DIR__ . '/TestCurlService.php'; | ||
|
||
if ($url = getenv('ROOT_URL')) { | ||
$this->testDocumentRootUrl = $url; | ||
} | ||
|
@@ -59,6 +62,7 @@ protected function setUp() | |
*/ | ||
protected function tearDown() | ||
{ | ||
ServiceFactory::getInstance()->setServiceClass('service.curl', '\\WebDriver\\Service\\CurlService'); | ||
if ($this->session) { | ||
$this->session->close(); | ||
} | ||
|
@@ -221,4 +225,22 @@ public function testSeleniumNoResponse() | |
|
||
$this->assertEquals(null, $out); | ||
} | ||
|
||
/** | ||
* Assert that empty response does not trigger exception, but invalid JSON does | ||
*/ | ||
public function testNonJsonResponse() | ||
{ | ||
ServiceFactory::getInstance()->setServiceClass('service.curl', '\\Test\\WebDriver\\TestCurlService'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I strongly feel this should be served by a mock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes also, but I was hesitant to make any strong changes to an unrelated API. ServiceFactory would be good if you could inject a mock directly. |
||
$result = $this->driver->status(); | ||
$this->assertNull($result); | ||
|
||
// Test /session should error | ||
$this->setExpectedException( | ||
'WebDriver\Exception\CurlExec', | ||
'Payload received from webdriver is not valid json: some invalid json' | ||
); | ||
$result = $this->driver->session(); | ||
$this->assertNull($result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to rewrite this since we want to push a fix tonight while maintaining our coding standards (eg no nested if).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I apologise for the mis-standard! Your standard is a good one. :)