diff --git a/src/HDBase.php b/src/HDBase.php index 6793bef..3d87ce5 100644 --- a/src/HDBase.php +++ b/src/HDBase.php @@ -603,7 +603,7 @@ function getBranch($branch) { $this->tree[$branch] = $tmp; return $tmp; } - return false; + return $this->setError(299, 'Branch not found. Is it installed ?'); } /** diff --git a/src/HDDevice.php b/src/HDDevice.php index 4c98bac..1ebc005 100644 --- a/src/HDDevice.php +++ b/src/HDDevice.php @@ -176,6 +176,16 @@ function localWhatHas($key, $value) { * @return bool true on success, false otherwise */ function localDetect($headers) { + $this->device = null; + $this->platform = null; + $this->browser = null; + $this->app = null; + $this->ratingResult = null; + $this->detectedRuleKey = array(); + $this->reply = array(); + $this->reply['status'] = 0; + $this->reply['message'] = ''; + // lowercase headers on the way in. $headers = array_change_key_case($headers); $hardwareInfo = @$headers['x-local-hardwareinfo']; @@ -466,14 +476,6 @@ function fetchDevices() { * @return mixed device array on success, false otherwise */ function v4MatchBuildInfo($buildInfo) { - $this->device = null; - $this->platform = null; - $this->browser = null; - $this->app = null; - $this->detectedRuleKey = null; - $this->ratingResult = null; - $this->reply = null; - // Nothing to check if (empty($buildInfo)) return false; @@ -560,13 +562,6 @@ function v4MatchBIHelper($buildInfo, $category='device') { * @return array device specs. (device.hd_specs) **/ function v4MatchHttpHeaders($headers, $hardwareInfo=null) { - $this->device = null; - $this->platform = null; - $this->browser = null; - $this->app = null; - $this->ratingResult = null; - $this->detectedRuleKey = array(); - $this->reply = null; $hwProps = null; // Nothing to check @@ -601,8 +596,14 @@ function v4MatchHttpHeaders($headers, $hardwareInfo=null) { } $this->device = $this->matchDevice($this->deviceHeaders); - if (empty($this->device)) - return $this->setError(301, "Not Found"); + if (empty($this->device)) { + if (! isset($this->reply['status']) || $this->reply['status'] == 0) { + // If no downstream error set then return not found. + return $this->setError(301, "Not Found"); + } + // Error is already set, so return false + return false; + } if (! empty($hardwareInfo)) $hwProps = $this->infoStringToArray($hardwareInfo); diff --git a/tests/hd4Test.php b/tests/hd4Test.php index 113252b..e637f3d 100644 --- a/tests/hd4Test.php +++ b/tests/hd4Test.php @@ -709,6 +709,26 @@ function test_unzipBogusArchive() { $this->assertFalse($result); $this->assertEquals(299, $data['status']); } + + /** + * Empty Archive Test + * @group ultimate + **/ + function test_detectionOnEmptyArchive() { + $hd = new HandsetDetection\HD4($this->ultimateConfig); + + $store = HandsetDetection\HDStore::getInstance(); + $store->purge(); + + $headers = array( + 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' + ); + + $result = $hd->deviceDetect($headers); + $reply = $hd->getReply(); + $this->assertEquals(299, $reply['status']); + $this->assertEquals('Branch not found. Is it installed ?', $reply['message']); + } /** * Fetch Archive Test