Skip to content

Commit

Permalink
Merge pull request #31 from madewithlove/remote-addr
Browse files Browse the repository at this point in the history
Add support for the REMOTE_ADDR server variable
  • Loading branch information
WouterSioen authored Oct 19, 2021
2 parents af69117 + 8666590 commit cd05a2c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/ServerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
30 changes: 30 additions & 0 deletions tests/HtaccessClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit cd05a2c

Please sign in to comment.