Skip to content

Commit 0e6c48c

Browse files
committed
Minor fixes:
- Bug: Rule didn't always match when URL contained any of the following characters +()[] - Added tests for the above characters - Minor readability and code optimizations Test improvements/fixes - List of hosts randomized when searching for an test subject, returning an HTTP 500 status code (we don't want to prioritize) - Mark MySQL tests as skipped (instead of failing) if unable to connect to the test database
1 parent 88dd1b4 commit 0e6c48c

8 files changed

Lines changed: 49 additions & 26 deletions

File tree

src/Client/Directives/DirectiveClientTrait.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@ private function isBetween($timestamp, $fromTime, $toTime, $format = 'Hi')
8282
*/
8383
private function checkPaths($path, array $paths)
8484
{
85-
$pairs = [
85+
$reserved = [
8686
'?' => '\?',
8787
'.' => '\.',
8888
'*' => '.*',
89+
'+' => '\+',
90+
'(' => '\(',
91+
')' => '\)',
92+
'[' => '\[',
93+
']' => '\]',
8994
];
9095
$errorHandler = new ErrorHandler();
9196
set_error_handler([$errorHandler, 'callback'], E_NOTICE | E_WARNING);
9297
foreach ($paths as $rule) {
93-
$escaped = $rule;
94-
foreach ($pairs as $search => $replace) {
95-
$escaped = str_replace($search, $replace, $escaped);
96-
}
98+
$escaped = str_replace(array_keys($reserved), array_values($reserved), $rule);
9799
if (preg_match('#^' . $escaped . '#', $path) === 1) {
98100
restore_error_handler();
99101
return mb_strlen($rule);

src/Parser/UriParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function encode()
7070
'!%21!ui' => "!",
7171
'!%23!ui' => "#",
7272
'!%24!ui' => "$",
73-
'!%25!ui' => "%",
7473
'!%26!ui' => "&",
7574
'!%27!ui' => "'",
7675
'!%28!ui' => "(",
@@ -86,7 +85,10 @@ public function encode()
8685
'!%40!ui' => "@",
8786
'!%5B!ui' => "[",
8887
'!%5D!ui' => "]",
88+
'!%25!ui' => "%",
8989
];
90+
// The % character must be the last in the $reserved array.
91+
// This makes sure that the already encoded values are not lost or encoded again.
9092
$this->uri = preg_replace(array_keys($reserved), array_values($reserved), rawurlencode($this->uri));
9193
return $this->baseToLowercase();
9294
}

tests/EscapingTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ public function testEscaping($robotsTxtContent, $rendered)
3232
$this->assertTrue($parser->userAgent()->isAllowed("/%5C."));
3333
$this->assertFalse($parser->userAgent()->isDisallowed("/%5C."));
3434

35-
/**
36-
* Additional tests to enable in the future, currently disabled due to bugs
37-
*/
38-
//$this->assertTrue($parser->userAgent()->isDisallowed("/("));
39-
//$this->assertFalse($parser->userAgent()->isAllowed("/("));
35+
$this->assertTrue($parser->userAgent()->isDisallowed("/("));
36+
$this->assertFalse($parser->userAgent()->isAllowed("/("));
37+
38+
$this->assertTrue($parser->userAgent()->isDisallowed("/)"));
39+
$this->assertFalse($parser->userAgent()->isAllowed("/)"));
40+
41+
$this->assertTrue($parser->userAgent()->isAllowed("/+"));
42+
$this->assertFalse($parser->userAgent()->isDisallowed("/+"));
4043

4144
if ($rendered !== false) {
4245
if (version_compare(phpversion(), '7.0.0', '<')) {

tests/HttpRetryAfterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testHttpRetryAfter()
5050
}
5151
}
5252
if ($count === 0) {
53-
$this->markTestSkipped("No URLs returned both `HTTP 503` and the `Retry-after` header. NB! Such circumstances are uncommon, and if the criteria is met, it's only a temporary state.");
53+
$this->markTestIncomplete('None of the test urls returned both `HTTP 503` and the `Retry-after` header. Such circumstances are usually temporary and uncommon. Try updating the list of test subjects.');
5454
}
5555
}
5656
}

tests/MysqlCacheDisplaceTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MysqlCacheDisplaceTest extends TestCase
2929
public function testCacheDisplace()
3030
{
3131
// This URL list is based on real data. Update when needed!
32-
// It loops thou the list, until an http 500 match is found. Therefore not all URL are fetched...
32+
// It loops thou the list, until an http 500 match is found. Therefore not all URLs are fetched...
3333
$bases = [
3434
'http://acheterdesvues.fr:80',
3535
'http://basbaassauce.com:80',
@@ -67,14 +67,14 @@ public function testCacheDisplace()
6767
'https://www.aei.org:443',
6868
'https://www.edoramedia.com:443',
6969
];
70-
$result = false;
70+
// Query these (random) hosts at random order, we don't want to prioritize any of them over each other.
71+
shuffle($bases);
7172
foreach ($bases as $base) {
72-
if ($result) {
73-
continue;
73+
if ($this->check($base)) {
74+
return;
7475
}
75-
$result = $this->check($base);
7676
}
77-
$this->assertTrue($result);
77+
$this->markTestIncomplete('None of the test urls returned an HTTP 500 status code. Try updating the list of test subjects.');
7878
}
7979

8080
/**
@@ -84,7 +84,12 @@ public function testCacheDisplace()
8484
*/
8585
private function check($baseUri)
8686
{
87-
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
87+
try {
88+
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
89+
} catch (\PDOException $e) {
90+
$this->markTestSkipped('Unable to connect to the MySQL database');
91+
return false;
92+
}
8893
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
8994

9095
$cache = (new RobotsTxtParser\Database($pdo))->cache();
@@ -116,9 +121,6 @@ private function check($baseUri)
116121
$query->execute();
117122
// Delete fake data
118123
$base->invalidate();
119-
if ($query->rowCount() > 0) {
120-
return true;
121-
}
122-
return false;
124+
return $query->rowCount() > 0;
123125
}
124126
}

tests/MysqlCacheTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class MysqlCacheTest extends TestCase
2727
*/
2828
public function testCache($uri, $baseUri)
2929
{
30-
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
30+
try {
31+
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
32+
} catch (\PDOException $e) {
33+
$this->markTestSkipped('Unable to connect to the MySQL database');
34+
return;
35+
}
3136
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
3237

3338
$database = new RobotsTxtParser\Database($pdo);

tests/MysqlDelayTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ class MysqlDelayTest extends TestCase
2626
*/
2727
public function testDelay($uri, $userAgent)
2828
{
29-
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
29+
try {
30+
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
31+
} catch (\PDOException $e) {
32+
$this->markTestSkipped('Unable to connect to the MySQL database');
33+
return;
34+
}
3035
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
3136

3237
$handler = (new RobotsTxtParser\Database($pdo))->delay();

tests/RenderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function testRender($robotsTxtContent, $rendered)
4242
$this->assertEquals($rendered['compatibility'], $parser->render()->compatibility("\n"));
4343

4444
// Make sure the compatibility robots.txt has a newline at the end
45-
foreach (["\r", "\n", "\r\n"] as $separator) {
45+
foreach ([
46+
"\r",
47+
"\n",
48+
"\r\n"
49+
] as $separator) {
4650
$length = strlen($separator);
4751
$this->assertEquals($separator, substr($parser->render()->compatibility($separator), -$length));
4852
}

0 commit comments

Comments
 (0)