From 838ebb1fdbdfdf23714283cefb8c335fd20a745c Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:11:46 +0200 Subject: [PATCH 01/11] Expect to be able to use custom server variables --- tests/HtaccessClientTest.php | 21 +++++++++++++++++++++ tests/ServerVariablesTest.php | 8 -------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/HtaccessClientTest.php b/tests/HtaccessClientTest.php index c8b08c9..f3cf670 100644 --- a/tests/HtaccessClientTest.php +++ b/tests/HtaccessClientTest.php @@ -302,4 +302,25 @@ public function it can share a test run with a server name(): void $sharedResult->getOutputUrl() ); } + + /** @test */ + public function it accepts custom server variables as well(): void + { + $client = new HtaccessClient( + new Client(), + new ServerRequestFactory() + ); + + $response = $client->test( + 'http://localhost', + 'RewriteCond %{CUSTOM_VARIABLE} example.com + RewriteRule .* /example-page [L]', + ServerVariables::default()->with('CUSTOM_VARIABLE', 'example.com') + ); + + $this->assertEquals( + 'http://localhost/example-page', + $response->getOutputUrl() + ); + } } diff --git a/tests/ServerVariablesTest.php b/tests/ServerVariablesTest.php index dcba461..deedc56 100644 --- a/tests/ServerVariablesTest.php +++ b/tests/ServerVariablesTest.php @@ -9,14 +9,6 @@ class ServerVariablesTest extends TestCase { - /** @test */ - public function it only allows supported variables(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Unsupported server variable: foo'); - ServerVariables::default()->with('foo', 'bar'); - } - /** @test */ public function it holds supported server variables(): void { From 5565598b36ba7a2ef0a53f5298fb818460862d0f Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:12:03 +0200 Subject: [PATCH 02/11] Remove limit on server variables that we accept You can now pass in everything you want as server variable. --- src/ServerVariables.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ServerVariables.php b/src/ServerVariables.php index 6219bdd..16cfcfc 100644 --- a/src/ServerVariables.php +++ b/src/ServerVariables.php @@ -20,10 +20,6 @@ public static function default(): self public function with(string $optionName, string $value): self { - if (!in_array($optionName, ServerVariable::ALL)) { - throw new InvalidArgumentException('Unsupported server variable: ' . $optionName); - } - $clone = clone $this; $clone->variables[$optionName] = $value; From fe2e1f791d9389f40e4c666d6845ac66bb960c6d Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:18:57 +0200 Subject: [PATCH 03/11] Add back some validation to the server variable names --- src/ServerVariables.php | 4 ++++ tests/ServerVariablesTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/ServerVariables.php b/src/ServerVariables.php index 16cfcfc..8b365e1 100644 --- a/src/ServerVariables.php +++ b/src/ServerVariables.php @@ -20,6 +20,10 @@ public static function default(): self public function with(string $optionName, string $value): self { + if (!preg_match('/^[a-zA-Z1-9_\-:]+$/', $optionName)) { + throw new InvalidArgumentException('Unsupported server variable: ' . $optionName); + } + $clone = clone $this; $clone->variables[$optionName] = $value; diff --git a/tests/ServerVariablesTest.php b/tests/ServerVariablesTest.php index deedc56..db4a89c 100644 --- a/tests/ServerVariablesTest.php +++ b/tests/ServerVariablesTest.php @@ -9,6 +9,38 @@ class ServerVariablesTest extends TestCase { + /** + * @test + * @dataProvider providesInvalidServerVariableNames + */ + public function it does not support variables using unsupported characters(string $serverVariableName): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Unsupported server variable: ' . $serverVariableName); + ServerVariables::default()->with($serverVariableName, 'bar'); + } + + public function providesInvalidServerVariableNames(): array + { + return [ + [ + 'PERCENTAGE_NOT_ALLOWED%', + ], + [ + 'CURLY_BRACE_NOT_ALLOWED{', + ], + [ + 'CURLY_BRACE_NOT_ALLOWED}', + ], + [ + 'DOLLAR_NOT_ALLOWED$', + ], + [ + 'CARRET_NOT_ALLOWED^', + ], + ]; + } + /** @test */ public function it holds supported server variables(): void { From 39df761a085e954809d25565e073c7209c42f0c1 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:19:40 +0200 Subject: [PATCH 04/11] Adapt documentation to allow all server variables --- README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c062def..8086016 100644 --- a/README.md +++ b/README.md @@ -52,19 +52,13 @@ Test are stopped, a redirect will be made with status code 302" ### Server variables -Htaccess Tester supports passing a predefined set of server variables to be evaluated by the rewrite rules. +Htaccess Tester supports passing a server variables to be evaluated by the rewrite rules. We currently support the following variables. -``` -HTTP_REFERER -HTTP_USER_AGENT -SERVER_NAME -REMOTE_ADDR -``` Server variables can be passed to the `test()` and `share()` methods. ```php $serverVariables = ServerVariables::default()->with( - ServerVariable::SERVER_NAME, + 'SERVER_NAME', 'example.com' ); From 4f77fe3a9d14e45143e9b3792bea8071eb14bdab Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:24:54 +0200 Subject: [PATCH 05/11] Make infection happy by adding more test cases for regex --- tests/ServerVariablesTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ServerVariablesTest.php b/tests/ServerVariablesTest.php index db4a89c..0dbf303 100644 --- a/tests/ServerVariablesTest.php +++ b/tests/ServerVariablesTest.php @@ -38,6 +38,12 @@ public function providesInvalidServerVariableNames(): array [ 'CARRET_NOT_ALLOWED^', ], + [ + '%ALSO_NOT_ALLOWED_IN_BEGINNING_OF_LINE', + ], + [ + 'ALSO_NOT_ALLOWED_IN_%MIDDLE_OF_LINE', + ], ]; } From 4dd37c59611fd858253701d7b25311a1795c0323 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:35:43 +0200 Subject: [PATCH 06/11] Bump PHPStan level --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 46b0f47..63aadba 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 5 + level: 6 paths: - src - tests From e8ba4aa43e423e8e945bc957640178b7349ed200 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:36:03 +0200 Subject: [PATCH 07/11] Add more phpdocs to make PHPStan happy --- src/HtaccessClient.php | 4 ++++ src/HtaccessException.php | 3 +++ src/ServerVariables.php | 6 ++++++ tests/ServerVariablesTest.php | 3 +++ 4 files changed, 16 insertions(+) diff --git a/src/HtaccessClient.php b/src/HtaccessClient.php index beb419c..bb7144b 100644 --- a/src/HtaccessClient.php +++ b/src/HtaccessClient.php @@ -102,6 +102,10 @@ function (array $line) { ); } + /** + * @param array $requestData + * @return array + */ private function request(string $method, string $endpoint = '', array $requestData = []): array { $request = $this->requestFactory->createServerRequest( diff --git a/src/HtaccessException.php b/src/HtaccessException.php index 48270f1..b4ca1bc 100644 --- a/src/HtaccessException.php +++ b/src/HtaccessException.php @@ -6,6 +6,9 @@ class HtaccessException extends Exception { + /** + * @param array $errors + */ public static function fromApiErrors(array $errors): self { $errorMessages = array_map( diff --git a/src/ServerVariables.php b/src/ServerVariables.php index 8b365e1..313409b 100644 --- a/src/ServerVariables.php +++ b/src/ServerVariables.php @@ -8,6 +8,9 @@ class ServerVariables { + /** + * @param array $variables + */ private function __construct( private array $variables ) { @@ -40,6 +43,9 @@ public function get(string $optionName): string return $this->variables[$optionName] ?? ''; } + /** + * @return array + */ public function toArray(): array { return $this->variables; diff --git a/tests/ServerVariablesTest.php b/tests/ServerVariablesTest.php index 0dbf303..34f39b4 100644 --- a/tests/ServerVariablesTest.php +++ b/tests/ServerVariablesTest.php @@ -20,6 +20,9 @@ public function it does not support variables using unsupported character ServerVariables::default()->with($serverVariableName, 'bar'); } + /** + * @return array> + */ public function providesInvalidServerVariableNames(): array { return [ From 9534966f768962c964df69c942c69c051f81e69c Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:37:20 +0200 Subject: [PATCH 08/11] Bump PHPStan to level 7 json_encode can theoretically return `false` if you have data that cannot be json serialized. Let's not take that scenario into account since you have to do really weird stuff to get json_encode to return this. --- phpstan.neon | 2 +- src/HtaccessClient.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 63aadba..7c87780 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 6 + level: 7 paths: - src - tests diff --git a/src/HtaccessClient.php b/src/HtaccessClient.php index bb7144b..a7a394d 100644 --- a/src/HtaccessClient.php +++ b/src/HtaccessClient.php @@ -113,8 +113,11 @@ private function request(string $method, string $endpoint = '', array $requestDa 'https://htaccess.madewithlove.be/api' . $endpoint ); + /** @var string $requestBody */ + $requestBody = json_encode($requestData); + $body = $request->getBody(); - $body->write(json_encode($requestData)); + $body->write($requestBody); $request = $request ->withHeader('Content-Type', 'application/json') From f0300e0f00e5a6a914c6d5ee62d03fd170ee3af7 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Thu, 28 Oct 2021 19:38:23 +0200 Subject: [PATCH 09/11] Bump PHPStan to the max level --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 7c87780..1494f51 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 7 + level: max paths: - src - tests From 4514d34858e7af6dd9ea7f8f94a220825c09ec63 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 29 Oct 2021 07:39:39 +0200 Subject: [PATCH 10/11] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8086016..5a0348a 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Test are stopped, a redirect will be made with status code 302" ### Server variables -Htaccess Tester supports passing a server variables to be evaluated by the rewrite rules. +Htaccess Tester supports passing server variables to be evaluated by the rewrite rules. We currently support the following variables. Server variables can be passed to the `test()` and `share()` methods. From 5bd9f6854ecc966600ba67a96ed397d825891fc9 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Fri, 29 Oct 2021 08:55:53 +0200 Subject: [PATCH 11/11] Deprecate the server variable class Will be removed in v3 --- src/ServerVariable.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ServerVariable.php b/src/ServerVariable.php index 270649f..23b94c4 100644 --- a/src/ServerVariable.php +++ b/src/ServerVariable.php @@ -4,6 +4,9 @@ namespace Madewithlove; +/** + * @deprecated use regular strings instead of the constants included in this class + */ final class ServerVariable { public const HTTP_REFERER = 'HTTP_REFERER';