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); }
public function testReadsBodiesFromMockedRequests() { $m = new puzzle_subscriber_Mock(array(new puzzle_message_Response(200))); $client = new puzzle_Client(array('base_url' => 'http://test.com')); $client->getEmitter()->attach($m); $body = puzzle_stream_Stream::factory('foo'); $client->put('/', array('body' => $body)); $this->assertEquals(3, $body->tell()); }
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 __construct(tubepress_api_event_EventDispatcherInterface $eventDispatcher, puzzle_Client $delegate, tubepress_api_log_LoggerInterface $logger) { parent::__construct($eventDispatcher, $logger); if (!function_exists('puzzle_request')) { require TUBEPRESS_ROOT . '/vendor/puzzlehttp/puzzle/src/main/php/puzzle/functions.php'; } if (!function_exists('puzzle_stream_create')) { require TUBEPRESS_ROOT . '/vendor/puzzlehttp/streams/src/main/php/puzzle/stream/functions.php'; } $this->_delegate = $delegate; $this->_delegate->setDefaultOption('verify', TUBEPRESS_ROOT . '/vendor/puzzlehttp/puzzle/src/main/php/puzzle/cacert.pem'); $delegate->getEmitter()->attach($this); }
public function testRelativeLinkBasedLatestRequest() { $client = new puzzle_Client(array('base_url' => 'http://www.foo.com')); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array("HTTP/1.1 301 Moved Permanently\r\nLocation: http://www.bar.com\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"))); $response = $client->get('/'); $this->assertEquals('http://www.bar.com/redirect', $response->getEffectiveUrl()); }
public function testSendsAllInParallel() { $client = new puzzle_Client(); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(200), new puzzle_message_Response(201), new puzzle_message_Response(202)))); $history = new puzzle_subscriber_History(); $client->getEmitter()->attach($history); $requests = array($client->createRequest('GET', 'http://test.com'), $client->createRequest('POST', 'http://test.com'), $client->createRequest('PUT', 'http://test.com')); $client->sendAll($requests); $requests = array_map(array($this, '__callback_testSendsAllInParallel'), $history->getRequests()); $this->assertContains('GET', $requests); $this->assertContains('POST', $requests); $this->assertContains('PUT', $requests); }
/** * @expectedException puzzle_exception_ClientException */ public function testFullTransaction() { $client = new puzzle_Client(); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(403)))); $client->get('http://httpbin.org'); }
public function testCanForceMultipartUploadWithContentType() { $client = new puzzle_Client(); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(200)))); $history = new puzzle_subscriber_History(); $client->getEmitter()->attach($history); $client->post('http://foo.com', array('headers' => array('Content-Type' => 'multipart/form-data'), 'body' => array('foo' => 'bar'))); $this->assertContains('multipart/form-data; boundary=', $history->getLastRequest()->getHeader('Content-Type')); $this->assertContains("Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar", (string) $history->getLastRequest()->getBody()); }
public function testBatchesRequests() { $client = new puzzle_Client(); $responses = array(new puzzle_message_Response(301, array('Location' => 'http://foo.com/bar')), new puzzle_message_Response(200), new puzzle_message_Response(200), new puzzle_message_Response(404)); $client->getEmitter()->attach(new puzzle_subscriber_Mock($responses)); $requests = array($client->createRequest('GET', 'http://foo.com/baz'), $client->createRequest('HEAD', 'http://httpbin.org/get'), $client->createRequest('PUT', 'http://httpbin.org/put')); $this->_closure_testBatchesRequests_a = $this->_closure_testBatchesRequests_b = $this->_closure_testBatchesRequests_c = 0; $result = puzzle_batch($client, $requests, array('before' => array($this, '__callback_testBatchesRequests_a'), 'complete' => array($this, '__callback_testBatchesRequests_b'), 'error' => array($this, '__callback_testBatchesRequests_c'))); $this->assertEquals(4, $this->_closure_testBatchesRequests_a); $this->assertEquals(2, $this->_closure_testBatchesRequests_b); $this->assertEquals(1, $this->_closure_testBatchesRequests_c); $this->assertCount(3, $result); foreach ($result as $i => $request) { $this->assertSame($requests[$i], $request); } // The first result is actually the second (redirect) response. $this->assertSame($responses[1], $result->offsetGet($requests[0])); // The second result is a 1:1 request:response map $this->assertSame($responses[2], $result->offsetGet($requests[1])); // The third entry is the 404 puzzle_exception_RequestException $this->assertSame($responses[3], $result->offsetGet($requests[2])->getResponse()); }
/** * Note: Longest test name ever. */ public function testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent() { $request = new puzzle_message_Request('GET', '/'); $this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_ex = new puzzle_exception_RequestException('foo', $request); $client = new puzzle_Client(); $client->getEmitter()->on('before', array($this, '__callback_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_1')); $this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_called = false; $client->getEmitter()->on('error', array($this, '__callback_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_2')); try { $client->get('http://foo.com'); $this->fail('Did not throw'); } catch (puzzle_exception_RequestException $e) { $this->assertTrue($this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_called); } }