Skip to content

Commit

Permalink
Merge pull request #32 from madewithlove/more-server-variable-support
Browse files Browse the repository at this point in the history
Add support for all kinds of custom server variables
  • Loading branch information
WouterSioen authored Oct 29, 2021
2 parents cd05a2c + 5bd9f68 commit 068bf1a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 15 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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'
);

Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: max
paths:
- src
- tests
9 changes: 8 additions & 1 deletion src/HtaccessClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,22 @@ function (array $line) {
);
}

/**
* @param array<string,mixed> $requestData
* @return array<string,mixed>
*/
private function request(string $method, string $endpoint = '', array $requestData = []): array
{
$request = $this->requestFactory->createServerRequest(
$method,
'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')
Expand Down
3 changes: 3 additions & 0 deletions src/HtaccessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class HtaccessException extends Exception
{
/**
* @param array<int,array{field: string, message: string}> $errors
*/
public static function fromApiErrors(array $errors): self
{
$errorMessages = array_map(
Expand Down
3 changes: 3 additions & 0 deletions src/ServerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 7 additions & 1 deletion src/ServerVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class ServerVariables
{
/**
* @param array<string, string> $variables
*/
private function __construct(
private array $variables
) {
Expand All @@ -20,7 +23,7 @@ public static function default(): self

public function with(string $optionName, string $value): self
{
if (!in_array($optionName, ServerVariable::ALL)) {
if (!preg_match('/^[a-zA-Z1-9_\-:]+$/', $optionName)) {
throw new InvalidArgumentException('Unsupported server variable: ' . $optionName);
}

Expand All @@ -40,6 +43,9 @@ public function get(string $optionName): string
return $this->variables[$optionName] ?? '';
}

/**
* @return array<string, string>
*/
public function toArray(): array
{
return $this->variables;
Expand Down
21 changes: 21 additions & 0 deletions tests/HtaccessClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
41 changes: 37 additions & 4 deletions tests/ServerVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,45 @@

class ServerVariablesTest extends TestCase
{
/** @test */
public function it only allows supported variables(): void
/**
* @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: foo');
ServerVariables::default()->with('foo', 'bar');
$this->expectExceptionMessage('Unsupported server variable: ' . $serverVariableName);
ServerVariables::default()->with($serverVariableName, 'bar');
}

/**
* @return array<int, array<string>>
*/
public function providesInvalidServerVariableNames(): array
{
return [
[
'PERCENTAGE_NOT_ALLOWED%',
],
[
'CURLY_BRACE_NOT_ALLOWED{',
],
[
'CURLY_BRACE_NOT_ALLOWED}',
],
[
'DOLLAR_NOT_ALLOWED$',
],
[
'CARRET_NOT_ALLOWED^',
],
[
'%ALSO_NOT_ALLOWED_IN_BEGINNING_OF_LINE',
],
[
'ALSO_NOT_ALLOWED_IN_%MIDDLE_OF_LINE',
],
];
}

/** @test */
Expand Down

0 comments on commit 068bf1a

Please sign in to comment.