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

Commit 23998e7

Browse files
committed
Merge pull request #41 from 0x46616c6b/guzzle-improve-requests
Improvement: allow to send not only get requests
2 parents a39a48e + 524cb14 commit 23998e7

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ $checkPageContent = new GuzzleHttpService(
448448
200,
449449
'<title>Hello World</title>'
450450
);
451+
452+
// Check that the post request returns the content
453+
$checkPageContent = new GuzzleHttpService(
454+
'www.example.com/user/update',
455+
array(),
456+
array(),
457+
200,
458+
'{"status":"success"}',
459+
'POST',
460+
array("post_field" => "post_value")
461+
);
451462
````
452463

453464
### Memcache

src/ZendDiagnostics/Check/GuzzleHttpService.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class GuzzleHttpService extends AbstractCheck
1414
{
1515
protected $url;
16+
protected $method;
17+
protected $body;
1618
protected $headers;
1719
protected $statusCode;
1820
protected $content;
@@ -25,14 +27,18 @@ class GuzzleHttpService extends AbstractCheck
2527
* @param int $statusCode The response status code to check
2628
* @param null $content The response content to check
2729
* @param ClientInterface $guzzle Instance of guzzle to use
30+
* @param string $method The method of the request
31+
* @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
2832
*/
29-
public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, ClientInterface $guzzle = null)
33+
public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, ClientInterface $guzzle = null, $method = 'GET', $body = null)
3034
{
3135
$this->url = $url;
3236
$this->headers = $headers;
3337
$this->options = $options;
3438
$this->statusCode = $statusCode;
3539
$this->content = $content;
40+
$this->method = $method;
41+
$this->body = $body;
3642

3743
if (!$guzzle) {
3844
$guzzle = new Client();
@@ -46,7 +52,7 @@ public function __construct($url, array $headers = array(), array $options = arr
4652
*/
4753
public function check()
4854
{
49-
$response = $this->guzzle->get($this->url, $this->headers, $this->options)->send();
55+
$response = $this->guzzle->createRequest($this->method, $this->url, $this->headers, $this->body, $this->options)->send();
5056

5157
if ($this->statusCode !== $statusCode = $response->getStatusCode()) {
5258
return new Failure("Status code {$this->statusCode} does not match {$statusCode} in response from {$this->url}");

tests/ZendDiagnosticsTest/GuzzleHttpServiceTest.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
1212
/**
1313
* @dataProvider checkProvider
1414
*/
15-
public function testCheck($content, $actualContent, $actualStatusCode, $resultClass)
15+
public function testCheck($content, $actualContent, $actualStatusCode, $resultClass, $method = 'GET', $body = null)
1616
{
1717
$check = new GuzzleHttpService(
1818
'http://www.example.com/foobar',
1919
array(),
2020
array(),
2121
200,
2222
$content,
23-
$this->getMockClient($actualStatusCode, $actualContent)
23+
$this->getMockClient($actualStatusCode, $actualContent),
24+
$method,
25+
$body
2426
);
2527
$result = $check->check();
2628

@@ -31,9 +33,17 @@ public function checkProvider()
3133
{
3234
return array(
3335
array(null, null, 200, 'ZendDiagnostics\Result\SuccessInterface'),
36+
array(null, null, 200, 'ZendDiagnostics\Result\SuccessInterface', 'POST', array('key' => 'value')),
37+
array(null, null, 200, 'ZendDiagnostics\Result\SuccessInterface', 'PUT'),
3438
array(null, null, 404, 'ZendDiagnostics\Result\FailureInterface'),
39+
array(null, null, 404, 'ZendDiagnostics\Result\FailureInterface', 'POST', array('key' => 'value')),
40+
array(null, null, 404, 'ZendDiagnostics\Result\FailureInterface', 'PUT'),
3541
array('foo', 'foobar', 200, 'ZendDiagnostics\Result\SuccessInterface'),
36-
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface')
42+
array('foo', 'foobar', 200, 'ZendDiagnostics\Result\SuccessInterface', 'POST', array('key' => 'value')),
43+
array('foo', 'foobar', 200, 'ZendDiagnostics\Result\SuccessInterface', 'PUT'),
44+
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface'),
45+
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface', 'POST', array('key' => 'value')),
46+
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface', 'PUT'),
3747
);
3848
}
3949

0 commit comments

Comments
 (0)