Beispiel #1
0
 public function testDispatchesErrorEventAndRecovers()
 {
     puzzle_test_Server::flush();
     puzzle_test_Server::enqueue("HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n");
     $r = new puzzle_message_Request('GET', puzzle_test_Server::$url);
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $r);
     $a = $this->getAdapter();
     $r->getEmitter()->once('complete', array($this, '__callback_testDispatchesErrorEventAndRecovers_1'));
     $r->getEmitter()->on('error', array($this, '__callback_testDispatchesErrorEventAndRecovers_2'));
     $response = $a->send($t);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('bar', $response->getHeader('Foo'));
 }
Beispiel #2
0
 public function testCloneIsDeep()
 {
     $r = new puzzle_message_Request('GET', '/test', array('foo' => 'baz'), puzzle_stream_Stream::factory('foo'));
     $r2 = clone $r;
     $this->assertNotSame($r->getEmitter(), $r2->getEmitter());
     $this->assertEquals('foo', $r2->getBody());
     $r->getConfig()->set('test', 123);
     $this->assertFalse($r2->getConfig()->hasKey('test'));
     $r->setPath('/abc');
     $this->assertEquals('/test', $r2->getPath());
 }
Beispiel #3
0
 public function testCanInterceptBeforeSending()
 {
     $client = new puzzle_Client();
     $request = new puzzle_message_Request('GET', 'http://httpbin.org/get');
     $this->_closure_testCanInterceptBeforeSending_response = new puzzle_message_Response(200);
     $request->getEmitter()->on('before', array($this, '__callback_testCanInterceptBeforeSending'));
     $transaction = new puzzle_adapter_Transaction($client, $request);
     $f = 'does_not_work';
     $a = new puzzle_adapter_curl_CurlAdapter(new puzzle_message_MessageFactory(), array('handle_factory' => $f));
     $a->send($transaction);
     $this->assertSame($this->_closure_testCanInterceptBeforeSending_response, $transaction->getResponse());
 }
 public function testSetsResponseBodyForDownload()
 {
     $body = puzzle_stream_Stream::factory();
     $request = new puzzle_message_Request('GET', 'http://httbin.org');
     $this->_closure_testSetsResponseBodyForDownload_ee = null;
     $request->getEmitter()->on('headers', array($this, '__callback_testSetsResponseBodyForDownload'));
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $request);
     $m = new puzzle_adapter_curl_RequestMediator($t, new puzzle_message_MessageFactory());
     $m->setResponseBody($body);
     $this->assertEquals(18, $m->receiveResponseHeader(null, "HTTP/1.1 202 FOO\r\n"));
     $this->assertEquals(10, $m->receiveResponseHeader(null, "Foo: Bar\r\n"));
     $this->assertEquals(11, $m->receiveResponseHeader(null, "Baz : Bam\r\n"));
     $this->assertEquals(19, $m->receiveResponseHeader(null, "Content-Length: 3\r\n"));
     $this->assertEquals(2, $m->receiveResponseHeader(null, "\r\n"));
     $this->assertNotNull($this->_closure_testSetsResponseBodyForDownload_ee);
     $this->assertEquals(202, $t->getResponse()->getStatusCode());
     $this->assertEquals('FOO', $t->getResponse()->getReasonPhrase());
     $this->assertEquals('Bar', $t->getResponse()->getHeader('Foo'));
     $this->assertEquals('Bam', $t->getResponse()->getHeader('Baz'));
     $m->writeResponseBody(null, 'foo');
     $this->assertEquals('foo', (string) $body);
     $this->assertEquals('3', $t->getResponse()->getHeader('Content-Length'));
 }
Beispiel #5
0
 public function testEmitsHeadersEvent()
 {
     $m = new puzzle_adapter_MockAdapter(new puzzle_message_Response(404));
     $request = new puzzle_message_Request('GET', 'http://httbin.org');
     $this->_closure_testEmitsHeadersEvent_called = false;
     $request->getEmitter()->once('headers', array($this, '__callback_testEmitsHeadersEvent'));
     $m->send(new puzzle_adapter_Transaction(new puzzle_Client(), $request));
     $this->assertTrue($this->_closure_testEmitsHeadersEvent_called);
 }
Beispiel #6
0
 public function testThrowsAndReleasesWhenErrorDuringCompleteEvent()
 {
     puzzle_test_Server::flush();
     puzzle_test_Server::enqueue("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n");
     $request = new puzzle_message_Request('GET', puzzle_test_Server::$url);
     $request->getEmitter()->on('complete', array($this, '__callback_testThrowsAndReleasesWhenErrorDuringCompleteEvent'));
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), $request);
     $a = new puzzle_adapter_curl_MultiAdapter(new puzzle_message_MessageFactory());
     try {
         $a->send($t);
         $this->fail('Did not throw');
     } catch (puzzle_exception_RequestException $e) {
         $this->assertSame($request, $e->getRequest());
     }
 }
 public function testThrowsUnInterceptedErrors()
 {
     $this->_closure_testThrowsUnInterceptedErrors_ex = new Exception('Foo');
     $client = new puzzle_Client();
     $request = new puzzle_message_Request('GET', '/');
     $t = new puzzle_adapter_Transaction($client, $request);
     $this->_closure_testThrowsUnInterceptedErrors_errCalled = 0;
     $request->getEmitter()->on('before', array($this, '__callback_testThrowsUnInterceptedErrors'));
     $request->getEmitter()->on('error', array($this, '__callback_testThrowsUnInterceptedErrors_2'));
     try {
         puzzle_event_RequestEvents::emitBefore($t);
         $this->fail('Did not throw');
     } catch (puzzle_exception_RequestException $e) {
         $this->assertEquals(1, $this->_closure_testThrowsUnInterceptedErrors_errCalled);
     }
 }