From 64442ab40dfed8239426665518a4aa01e58c4a7f Mon Sep 17 00:00:00 2001 From: woutersioen Date: Mon, 18 Oct 2021 09:20:36 +0200 Subject: [PATCH 1/2] Expect REMOTE_ADDR to be accepted as server variable --- README.md | 1 + tests/HtaccessClientTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 4f1398d..c062def 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ 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. diff --git a/tests/HtaccessClientTest.php b/tests/HtaccessClientTest.php index 38119b7..c8b08c9 100644 --- a/tests/HtaccessClientTest.php +++ b/tests/HtaccessClientTest.php @@ -130,6 +130,36 @@ public function it allows for passing an http user agent(): void $this->assertTrue($response->getLines()[0]->isSupported()); } + /** @test */ + public function it allows for passing an remote addr(): void + { + $client = new HtaccessClient( + new Client(), + new ServerRequestFactory() + ); + + $response = $client->test( + 'http://localhost', + 'RewriteCond %{REMOTE_ADDR} 10.0.0.1 + RewriteRule .* /example-page [L]', + ServerVariables::default()->with( + ServerVariable::REMOTE_ADDR, + '10.0.0.1' + ) + ); + + $this->assertEquals( + 'http://localhost/example-page', + $response->getOutputUrl() + ); + $this->assertEquals('RewriteCond %{REMOTE_ADDR} 10.0.0.1', $response->getLines()[0]->getLine()); + $this->assertStringContainsString('condition was met', $response->getLines()[0]->getMessage()); + $this->assertTrue($response->getLines()[0]->isMet()); + $this->assertTrue($response->getLines()[0]->isValid()); + $this->assertTrue($response->getLines()[0]->wasReached()); + $this->assertTrue($response->getLines()[0]->isSupported()); + } + /** @test */ public function it throws an exception when we pass an invalid url(): void { From 86665900eb72c9abf3c030ac425bacbed49f37a0 Mon Sep 17 00:00:00 2001 From: woutersioen Date: Mon, 18 Oct 2021 09:20:50 +0200 Subject: [PATCH 2/2] Accept REMOTE_ADDR as server variable --- src/ServerVariable.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ServerVariable.php b/src/ServerVariable.php index 6e4f0eb..270649f 100644 --- a/src/ServerVariable.php +++ b/src/ServerVariable.php @@ -9,10 +9,12 @@ final class ServerVariable public const HTTP_REFERER = 'HTTP_REFERER'; public const HTTP_USER_AGENT = 'HTTP_USER_AGENT'; public const SERVER_NAME = 'SERVER_NAME'; + public const REMOTE_ADDR = 'REMOTE_ADDR'; public const ALL = [ self::HTTP_REFERER, self::HTTP_USER_AGENT, self::SERVER_NAME, + self::REMOTE_ADDR, ]; }