Skip to content

Commit 6a6063b

Browse files
authored
Merge pull request #79 from open-sausages/pulls/allow-empty-response
Suppress exception in case of empty curl response
2 parents 3e45b65 + 59991f2 commit 6a6063b

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

lib/WebDriver/AbstractWebDriver.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,44 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
128128
throw WebDriverException::factory(WebDriverException::CURL_EXEC, 'Webdriver http error: ' . $info['http_code'] . ', payload :' . substr($rawResult, 0, 1000));
129129
}
130130

131-
$result = json_decode($rawResult, true);
131+
$result = [];
132+
$value = null;
133+
if (!empty($rawResult)) {
134+
$result = json_decode($rawResult, true);
132135

133-
if ($result === null && json_last_error() != JSON_ERROR_NONE) {
134-
throw WebDriverException::factory(WebDriverException::CURL_EXEC, 'Payload received from webdriver is not valid json: ' . substr($rawResult, 0, 1000));
135-
}
136+
if ($result === null && json_last_error() != JSON_ERROR_NONE) {
137+
throw WebDriverException::factory(WebDriverException::CURL_EXEC,
138+
'Payload received from webdriver is not valid json: ' . substr($rawResult, 0, 1000));
139+
}
136140

137-
if (!is_array($result) || !array_key_exists('status', $result)) {
138-
throw WebDriverException::factory(WebDriverException::CURL_EXEC, 'Payload received from webdriver is valid but unexpected json: ' . substr($rawResult, 0, 1000));
139-
}
141+
if (!is_array($result) || !array_key_exists('status', $result)) {
142+
throw WebDriverException::factory(WebDriverException::CURL_EXEC,
143+
'Payload received from webdriver is valid but unexpected json: ' . substr($rawResult, 0, 1000));
144+
}
140145

141-
$value = null;
146+
if (array_key_exists('value', $result)) {
147+
$value = $result['value'];
148+
}
142149

143-
if (array_key_exists('value', $result)) {
144-
$value = $result['value'];
145-
}
146-
147-
$message = null;
150+
$message = null;
148151

149-
if (is_array($value) && array_key_exists('message', $value)) {
150-
$message = $value['message'];
151-
}
152+
if (is_array($value) && array_key_exists('message', $value)) {
153+
$message = $value['message'];
154+
}
152155

153-
// if not success, throw exception
154-
if ((int) $result['status'] !== 0) {
155-
throw WebDriverException::factory($result['status'], $message);
156+
// if not success, throw exception
157+
if ((int) $result['status'] !== 0) {
158+
throw WebDriverException::factory($result['status'], $message);
159+
}
156160
}
157161

158162
$sessionId = isset($result['sessionId'])
159-
? $result['sessionId']
160-
: (isset($value['webdriver.remote.sessionid'])
163+
? $result['sessionId']
164+
: (
165+
isset($value['webdriver.remote.sessionid'])
161166
? $value['webdriver.remote.sessionid']
162-
: null);
167+
: null
168+
);
163169

164170
return array(
165171
'value' => $value,

lib/WebDriver/ServiceFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public function setServiceClass($serviceName, $className)
103103
$className = '\\' . $className;
104104
}
105105

106+
// Flush outdated service cache
107+
if (isset($this->serviceClasses[$serviceName]) && $this->serviceClasses[$serviceName] !== $className) {
108+
unset($this->services[$serviceName]);
109+
}
110+
106111
$this->serviceClasses[$serviceName] = $className;
107112
}
108113
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Test\WebDriver;
4+
5+
use WebDriver\Service\CurlServiceInterface;
6+
7+
/**
8+
* HTTP 200 response for any request:
9+
* - /session returns invalid json
10+
* - any other request returns empty body
11+
*/
12+
class TestCurlService implements CurlServiceInterface
13+
{
14+
public function execute($requestMethod, $url, $parameters = null, $extraOptions = array())
15+
{
16+
$info = array(
17+
'url' => $url,
18+
'request_method' => $requestMethod,
19+
'http_code' => 200,
20+
);
21+
if (preg_match('#.*session$#', $url)) {
22+
$result = 'some invalid json';
23+
} else {
24+
$result = '';
25+
}
26+
return array($result, $info);
27+
}
28+
}

test/Test/WebDriver/WebDriverTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace Test\WebDriver;
2323

24+
use WebDriver\ServiceFactory;
2425
use WebDriver\WebDriver;
2526

2627
/**
@@ -42,6 +43,8 @@ class WebDriverTest extends \PHPUnit_Framework_TestCase
4243
*/
4344
protected function setUp()
4445
{
46+
require_once __DIR__ . '/TestCurlService.php';
47+
4548
if ($url = getenv('ROOT_URL')) {
4649
$this->testDocumentRootUrl = $url;
4750
}
@@ -59,6 +62,7 @@ protected function setUp()
5962
*/
6063
protected function tearDown()
6164
{
65+
ServiceFactory::getInstance()->setServiceClass('service.curl', '\\WebDriver\\Service\\CurlService');
6266
if ($this->session) {
6367
$this->session->close();
6468
}
@@ -221,4 +225,22 @@ public function testSeleniumNoResponse()
221225

222226
$this->assertEquals(null, $out);
223227
}
228+
229+
/**
230+
* Assert that empty response does not trigger exception, but invalid JSON does
231+
*/
232+
public function testNonJsonResponse()
233+
{
234+
ServiceFactory::getInstance()->setServiceClass('service.curl', '\\Test\\WebDriver\\TestCurlService');
235+
$result = $this->driver->status();
236+
$this->assertNull($result);
237+
238+
// Test /session should error
239+
$this->setExpectedException(
240+
'WebDriver\Exception\CurlExec',
241+
'Payload received from webdriver is not valid json: some invalid json'
242+
);
243+
$result = $this->driver->session();
244+
$this->assertNull($result);
245+
}
224246
}

0 commit comments

Comments
 (0)