Skip to content

Commit

Permalink
[bug] No user-agent has been set #946
Browse files Browse the repository at this point in the history
  • Loading branch information
serbanghita committed Oct 28, 2023
1 parent 80607ae commit 4921892
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 33 deletions.
18 changes: 0 additions & 18 deletions Dockerfile

This file was deleted.

43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
setup:
image: composer:latest
command: >
/bin/sh -c "
rm -rf vendor &&
rm -f composer.lock composer.phar &&
set -xe && composer install &&
composer -v &&
ls -al"
working_dir: /app
volumes:
- .:/app

runUnitTests:
image: php:8.0.3-fpm-alpine3.13
working_dir: /app
command: >
/bin/sh -c "vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-text --strict-coverage --stop-on-risky"
ports:
- "8000:8000"
volumes:
- .:/app

runLinting:
image: php:8.0.3-fpm-alpine3.13
working_dir: /app
command: >
/bin/sh -c "vendor/bin/phpcs; vendor/bin/phpcbf"
ports:
- "8000:8000"
volumes:
- .:/app

generateModel:
image: php:8.0.3-fpm-alpine3.13
working_dir: /app
command: >
/bin/sh -c "php ./scripts/export_to_json.php"
ports:
- "8000:8000"
volumes:
- .:/app
33 changes: 25 additions & 8 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.03
* @version 4.8.04
*/

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.03';
protected string $VERSION = '4.8.04';

protected array $config = [
// Auto-initialization on HTTP headers from $_SERVER['HTTP...']
Expand Down Expand Up @@ -1210,12 +1210,12 @@ private function prepareUserAgent(string $userAgent): string
* Set the User-Agent to be used.
*
* @param string $userAgent The User-Agent string.
* @return string|null
* @return string
*/
public function setUserAgent(string $userAgent): string|null
public function setUserAgent(string $userAgent): string
{
$preparedUserAgent = $this->prepareUserAgent($userAgent);
return $this->userAgent = !empty($preparedUserAgent) ? $preparedUserAgent : null;
return $this->userAgent = $preparedUserAgent;
}

/**
Expand All @@ -1230,7 +1230,12 @@ public function getUserAgent(): ?string

public function hasUserAgent(): bool
{
return \is_string($this->userAgent) && !empty($this->userAgent);
return is_string($this->userAgent);
}

public function isUserAgentEmpty(): bool
{
return $this->hasUserAgent() && $this->userAgent === '';
}

public function getMatchingRegex(): ?string
Expand Down Expand Up @@ -1366,7 +1371,11 @@ public function __call(string $name, array $arguments)
public function isMobile(): bool
{
if (!$this->hasUserAgent()) {
throw new MobileDetectException('No user-agent has been set.');
throw new MobileDetectException('No valid user-agent has been set.');
}

if ($this->isUserAgentEmpty()) {
return false;
}

// Cache check.
Expand Down Expand Up @@ -1411,6 +1420,10 @@ public function isTablet(): bool
throw new MobileDetectException('No user-agent has been set.');
}

if ($this->isUserAgentEmpty()) {
return false;
}

// Cache check.
try {
$cacheKey = $this->createCacheKey("tablet");
Expand Down Expand Up @@ -1476,6 +1489,10 @@ public function is(string $ruleName): bool
throw new MobileDetectException('No user-agent has been set.');
}

if ($this->isUserAgentEmpty()) {
return false;
}

// Cache check.
try {
$cacheKey = $this->createCacheKey($ruleName);
Expand Down Expand Up @@ -1671,7 +1688,7 @@ public static function flattenHeaders(array $httpHeaders): string
{
$key = '';
foreach ($httpHeaders as $name => $value) {
$key .= "$name: $value" . "\n";
$key .= "$name: $value" . PHP_EOL;
}
return trim($key);
}
Expand Down
14 changes: 12 additions & 2 deletions tests/MobileDetectGeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ public function testBadMethodCall()
public function testNoUserAgentSet()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No user-agent has been set.');
$this->expectExceptionMessage('No valid user-agent has been set.');

$detect = new MobileDetect();
$detect->isMobile();
}

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

/**
* @throws MobileDetectException
*/
Expand All @@ -55,7 +65,7 @@ public function testAutoInitPicksUpKnownHttpHeaders()
public function testValidHeadersThatDoNotContainHttpUserAgentHeaderButNoUserAgentIsManuallySet()
{
$this->expectException(MobileDetectException::class);
$this->expectExceptionMessage('No user-agent has been set.');
$this->expectExceptionMessage('No valid user-agent has been set.');

$detect = new MobileDetect();
$detect->setHttpHeaders([
Expand Down
9 changes: 4 additions & 5 deletions tests/MobileDetectWithCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ public function testFlattenHeaders()
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
]);

$expectedString = <<<TEXT
HTTP_REQUEST_METHOD: DELETE
HTTP_USER_AGENT: Mozilla/5.0 iPhone;
HTTP_ACCEPT_LANGUAGE: en-us,en;q=0.5
TEXT;
$expectedString = "HTTP_REQUEST_METHOD: DELETE" . PHP_EOL .
"HTTP_USER_AGENT: Mozilla/5.0 iPhone;" . PHP_EOL .
"HTTP_ACCEPT_LANGUAGE: en-us,en;q=0.5";

$this->assertEquals($expectedString, $cacheKey);
}

Expand Down

0 comments on commit 4921892

Please sign in to comment.