diff --git a/_config/injector.yml b/_config/injector.yml index 2b35577..6885610 100644 --- a/_config/injector.yml +++ b/_config/injector.yml @@ -22,3 +22,11 @@ SilverStripe\Core\Injector\Injector: class: SilverStripe\EnvironmentCheck\Checks\SolrIndexCheck URLCheck: class: SilverStripe\EnvironmentCheck\Checks\URLCheck + EnvCheckClient: + factory: 'SilverStripe\EnvironmentCheck\Services\ClientFactory' + constructor: + timeout: 10.0 + +SilverStripe\EnvironmentCheck\Checks\SessionCheck: + dependencies: + client: '%$EnvCheckClient' diff --git a/src/Checks/SessionCheck.php b/src/Checks/SessionCheck.php index d7779da..47a95bd 100644 --- a/src/Checks/SessionCheck.php +++ b/src/Checks/SessionCheck.php @@ -2,10 +2,7 @@ namespace SilverStripe\EnvironmentCheck\Checks; -use SilverStripe\Control\Director; -use SilverStripe\Control\Controller; use Psr\Http\Message\ResponseInterface; -use SilverStripe\Core\Config\Configurable; use SilverStripe\EnvironmentCheck\Traits\Fetcher; use SilverStripe\EnvironmentCheck\EnvironmentCheck; @@ -17,16 +14,8 @@ use SilverStripe\EnvironmentCheck\EnvironmentCheck; */ class SessionCheck implements EnvironmentCheck { - use Configurable; use Fetcher; - /** - * URL to check - * - * @var string - */ - protected $url; - /** * Set up check with URL * @@ -35,11 +24,7 @@ class SessionCheck implements EnvironmentCheck */ public function __construct($url = '') { - $this->url = $url; - $this->clientConfig = [ - 'base_uri' => Director::absoluteBaseURL(), - 'timeout' => 10.0, - ]; + $this->setURL($url); } /** @@ -49,9 +34,9 @@ class SessionCheck implements EnvironmentCheck */ public function check() { - $response = $this->fetchResponse($this->url); + $response = $this->client->get($this->getURL()); $cookie = $this->getCookie($response); - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); + $fullURL = $this->getURL(); if ($cookie) { return [ diff --git a/src/Services/ClientFactory.php b/src/Services/ClientFactory.php new file mode 100644 index 0000000..ef066f0 --- /dev/null +++ b/src/Services/ClientFactory.php @@ -0,0 +1,49 @@ +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 + ); + } +} diff --git a/src/Traits/Fetcher.php b/src/Traits/Fetcher.php index afba398..60dd5c3 100644 --- a/src/Traits/Fetcher.php +++ b/src/Traits/Fetcher.php @@ -2,45 +2,50 @@ namespace SilverStripe\EnvironmentCheck\Traits; -use GuzzleHttp\Client; -use Psr\Http\Message\ResponseInterface; +use SilverStripe\Control\Director; /** - * Simple helper for fetching responses using Guzzle client. + * Simple helper for env checks which require HTTP clients. * * @package environmentcheck */ 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 - * @return array + * @var string */ - 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 - * @param array|null $extraConfig Extra configuration - * @return ResponseInterface + * @return string */ - public function fetchResponse(string $url, array $extraConfig = []) + public function getURL() { - $config = $this->getClientConfig($extraConfig); - $client = new Client($config); - return $client->get($url); + return $this->url; } } diff --git a/tests/Checks/SessionCheckTest.php b/tests/Checks/SessionCheckTest.php new file mode 100644 index 0000000..a84bcef --- /dev/null +++ b/tests/Checks/SessionCheckTest.php @@ -0,0 +1,76 @@ +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()); + } +}