$serviceBuilder
$serviceBuilder :
Base testcase class for all Guzzle testcases.
getServer() : \Guzzle\Tests\Http\Server
Get the global server object used throughout the unit tests of Guzzle
setServiceBuilder(\Guzzle\Service\Builder\ServiceBuilderInterface $builder)
Set the service builder to use for tests
\Guzzle\Service\Builder\ServiceBuilderInterface | $builder | Service builder |
getServiceBuilder() : \Guzzle\Service\Builder\ServiceBuilder
Get a service builder object that can be used throughout the service tests
getWildcardObserver(\Guzzle\Common\HasDispatcherInterface $hasDispatcher) : \Guzzle\Tests\Mock\MockObserver
Get a wildcard observer for an event dispatcher
\Guzzle\Common\HasDispatcherInterface | $hasDispatcher |
setMockBasePath(string $path) : \Guzzle\Tests\GuzzleTestCase
Set the mock response base path
string | $path | Path to mock response folder |
addMockedRequest(\Guzzle\Http\Message\RequestInterface $request) : self
Mark a request as being mocked
\Guzzle\Http\Message\RequestInterface | $request |
getMockResponse(string $path) : \Guzzle\Http\Message\Response
Get a mock response for a client by mock file name
string | $path | Relative path to the mock response file |
setMockResponse(\Guzzle\Http\Client $client, string $paths) : \Guzzle\Plugin\Mock\MockPlugin
Set a mock response from a mock file on the next client request.
This method assumes that mock response files are located under the Command/Mock/ directory of the Service being tested (e.g. Unfuddle/Command/Mock/). A mock response is added to the next request sent by the client.
\Guzzle\Http\Client | $client | Client object to modify |
string | $paths | Path to files within the Mock folder of the service |
returns the created mock plugin
compareHeaders(array $filteredHeaders, array $actualHeaders) : array|boolean
Compare HTTP headers and use special markup to filter values A header prefixed with '!' means it must not exist A header prefixed with '_' means it must be ignored A header value of '*' means anything after the * will be ignored
array | $filteredHeaders | Array of special headers |
array | $actualHeaders | Array of headers to check against |
Returns an array of the differences or FALSE if none
hasSubscriber(\Guzzle\Common\HasDispatcherInterface $dispatcher, \Symfony\Component\EventDispatcher\EventSubscriberInterface $subscriber) : boolean
Check if an event dispatcher has a subscriber
\Guzzle\Common\HasDispatcherInterface | $dispatcher | |
\Symfony\Component\EventDispatcher\EventSubscriberInterface | $subscriber |
<?php
namespace Guzzle\Tests\Plugin\Cookie;
use Guzzle\Common\Event;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cookie\CookiePlugin;
/**
* @group server
* @covers Guzzle\Plugin\Cookie\CookiePlugin
*/
class CookiePluginTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testExtractsAndStoresCookies()
{
$response = new Response(200);
$mock = $this->getMockBuilder('Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar')
->setMethods(array('addCookiesFromResponse'))
->getMock();
$mock->expects($this->exactly(1))
->method('addCookiesFromResponse')
->with($response);
$plugin = new CookiePlugin($mock);
$plugin->onRequestSent(new Event(array(
'response' => $response
)));
}
public function testAddsCookiesToRequests()
{
$cookie = new Cookie(array(
'name' => 'foo',
'value' => 'bar'
));
$mock = $this->getMockBuilder('Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar')
->setMethods(array('getMatchingCookies'))
->getMock();
$mock->expects($this->once())
->method('getMatchingCookies')
->will($this->returnValue(array($cookie)));
$plugin = new CookiePlugin($mock);
$client = new Client();
$client->getEventDispatcher()->addSubscriber($plugin);
$request = $client->get('http://www.example.com');
$plugin->onRequestBeforeSend(new Event(array(
'request' => $request
)));
$this->assertEquals('bar', $request->getCookie('foo'));
}
public function testCookiesAreExtractedFromRedirectResponses()
{
$plugin = new CookiePlugin(new ArrayCookieJar());
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 302 Moved Temporarily\r\n" .
"Set-Cookie: test=583551; expires=Wednesday, 23-Mar-2050 19:49:45 GMT; path=/\r\n" .
"Location: /redirect\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$client->getEventDispatcher()->addSubscriber($plugin);
$client->get()->send();
$request = $client->get();
$request->send();
$this->assertEquals('test=583551', $request->getHeader('Cookie'));
$requests = $this->getServer()->getReceivedRequests(true);
// Confirm subsequent requests have the cookie.
$this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
// Confirm the redirected request has the cookie.
$this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
}
public function testCookiesAreNotAddedWhenParamIsSet()
{
$jar = new ArrayCookieJar();
$plugin = new CookiePlugin($jar);
$jar->add(new Cookie(array(
'domain' => 'example.com',
'path' => '/',
'name' => 'test',
'value' => 'hi',
'expires' => time() + 3600
)));
$client = new Client('http://example.com');
$client->getEventDispatcher()->addSubscriber($plugin);
// Ensure that it is normally added
$request = $client->get();
$request->setResponse(new Response(200), true);
$request->send();
$this->assertEquals('hi', $request->getCookie('test'));
// Now ensure that it is not added
$request = $client->get();
$request->getParams()->set('cookies.disable', true);
$request->setResponse(new Response(200), true);
$request->send();
$this->assertNull($request->getCookie('test'));
}
public function testProvidesCookieJar()
{
$jar = new ArrayCookieJar();
$plugin = new CookiePlugin($jar);
$this->assertSame($jar, $plugin->getCookieJar());
}
public function testEscapesCookieDomains()
{
$cookie = new Cookie(array('domain' => '/foo/^$[A-Z]+/'));
$this->assertFalse($cookie->matchesDomain('foo'));
}
}