Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit b8b34f1

Browse files
committed
Merge pull request #31 from kbond/guzzle-http-service
added GuzzleHttpService Check
2 parents cb31a54 + 8eafef1 commit b8b34f1

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ It currently ships with the following Diagnostic Checks:
1717
* [DiskFree](#diskfree) - check there's enough free space on given path,
1818
* [ExtensionLoaded](#extensionloaded) - make sure extension is loaded,
1919
* [HttpService](#httpservice) - check if given http host is responding,
20+
* [GuzzleHttpService](#guzzlehttpservice) - check if given http host is responding using Guzzle,
2021
* [Memcache](#memcache) - check if memcache extension is loaded and given server is reachable,
2122
* [PhpVersion](#phpversion) - make sure that PHP version matches constraint,
2223
* [PhpFlag](#phpflag) - make sure that given PHP flag (feature) is turned on or off.
@@ -418,6 +419,35 @@ $checkPageContent = new HttpService(
418419
);
419420
````
420421

422+
### GuzzleHttpService
423+
424+
Attempt connection to given HTTP host or IP address and try to load a web page using
425+
[Guzzle](http://guzzle3.readthedocs.org/en/latest/). The check also supports checking response
426+
codes and page contents.
427+
428+
````php
429+
<?php
430+
use ZendDiagnostics\Check\GuzzleHttpService;
431+
432+
// Try to connect to google.com
433+
$checkGoogle = new GuzzleHttpService('www.google.com');
434+
435+
// Check port 8080 on localhost
436+
$checkLocal = new GuzzleHttpService('127.0.0.1:8080');
437+
438+
// Check that the page exists (response code must equal 200)
439+
$checkPage = new GuzzleHttpService('www.example.com/some/page.html');
440+
441+
// Check page content
442+
$checkPageContent = new GuzzleHttpService(
443+
'www.example.com/some/page.html',
444+
array(),
445+
array(),
446+
200,
447+
'<title>Hello World</title>'
448+
);
449+
````
450+
421451
### Memcache
422452

423453
Attempt to connect to given Memcache server.

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
"require-dev": {
2121
"zendframework/zend-loader" : "2.*",
2222
"sensiolabs/security-checker": "1.3.*@dev",
23-
"symfony/yaml": "v2.3.11"
23+
"symfony/yaml": "v2.3.11",
24+
"guzzle/http" : "3.*",
25+
"guzzle/plugin-mock" : "3.*"
2426
},
2527
"suggest" : {
2628
"ext-bcmath" : "Required by Check\\CpuPerformance",
2729
"sensiolabs/security-checker": "Required by Check\\SecurityAdvisory",
28-
"symfony/yaml": "Required by Check\\YamlFile"
30+
"symfony/yaml": "Required by Check\\YamlFile",
31+
"guzzle/http": "Required by Check\\GuzzleHttpService"
2932
},
3033
"extra": {
3134
"branch-alias": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use Guzzle\Http\Client;
9+
use Guzzle\Http\ClientInterface;
10+
use ZendDiagnostics\Result\Failure;
11+
use ZendDiagnostics\Result\Success;
12+
13+
class GuzzleHttpService extends AbstractCheck
14+
{
15+
protected $url;
16+
protected $headers;
17+
protected $statusCode;
18+
protected $content;
19+
protected $guzzle;
20+
21+
/**
22+
* @param string $url The absolute url to check
23+
* @param array $headers An array of headers used to create the request
24+
* @param array $options An array of guzzle options used to create the request
25+
* @param int $statusCode The response status code to check
26+
* @param null $content The response content to check
27+
* @param ClientInterface $guzzle Instance of guzzle to use
28+
*/
29+
public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, ClientInterface $guzzle = null)
30+
{
31+
$this->url = $url;
32+
$this->headers = $headers;
33+
$this->options = $options;
34+
$this->statusCode = $statusCode;
35+
$this->content = $content;
36+
37+
if (!$guzzle) {
38+
$guzzle = new Client();
39+
}
40+
41+
$this->guzzle = $guzzle;
42+
}
43+
44+
/**
45+
* @see ZendDiagnostics\CheckInterface::check()
46+
*/
47+
public function check()
48+
{
49+
$response = $this->guzzle->get($this->url, $this->headers, $this->options)->send();
50+
51+
if ($this->statusCode !== $statusCode = $response->getStatusCode()) {
52+
return new Failure("Status code {$this->statusCode} does not match {$statusCode} in response from {$this->url}");
53+
}
54+
55+
if ($this->content && (false === strpos($response->getBody(true), $this->content))) {
56+
return new Failure("Content {$this->content} not found in response from {$this->url}");
57+
}
58+
59+
return new Success();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace ZendDiagnosticsTest;
4+
5+
use Guzzle\Http\Client;
6+
use Guzzle\Http\Message\Response;
7+
use Guzzle\Plugin\Mock\MockPlugin;
8+
use ZendDiagnostics\Check\GuzzleHttpService;
9+
10+
class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @dataProvider checkProvider
14+
*/
15+
public function testCheck($content, $actualContent, $actualStatusCode, $resultClass)
16+
{
17+
$check = new GuzzleHttpService(
18+
'http://www.example.com/foobar',
19+
array(),
20+
array(),
21+
200,
22+
$content,
23+
$this->getMockClient($actualStatusCode, $actualContent)
24+
);
25+
$result = $check->check();
26+
27+
$this->assertInstanceOf($resultClass, $result);
28+
}
29+
30+
public function checkProvider()
31+
{
32+
return array(
33+
array(null, null, 200, 'ZendDiagnostics\Result\SuccessInterface'),
34+
array(null, null, 404, 'ZendDiagnostics\Result\FailureInterface'),
35+
array('foo', 'foobar', 200, 'ZendDiagnostics\Result\SuccessInterface'),
36+
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface')
37+
);
38+
}
39+
40+
private function getMockClient($statusCode = 200, $content = null)
41+
{
42+
$plugin = new MockPlugin();
43+
$plugin->addResponse(new Response($statusCode, null, $content));
44+
45+
$client = new Client(null, array(
46+
'request.options' => array(
47+
'exceptions' => false
48+
)
49+
));
50+
$client->addSubscriber($plugin);
51+
52+
return $client;
53+
}
54+
}

0 commit comments

Comments
 (0)