Skip to content

Commit

Permalink
Merge pull request #27 from madewithlove/2.x
Browse files Browse the repository at this point in the history
2.x
  • Loading branch information
jdrieghe authored Oct 8, 2021
2 parents 367925b + 63e6cbf commit bccffd3
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0']
php-versions: ['8.0', '8.1']
prefer-lowest: ['', '--prefer-lowest']
steps:
- uses: actions/checkout@v1
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ 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.
We currently support the following variables.
```
HTTP_REFERER
SERVER_NAME
```

Server variables can be passed to the `test()` method.
```php
$serverVariables = ServerVariables::default()->with(
ServerVariable::SERVER_NAME,
'example.com'
);

$response = $client->test(
'http://localhost',
'RewriteCond %{SERVER_NAME} example.com
RewriteRule .* /foo [R]',
$serverVariables
);
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "API client for the best htaccess tester in the world.",
"type": "package",
"require": {
"php": "^7.3|^8.0",
"php": "^8.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0"
},
Expand Down
25 changes: 7 additions & 18 deletions src/HtaccessClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@

final class HtaccessClient
{
/**
* @var ClientInterface
*/
private $httpClient;

/**
* @var ServerRequestFactoryInterface
*/
private $requestFactory;

public function __construct(ClientInterface $httpClient, ServerRequestFactoryInterface $requestFactory)
{
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
public function __construct(
private ClientInterface $httpClient,
private ServerRequestFactoryInterface $requestFactory
) {
}

/**
Expand All @@ -29,17 +19,16 @@ public function __construct(ClientInterface $httpClient, ServerRequestFactoryInt
public function test(
string $url,
string $htaccess,
?string $referrer = '',
?string $serverName = ''
?ServerVariables $serverVariables = null
): HtaccessResult {
$serverVariables = $serverVariables ?? ServerVariables::default();
$responseData = $this->request(
'POST',
'',
[
'url' => $url,
'htaccess' => $htaccess,
'referrer' => $referrer ?? '',
'serverName' => $serverName ?? '',
'serverVariables' => $serverVariables->toArray(),
]
);

Expand Down
31 changes: 4 additions & 27 deletions src/HtaccessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,13 @@
final class HtaccessResult
{
/**
* @var string
* @param ResultLine[] $lines
*/
private $outputUrl;

/**
* @var ResultLine[]
*/
private $lines = [];

/**
* @var int?
*/
private $outputStatusCode;

public function __construct(
string $outputUrl,
array $lines,
?int $outputStatusCode
private string $outputUrl,
private array $lines,
private ?int $outputStatusCode
) {
$this->outputUrl = $outputUrl;
$this->outputStatusCode = $outputStatusCode;

foreach ($lines as $line) {
$this->addLine($line);
}
}

public function getOutputUrl(): string
Expand All @@ -49,9 +31,4 @@ public function getOutputStatusCode(): ?int
{
return $this->outputStatusCode;
}

private function addLine(ResultLine $line): void
{
$this->lines[] = $line;
}
}
50 changes: 7 additions & 43 deletions src/ResultLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,14 @@

final class ResultLine
{
/**
* @var string
*/
private $line;

/**
* @var string
*/
private $message;

/**
* @var bool
*/
private $isMet;

/**
* @var bool
*/
private $isValid;

/**
* @var bool
*/
private $wasReached;

/**
* @var bool
*/
private $isSupported;

public function __construct(
string $line,
string $message,
bool $isMet,
bool $isValid,
bool $wasReached,
bool $isSupported
private string $line,
private string $message,
private bool $isMet,
private bool $isValid,
private bool $wasReached,
private bool $isSupported
) {
$this->line = $line;
$this->message = $message;
$this->isMet = $isMet;
$this->isValid = $isValid;
$this->wasReached = $wasReached;
$this->isSupported = $isSupported;
}

public function getLine(): string
Expand Down Expand Up @@ -77,6 +41,6 @@ public function wasReached(): bool

public function isSupported(): bool
{
return $this->wasReached;
return $this->isSupported;
}
}
16 changes: 16 additions & 0 deletions src/ServerVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Madewithlove;

final class ServerVariable
{
public const HTTP_REFERER = 'HTTP_REFERER';
public const SERVER_NAME = 'SERVER_NAME';

public const ALL = [
self::HTTP_REFERER,
self::SERVER_NAME,
];
}
47 changes: 47 additions & 0 deletions src/ServerVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Madewithlove;

use InvalidArgumentException;

class ServerVariables
{
private function __construct(
private array $variables
) {
}

public static function default(): self
{
return new 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;

return $clone;
}

public function has(string $optionName): bool
{
return isset($this->variables[$optionName]);
}

public function get(string $optionName): string
{
return $this->variables[$optionName] ?? '';
}

public function toArray(): array
{
return $this->variables;
}
}
11 changes: 3 additions & 8 deletions src/ShareResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

final class ShareResult
{
/**
* @var string
*/
private $shareUrl;

public function __construct(string $shareUrl)
{
$this->shareUrl = $shareUrl;
public function __construct(
private string $shareUrl
) {
}

public function getShareUrl(): string
Expand Down
11 changes: 8 additions & 3 deletions tests/HtaccessClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function it allows for passing a referrer(): void
'http://localhost',
'RewriteCond %{HTTP_REFERER} http://example.com
RewriteRule .* /example-page [L]',
'http://example.com'
ServerVariables::default()->with(
ServerVariable::HTTP_REFERER,
'http://example.com'
)
);

$this->assertEquals(
Expand All @@ -79,8 +82,10 @@ public function it allows for passing a server name(): void
'http://localhost',
'RewriteCond %{SERVER_NAME} example.com
RewriteRule .* /example-page [L]',
null,
'example.com'
ServerVariables::default()->with(
ServerVariable::SERVER_NAME,
'example.com'
)
);

$this->assertEquals(
Expand Down
34 changes: 34 additions & 0 deletions tests/ResultLineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Madewithlove;

use PHPUnit\Framework\TestCase;

class ResultLineTest extends TestCase
{
/** @test */
public function it correctly returns all properties(): void
{
$resultLine = new ResultLine('foo', 'bar', true, true, true, true);
$this->assertEquals('foo', $resultLine->getLine());
$this->assertEquals('bar', $resultLine->getMessage());
$this->assertTrue($resultLine->isMet());
$this->assertTrue($resultLine->isValid());
$this->assertTrue($resultLine->isSupported());
$this->assertTrue($resultLine->wasReached());

$resultLine = new ResultLine('foo', 'bar', false, true, true, true);
$this->assertFalse($resultLine->isMet());

$resultLine = new ResultLine('foo', 'bar', true, false, true, true);
$this->assertFalse($resultLine->isValid());

$resultLine = new ResultLine('foo', 'bar', true, true, false, true);
$this->assertFalse($resultLine->wasReached());

$resultLine = new ResultLine('foo', 'bar', true, true, true, false);
$this->assertFalse($resultLine->isSupported());
}
}
42 changes: 42 additions & 0 deletions tests/ServerVariablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Madewithlove;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

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
{
$serverVariables = ServerVariables::default()
->with(ServerVariable::HTTP_REFERER, 'example.com');

$this->assertTrue($serverVariables->has(ServerVariable::HTTP_REFERER));
$this->assertEquals('example.com', $serverVariables->get(ServerVariable::HTTP_REFERER));

$this->assertFalse($serverVariables->has(ServerVariable::SERVER_NAME));
$this->assertEquals('', $serverVariables->get(ServerVariable::SERVER_NAME));
}

/** @test */
public function it is immutable(): void
{
$original = ServerVariables::default();
$clone = $original->with(ServerVariable::SERVER_NAME, 'example.com');

$this->assertNotSame($original, $clone);
$this->assertFalse($original->has(ServerVariable::SERVER_NAME));
}
}

0 comments on commit bccffd3

Please sign in to comment.