/** * @covers Aws\Common\Signature\SignatureListener */ public function testSignsRequestsProperly() { $request = new Request('GET', 'http://www.example.com'); $request->getEventDispatcher(); $credentials = new Credentials('a', 'b'); $signature = $this->getMock('Aws\\Common\\Signature\\SignatureV4'); // Ensure that signing the request occurred once with the correct args $signature->expects($this->once())->method('signRequest')->with($this->equalTo($request), $this->equalTo($credentials)); $listener = new SignatureListener($credentials, $signature); // Create a mock event $event = new Event(array('request' => $request)); $listener->onRequestBeforeSend($event); }
/** * Function to add an error handler to the request. This could be used * * @param int $httpStatus HTTP status to error handle (-1 matches all) * @param Closure $func Function to call on error * @param boolean $stopPropagation Boolean as to wether to stop event propagation */ public function addErrorHandler($httpStatus, \Closure $func, $stopPropagation = true) { $request = $this->request; $this->request->getEventDispatcher()->addListener('request.error', function (\Guzzle\Common\Event $event) use($httpStatus, $stopPropagation, $func, $request) { if ($httpStatus == -1 || $event['response']->getStatusCode() == $httpStatus) { // Stop other events from firing if needed if ($stopPropagation) { $event->stopPropagation(); } //execute the callback $func($event, $request); } }); }
/** * @covers Guzzle\Http\Message\Request::__clone */ public function testClonedRequestsUseNewInternalState() { $p = new AsyncPlugin(); $this->request->getEventDispatcher()->addSubscriber($p); $h = $this->request->getHeader('Host'); $r = clone $this->request; $this->assertEquals(RequestInterface::STATE_NEW, $r->getState()); $this->assertNotSame($r->getQuery(), $this->request->getQuery()); $this->assertNotSame($r->getCurlOptions(), $this->request->getCurlOptions()); $this->assertNotSame($r->getEventDispatcher(), $this->request->getEventDispatcher()); $this->assertEquals($r->getHeaders(), $this->request->getHeaders()); $this->assertNotSame($h, $r->getHeader('Host')); $this->assertNotSame($r->getParams(), $this->request->getParams()); $this->assertTrue($this->request->getEventDispatcher()->hasListeners('request.sent')); }
public function testRequestBeforeSendIncludesContentLengthHeaderIfEmptyBody() { $this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); $request = new Request('PUT', $this->getServer()->getUrl()); $that = $this; $request->getEventDispatcher()->addListener('request.before_send', function ($event) use($that) { $that->assertEquals(0, $event['request']->getHeader('Content-Length')); }); $this->multi->add($request); $this->multi->send(); }
public function onError($callable, $priority = 0) { $this->_request->getEventDispatcher()->addListener('request.error', $callable, $priority); return $this; }
/** * @covers Guzzle\Http\Plugin\CookiePlugin */ public function testAddsCookiesToRequests() { ArrayCookieJarTest::addCookies($this->storage); $this->storage->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('secure', 'sec'), 'expires' => Guzzle::getHttpDate('+1 day'), 'secure' => true)); // Add a cookie that is only set on a specific port, so it wont be // added to the following requests $this->storage->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('test', 'port'), 'expires' => Guzzle::getHttpDate('+1 day'), 'secure' => false, 'port' => array(8192))); $request1 = new Request('GET', 'https://a.y.example.com/acme/'); $request1->setClient(new Client()); $request2 = new Request('GET', 'https://a.y.example.com/acme/'); $request2->setClient(new Client()); $request3 = new Request('GET', 'http://a.y.example.com/acme/'); $request3->setClient(new Client()); $request4 = new Request('GET', 'http://a.y.example.com/acme/'); $request4->setClient(new Client()); $request1->getEventDispatcher()->addSubscriber($this->plugin); $request2->getEventDispatcher()->addSubscriber($this->plugin); $request3->getEventDispatcher()->addSubscriber($this->plugin); $request4->getEventDispatcher()->addSubscriber($this->plugin); // Set a secure cookie $response1 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: a=b; c=d; Max-Age=86400; domain=.example.com; secure;\r\n\r\n"); // Set a regular cookie $response2 = Response::fromMessage("HTTP/1.1 200 OK\r\nSet-Cookie: e=f h; discard; domain=.example.com;\r\n\r\n"); $response3 = Response::fromMessage("HTTP/1.1 200 OK\r\n\r\n"); $request1->setResponse($response1, true); $request2->setResponse($response2, true); $request3->setResponse($response3, true); $request1->send(); $request2->send(); $request3->send(); $this->assertEquals('muppet=cookie_monster;secure=sec', (string) $request1->getCookie()); $this->assertEquals('muppet=cookie_monster;secure=sec;a=b;c=d', (string) $request2->getCookie()); $this->assertEquals('muppet=cookie_monster;e=f h', (string) $request3->getCookie()); // Clear the e=f h temporary cookie $this->plugin->clearTemporaryCookies(); $request4->setResponse($response3, true); $request4->send(); $this->assertEquals('muppet=cookie_monster', (string) $request4->getCookie()); }