Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from elricho/master
Browse files Browse the repository at this point in the history
Add error code for localDetection when files not found/installed.
  • Loading branch information
elricho authored Dec 21, 2016
2 parents 42627f8 + 91283e9 commit 42f5f9b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/HDBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?');
}

/**
Expand Down
35 changes: 18 additions & 17 deletions src/HDDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions tests/hd4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 42f5f9b

Please sign in to comment.