Skip to content

Commit 76b5a85

Browse files
committed
Fix domain validator + formatter + double shash tests
1 parent 349d403 commit 76b5a85

10 files changed

Lines changed: 54 additions & 16 deletions

File tree

src/Http/Adapter/Swoole/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function onStart(callable $callback)
4242

4343
public function start()
4444
{
45-
if(Coroutine::getCid() === -1) {
45+
if (Coroutine::getCid() === -1) {
4646
run(fn () => $this->server->start());
4747
} else {
4848
$this->server->start();

src/Http/Http.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public function getResource(string $name, string $context = 'utopia', bool $fres
366366
$this->resources[$context] ??= [];
367367

368368
$resourcesCallback = &self::$resourcesCallbacks[$context] ?? [];
369-
if(empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
369+
if (empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
370370
$resourcesCallback = &self::$resourcesCallbacks['utopia'];
371371
}
372372

@@ -576,7 +576,7 @@ public function start()
576576
try {
577577
$this->run($request, $response, $context);
578578
} finally {
579-
if(isset(self::$resourcesCallbacks[$context])) {
579+
if (isset(self::$resourcesCallbacks[$context])) {
580580
unset(self::$resourcesCallbacks[$context]);
581581
}
582582
}
@@ -593,7 +593,7 @@ public function start()
593593
$arguments = $this->getArguments($hook, 'utopia', [], []);
594594
\call_user_func_array($hook->getAction(), $arguments);
595595
}
596-
} catch(\Exception $e) {
596+
} catch (\Exception $e) {
597597
self::setResource('error', fn () => $e);
598598

599599
foreach (self::$errors as $error) { // Global error hooks
@@ -793,7 +793,7 @@ public function run(Request $request, Response $response, string $context): stat
793793
$arguments = $this->getArguments($hook, $context, [], []);
794794
\call_user_func_array($hook->getAction(), $arguments);
795795
}
796-
} catch(\Exception $e) {
796+
} catch (\Exception $e) {
797797
self::setResource('error', fn () => $e, [], $context);
798798

799799
foreach (self::$errors as $error) { // Global error hooks

src/Http/Validator/AllOf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(protected array $validators, protected string $type
2929
*/
3030
public function getDescription(): string
3131
{
32-
if(!(\is_null($this->failedRule))) {
32+
if (!(\is_null($this->failedRule))) {
3333
$description = $this->failedRule->getDescription();
3434
} else {
3535
$description = $this->validators[0]->getDescription();
@@ -51,7 +51,7 @@ public function isValid(mixed $value): bool
5151
foreach ($this->validators as $rule) {
5252
$valid = $rule->isValid($value);
5353

54-
if(!$valid) {
54+
if (!$valid) {
5555
$this->failedRule = $rule;
5656
return false;
5757
}

src/Http/Validator/AnyOf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(protected array $validators, protected string $type
2929
*/
3030
public function getDescription(): string
3131
{
32-
if(!(\is_null($this->failedRule))) {
32+
if (!(\is_null($this->failedRule))) {
3333
$description = $this->failedRule->getDescription();
3434
} else {
3535
$description = $this->validators[0]->getDescription();
@@ -53,7 +53,7 @@ public function isValid(mixed $value): bool
5353

5454
$this->failedRule = $rule;
5555

56-
if($valid) {
56+
if ($valid) {
5757
return true;
5858
}
5959
}

src/Http/Validator/ArrayList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getDescription(): string
4646
{
4747
$msg = 'Value must a valid array';
4848

49-
if($this->length > 0) {
49+
if ($this->length > 0) {
5050
$msg .= ' no longer than ' . $this->length . ' items';
5151
}
5252

src/Http/Validator/Domain.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public function isValid($value): bool
4545
return false;
4646
}
4747

48-
if (\filter_var($value, FILTER_VALIDATE_DOMAIN) === false) {
48+
if (\filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) {
49+
return false;
50+
}
51+
52+
if (\str_ends_with($value, '.') || \str_ends_with($value, '-')) {
4953
return false;
5054
}
5155

src/Http/Validator/NoneOf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getDescription(): string
3131
{
3232
$description = '';
3333

34-
if(!(\is_null($this->failedRule))) {
34+
if (!(\is_null($this->failedRule))) {
3535
$description = $this->failedRule->getDescription();
3636
} else {
3737
$description = $this->validators[0]->getDescription();
@@ -53,7 +53,7 @@ public function isValid(mixed $value): bool
5353
foreach ($this->validators as $rule) {
5454
$valid = $rule->isValid($value);
5555

56-
if($valid) {
56+
if ($valid) {
5757
$this->failedRule = $rule;
5858
return false;
5959
}

tests/ResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testCanSetStatus()
3636

3737
try {
3838
$this->response->setStatusCode(0); // Unknown status code
39-
} catch(\Exception $e) {
39+
} catch (\Exception $e) {
4040
$this->assertInstanceOf('\Exception', $e);
4141

4242
return;

tests/Validator/DomainTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@ public function testIsValid()
1919
$this->assertEquals(true, $this->domain->isValid('example.com'));
2020
$this->assertEquals(true, $this->domain->isValid('subdomain.example.com'));
2121
$this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com'));
22-
$this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com'));
22+
$this->assertEquals(false, $this->domain->isValid('subdomain.example_app.com'));
2323
$this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com'));
24-
$this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com'));
24+
$this->assertEquals(false, $this->domain->isValid('subdomain_new.example.com'));
2525
$this->assertEquals(true, $this->domain->isValid('localhost'));
2626
$this->assertEquals(true, $this->domain->isValid('example.io'));
2727
$this->assertEquals(true, $this->domain->isValid('example.org'));
2828
$this->assertEquals(true, $this->domain->isValid('example.org'));
2929
$this->assertEquals(false, $this->domain->isValid(false));
30+
$this->assertEquals(false, $this->domain->isValid('api.appwrite.io.'));
31+
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
32+
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
33+
$this->assertEquals(false, $this->domain->isValid('api..appwrite.io'));
34+
$this->assertEquals(false, $this->domain->isValid('api-.appwrite.io'));
35+
$this->assertEquals(false, $this->domain->isValid('api.-appwrite.io'));
36+
$this->assertEquals(false, $this->domain->isValid('app write.io'));
37+
$this->assertEquals(false, $this->domain->isValid(' appwrite.io'));
38+
$this->assertEquals(false, $this->domain->isValid('appwrite.io '));
39+
$this->assertEquals(false, $this->domain->isValid('-appwrite.io'));
40+
$this->assertEquals(false, $this->domain->isValid('appwrite.io-'));
3041
$this->assertEquals(false, $this->domain->isValid('.'));
3142
$this->assertEquals(false, $this->domain->isValid('..'));
3243
$this->assertEquals(false, $this->domain->isValid(''));

tests/e2e/BaseTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,27 @@ public function testFile()
3535
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
3636
$this->assertEquals(204, $response['headers']['status-code']);
3737
}
38+
39+
public function testDoubleSlash()
40+
{
41+
$response = $this->client->call(Client::METHOD_GET, '//');
42+
$this->assertEquals(200, $response['headers']['status-code']);
43+
$this->assertEquals('Hello World!', $response['body']);
44+
45+
$response = $this->client->call(Client::METHOD_GET, '//path-404');
46+
$this->assertEquals(200, $response['headers']['status-code']);
47+
$this->assertEquals('Hello World!', $response['body']);
48+
49+
$response = $this->client->call(Client::METHOD_GET, '//value/123');
50+
$this->assertEquals(200, $response['headers']['status-code']);
51+
$this->assertEmpty($response['body']);
52+
53+
$response = $this->client->call(Client::METHOD_GET, '/value//123');
54+
$this->assertEquals(200, $response['headers']['status-code']);
55+
$this->assertEmpty($response['body']);
56+
57+
$response = $this->client->call(Client::METHOD_GET, '//value//123');
58+
$this->assertEquals(200, $response['headers']['status-code']);
59+
$this->assertEmpty($response['body']);
60+
}
3861
}

0 commit comments

Comments
 (0)