Skip to content

Commit

Permalink
MobileDetect by default will not throw an exception if UserAgent is n…
Browse files Browse the repository at this point in the history
…ot set. By default "autoInitOfHttpHeaders" is "true", and if UserAgent is not found, it will be set to empty string ""

#946 (comment)
  • Loading branch information
serbanghita committed Jan 10, 2024
1 parent ae8a4bc commit b7a8cdd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion MobileDetect.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.8.04",
"version": "4.8.05",
"headerMatch": {
"HTTP_ACCEPT": {
"matches": [
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- .:/app

runUnitTests:
depends_on: ['setup']
image: php:8.3-rc-alpine3.18
working_dir: /app
command: >
Expand All @@ -21,6 +22,7 @@ services:
- .:/app

runPerfTests:
depends_on: ['setup']
image: php:8.3-rc-alpine3.18
working_dir: /app
command: >
Expand All @@ -29,6 +31,7 @@ services:
- .:/app

runLinting:
depends_on: ['setup']
image: php:8.3-rc-alpine3.18
working_dir: /app
command: >
Expand All @@ -39,6 +42,7 @@ services:
- .:/app

generateModel:
depends_on: ['setup', 'runUnitTests', 'runPerfTests', 'runLinting']
image: php:8.3-rc-alpine3.18
working_dir: /app
command: >
Expand Down
12 changes: 10 additions & 2 deletions src/MobileDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Nick Ilyin <[email protected]>
* @author: Victor Stanciu <[email protected]> (original author)
*
* @version 4.8.04
* @version 4.8.05
*/

declare(strict_types=1);
Expand Down Expand Up @@ -236,7 +236,7 @@ class MobileDetect
/**
* Stores the version number of the current release.
*/
protected string $VERSION = '4.8.04';
protected string $VERSION = '4.8.05';

protected array $config = [
// Auto-initialization on HTTP headers from $_SERVER['HTTP...']
Expand Down Expand Up @@ -1033,6 +1033,8 @@ public function __construct(
// Override config from user.
$this->config = array_merge($this->config, $config);

// Beware that if you use "autoInitOfHttpHeaders: false" and you forget to setUserAgent
// to something other than a string, an MobileDetectException exception will be thrown.
if ($this->config['autoInitOfHttpHeaders']) {
$this->autoInitKnownHttpHeaders();
}
Expand Down Expand Up @@ -1077,6 +1079,12 @@ public function autoInitKnownHttpHeaders(): void
}
}
$this->setHttpHeaders($httpHeaders);

// Set the User-Agent even if it's an empty string so that "autoInitOfHttpHeaders" doesn't throw an exception.
// https://github.com/serbanghita/Mobile-Detect/issues/946#issuecomment-1885675939
if (!$this->hasUserAgent()) {
$this->setUserAgent("");
}
}

/**
Expand Down
27 changes: 23 additions & 4 deletions tests/MobileDetectGeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ public function testBadMethodCall()
/**
* @throws MobileDetectException
*/
public function testNoUserAgentSet()
public function testNoUserAgentSetAndAutoInitOfHttpHeadersIsFalse()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No valid user-agent has been set.');

$detect = new MobileDetect();
$detect = new MobileDetect(null, ['autoInitOfHttpHeaders' => false]);
$detect->isMobile();
}

/**
* @throws MobileDetectException
*/
public function testNoUserAgentSet()
{
$detect = new MobileDetect();
$this->assertFalse($detect->isMobile());
}

/**
* @throws MobileDetectException
*/
Expand Down Expand Up @@ -63,11 +72,21 @@ public function testAutoInitPicksUpKnownHttpHeaders()
* @throws MobileDetectException
*/
public function testValidHeadersThatDoNotContainHttpUserAgentHeaderButNoUserAgentIsManuallySet()
{
$detect = new MobileDetect();
$detect->setHttpHeaders([
'HTTP_CONNECTION' => 'close',
'HTTP_ACCEPT' => 'text/vnd.wap.wml, application/json, text/javascript, */*; q=0.01',
]);
$this->assertFalse($detect->isMobile());
}

public function testValidHeadersThatDoNotContainHttpUserAgentHeaderButNoUserAgentIsManuallySetAndAutoInitOfHttpHeadersIsFalse()
{
$this->expectException(MobileDetectException::class);
$this->expectExceptionMessage('No valid user-agent has been set.');

$detect = new MobileDetect();
$detect = new MobileDetect(null, ['autoInitOfHttpHeaders' => false]);
$detect->setHttpHeaders([
'HTTP_CONNECTION' => 'close',
'HTTP_ACCEPT' => 'text/vnd.wap.wml, application/json, text/javascript, */*; q=0.01',
Expand Down Expand Up @@ -234,7 +253,7 @@ public function userAgentProvider(): array
[[
'HTTP_X_DEVICE_USER_AGENT' => 'hello world'
], 'hello world'],
[[], null]
[[], '']
];
}

Expand Down

0 comments on commit b7a8cdd

Please sign in to comment.