mirror of
https://github.com/silverstripe/silverstripe-environmentcheck
synced 2024-10-22 17:05:40 +02:00
NEW: Refactor to provide unit tests for session check.
This commit is contained in:
parent
2674beb94a
commit
578c73e6ce
@ -22,3 +22,11 @@ SilverStripe\Core\Injector\Injector:
|
|||||||
class: SilverStripe\EnvironmentCheck\Checks\SolrIndexCheck
|
class: SilverStripe\EnvironmentCheck\Checks\SolrIndexCheck
|
||||||
URLCheck:
|
URLCheck:
|
||||||
class: SilverStripe\EnvironmentCheck\Checks\URLCheck
|
class: SilverStripe\EnvironmentCheck\Checks\URLCheck
|
||||||
|
EnvCheckClient:
|
||||||
|
factory: 'SilverStripe\EnvironmentCheck\Services\ClientFactory'
|
||||||
|
constructor:
|
||||||
|
timeout: 10.0
|
||||||
|
|
||||||
|
SilverStripe\EnvironmentCheck\Checks\SessionCheck:
|
||||||
|
dependencies:
|
||||||
|
client: '%$EnvCheckClient'
|
||||||
|
@ -2,10 +2,7 @@
|
|||||||
|
|
||||||
namespace SilverStripe\EnvironmentCheck\Checks;
|
namespace SilverStripe\EnvironmentCheck\Checks;
|
||||||
|
|
||||||
use SilverStripe\Control\Director;
|
|
||||||
use SilverStripe\Control\Controller;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use SilverStripe\Core\Config\Configurable;
|
|
||||||
use SilverStripe\EnvironmentCheck\Traits\Fetcher;
|
use SilverStripe\EnvironmentCheck\Traits\Fetcher;
|
||||||
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
||||||
|
|
||||||
@ -17,16 +14,8 @@ use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
|||||||
*/
|
*/
|
||||||
class SessionCheck implements EnvironmentCheck
|
class SessionCheck implements EnvironmentCheck
|
||||||
{
|
{
|
||||||
use Configurable;
|
|
||||||
use Fetcher;
|
use Fetcher;
|
||||||
|
|
||||||
/**
|
|
||||||
* URL to check
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $url;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up check with URL
|
* Set up check with URL
|
||||||
*
|
*
|
||||||
@ -35,11 +24,7 @@ class SessionCheck implements EnvironmentCheck
|
|||||||
*/
|
*/
|
||||||
public function __construct($url = '')
|
public function __construct($url = '')
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->setURL($url);
|
||||||
$this->clientConfig = [
|
|
||||||
'base_uri' => Director::absoluteBaseURL(),
|
|
||||||
'timeout' => 10.0,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,9 +34,9 @@ class SessionCheck implements EnvironmentCheck
|
|||||||
*/
|
*/
|
||||||
public function check()
|
public function check()
|
||||||
{
|
{
|
||||||
$response = $this->fetchResponse($this->url);
|
$response = $this->client->get($this->getURL());
|
||||||
$cookie = $this->getCookie($response);
|
$cookie = $this->getCookie($response);
|
||||||
$fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url);
|
$fullURL = $this->getURL();
|
||||||
|
|
||||||
if ($cookie) {
|
if ($cookie) {
|
||||||
return [
|
return [
|
||||||
|
49
src/Services/ClientFactory.php
Normal file
49
src/Services/ClientFactory.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\EnvironmentCheck\Services;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client as GuzzleClient;
|
||||||
|
use SilverStripe\Core\Injector\Factory;
|
||||||
|
use SilverStripe\Core\Config\Configurable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory class for creating HTTP client which are injected into some env check classes. Inject via YAML,
|
||||||
|
* arguments for Guzzle client can be supplied using "constructor" property or set as default_config.
|
||||||
|
*
|
||||||
|
* @see SilverStripe\EnvironmentCheck\Traits\Fetcher
|
||||||
|
*/
|
||||||
|
class ClientFactory implements Factory
|
||||||
|
{
|
||||||
|
use Configurable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default config for Guzzle client.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $default_config = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper to create a Guzzle client.
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function create($service, array $params = [])
|
||||||
|
{
|
||||||
|
return new GuzzleClient($this->getConfig($params));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge config provided from yaml with default config
|
||||||
|
*
|
||||||
|
* @param array $overrides
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getConfig(array $overrides)
|
||||||
|
{
|
||||||
|
return array_merge(
|
||||||
|
$this->config()->get('default_config'),
|
||||||
|
$overrides
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -2,45 +2,50 @@
|
|||||||
|
|
||||||
namespace SilverStripe\EnvironmentCheck\Traits;
|
namespace SilverStripe\EnvironmentCheck\Traits;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use SilverStripe\Control\Director;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple helper for fetching responses using Guzzle client.
|
* Simple helper for env checks which require HTTP clients.
|
||||||
*
|
*
|
||||||
* @package environmentcheck
|
* @package environmentcheck
|
||||||
*/
|
*/
|
||||||
trait Fetcher
|
trait Fetcher
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Configuration for the Guzzle client
|
* Client for making requests, set vi Injector.
|
||||||
*
|
*
|
||||||
* @var array
|
* @see SilverStripe\EnvironmentCheck\Services
|
||||||
|
*
|
||||||
|
* @var GuzzleHttp\Client
|
||||||
*/
|
*/
|
||||||
protected $clientConfig = [];
|
public $client = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges configuration arrays and returns the result
|
* Absolute URL for requests.
|
||||||
*
|
*
|
||||||
* @param array $extraConfig
|
* @var string
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
private function getClientConfig(array $extraConfig = [])
|
protected $url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set URL for requests.
|
||||||
|
*
|
||||||
|
* @param string $url Relative URL
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setURL($url)
|
||||||
{
|
{
|
||||||
return array_merge($this->clientConfig, $extraConfig);
|
$this->url = Director::absoluteURL($url);
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch a response for a URL using Guzzle client.
|
* Getter for URL
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @return string
|
||||||
* @param array|null $extraConfig Extra configuration
|
|
||||||
* @return ResponseInterface
|
|
||||||
*/
|
*/
|
||||||
public function fetchResponse(string $url, array $extraConfig = [])
|
public function getURL()
|
||||||
{
|
{
|
||||||
$config = $this->getClientConfig($extraConfig);
|
return $this->url;
|
||||||
$client = new Client($config);
|
|
||||||
return $client->get($url);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
76
tests/Checks/SessionCheckTest.php
Normal file
76
tests/Checks/SessionCheckTest.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\EnvironmentCheck\Tests\Checks;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\HandlerStack;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use GuzzleHttp\Handler\MockHandler;
|
||||||
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
||||||
|
use SilverStripe\EnvironmentCheck\Checks\SessionCheck;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test session checks.
|
||||||
|
*/
|
||||||
|
class SessionCheckTest extends SapphireTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var SilverStripe\EnvironmentCheck\Checks\SessionCheck
|
||||||
|
*/
|
||||||
|
public $sessionCheck = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a session check for use by tests.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->sessionCheck = new SessionCheck('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Env check reports error when session cookies are being set.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSessionSet()
|
||||||
|
{
|
||||||
|
// Create a mock and queue two responses.
|
||||||
|
$mock = new MockHandler([
|
||||||
|
new Response(200, ['Set-Cookie' => 'PHPSESSID:foo']),
|
||||||
|
new Response(200, ['Set-Cookie' => 'SECSESSID:bar'])
|
||||||
|
]);
|
||||||
|
|
||||||
|
$handler = HandlerStack::create($mock);
|
||||||
|
$client = new Client(['handler' => $handler]);
|
||||||
|
$this->sessionCheck->client = $client;
|
||||||
|
|
||||||
|
// Check for PHPSESSID
|
||||||
|
$this->assertContains(EnvironmentCheck::ERROR, $this->sessionCheck->check());
|
||||||
|
|
||||||
|
// Check for SECSESSID
|
||||||
|
$this->assertContains(EnvironmentCheck::ERROR, $this->sessionCheck->check());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Env check responds OK when no session cookies are set in response.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSessionNotSet()
|
||||||
|
{
|
||||||
|
// Create a mock and queue two responses.
|
||||||
|
$mock = new MockHandler([
|
||||||
|
new Response(200)
|
||||||
|
]);
|
||||||
|
|
||||||
|
$handler = HandlerStack::create($mock);
|
||||||
|
$client = new Client(['handler' => $handler]);
|
||||||
|
$this->sessionCheck->client = $client;
|
||||||
|
|
||||||
|
$this->assertContains(EnvironmentCheck::OK, $this->sessionCheck->check());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user