Exemple #1
0
 /**
  * Send a custom request
  *
  * @param string $method  HTTP request method
  * @param string $url     URL of the request
  * @param array  $options Options to use with the request.
  *
  * @return puzzle_message_ResponseInterface
  */
 function puzzle_request($method, $url, array $options = array())
 {
     static $client;
     if (!$client) {
         $client = new puzzle_Client();
     }
     return $client->send($client->createRequest($method, $url, $options));
 }
 public function testEmitsAfterSendEvent()
 {
     $this->_closure_testEmitsAfterSendEvent_ee = null;
     puzzle_test_Server::flush();
     puzzle_test_Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there");
     $client = new puzzle_Client(array('adapter' => new puzzle_adapter_StreamAdapter(new puzzle_message_MessageFactory())));
     $request = $client->createRequest('GET', puzzle_test_Server::$url);
     $request->getEmitter()->on('complete', array($this, '__callback_testEmitsAfterSendEvent'));
     $client->send($request);
     $this->assertInstanceOf('puzzle_event_CompleteEvent', $this->_closure_testEmitsAfterSendEvent_ee);
     $this->assertSame($request, $this->_closure_testEmitsAfterSendEvent_ee->getRequest());
     $this->assertEquals(200, $this->_closure_testEmitsAfterSendEvent_ee->getResponse()->getStatusCode());
 }
Exemple #3
0
 public function testCanCastToString()
 {
     $client = new puzzle_Client(array('base_url' => 'http://localhost/'));
     $h = new puzzle_subscriber_History();
     $client->getEmitter()->attach($h);
     $mock = new puzzle_subscriber_Mock(array(new puzzle_message_Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), new puzzle_message_Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), new puzzle_message_Response(200, array('Content-Length' => '2'), puzzle_stream_Stream::factory('HI'))));
     $client->getEmitter()->attach($mock);
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals(3, count($h));
     $h = str_replace("\r", '', $h);
     $this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h);
     $this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h);
     $this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h);
     $this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h);
 }
Exemple #4
0
 public function testCanMockBadRequestExceptions()
 {
     $client = new puzzle_Client(array('base_url' => 'http://test.com'));
     $request = $client->createRequest('GET', '/');
     $ex = new puzzle_exception_RequestException('foo', $request);
     $mock = new puzzle_subscriber_Mock(array($ex));
     $this->assertCount(1, $mock);
     $request->getEmitter()->attach($mock);
     try {
         $client->send($request);
         $this->fail('Did not dequeue an exception');
     } catch (puzzle_exception_RequestException $e) {
         $this->assertSame($e, $ex);
         $this->assertSame($request, $ex->getRequest());
     }
 }
Exemple #5
0
 public function testCookiesAreExtractedFromRedirectResponses()
 {
     $jar = new puzzle_cookie_CookieJar();
     $cookie = new puzzle_subscriber_Cookie($jar);
     $history = new puzzle_subscriber_History();
     $mock = new puzzle_subscriber_Mock(array("HTTP/1.1 302 Moved Temporarily\r\n" . "Set-Cookie: test=583551; Domain=www.foo.com; 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 puzzle_Client(array('base_url' => 'http://www.foo.com'));
     $client->getEmitter()->attach($cookie);
     $client->getEmitter()->attach($mock);
     $client->getEmitter()->attach($history);
     $client->get();
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals('test=583551', $request->getHeader('Cookie'));
     $requests = $history->getRequests();
     // 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 testCanAddEventsOnce()
 {
     $this->_closure_testCanAddEventsOnce_foo = 0;
     $client = new puzzle_Client();
     $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(200), new puzzle_message_Response(200))));
     $fn = array($this, '__callback_testCanAddEventsOnce');
     $request = $client->createRequest('GET', 'http://test.com', array('events' => array('before' => array('fn' => $fn, 'once' => true))));
     $client->send($request);
     $this->assertEquals(1, $this->_closure_testCanAddEventsOnce_foo);
     $client->send($request);
     $this->assertEquals(1, $this->_closure_testCanAddEventsOnce_foo);
 }