Ejemplo n.º 1
0
 public function testCastsToString()
 {
     $m = new puzzle_message_Request('GET', 'http://foo.com');
     $m->setHeader('foo', 'bar');
     $m->setBody(puzzle_stream_Stream::factory('baz'));
     $this->assertEquals("GET / HTTP/1.1\r\nHost: foo.com\r\nfoo: bar\r\n\r\nbaz", (string) $m);
 }
Ejemplo n.º 2
0
 public function testReadsFromRequestBody()
 {
     $body = puzzle_stream_Stream::factory('foo');
     $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('PUT', 'http://httbin.org', array(), $body));
     $m = new puzzle_adapter_curl_RequestMediator($t, new puzzle_message_MessageFactory());
     $this->assertEquals('foo', $m->readRequestBody(null, null, 3));
 }
Ejemplo n.º 3
0
 public function testCanAddHeaders()
 {
     $p = new puzzle_post_PostFile('foo', puzzle_stream_Stream::factory('hi'), 'test.php', array('X-Foo' => '123', 'Content-Disposition' => 'bar'));
     $headers = $p->getHeaders();
     $this->assertEquals('bar', $headers['Content-Disposition']);
     $this->assertEquals('123', $headers['X-Foo']);
 }
Ejemplo n.º 4
0
 public function testHasStream()
 {
     $s = puzzle_stream_Stream::factory('foo');
     $e = new puzzle_stream_exception_SeekException($s, 10);
     $this->assertSame($s, $e->getStream());
     $this->assertContains('10', $e->getMessage());
 }
Ejemplo n.º 5
0
 public function testCastsToString()
 {
     $m = new tubepress_test_lib_http_impl_puzzle_message_MessageTest__foo(new tubepress_test_lib_http_impl_puzzle_message_MessageTest__bar());
     $m->setHeader('foo', 'bar');
     $m->setBody(new tubepress_http_impl_puzzle_streams_PuzzleBasedStream(puzzle_stream_Stream::factory('baz')));
     $this->assertEquals("GET / HTTP/1.1\r\nfoo: bar\r\n\r\nbaz", (string) $m);
 }
Ejemplo n.º 6
0
 public function testHandlesClose()
 {
     $s = puzzle_stream_Stream::factory('foo');
     $wrapped = new puzzle_stream_NoSeekStream($s);
     $wrapped->close();
     $this->assertFalse($wrapped->write('foo'));
 }
Ejemplo n.º 7
0
 public function testInflatesStreams()
 {
     $content = gzencode('test');
     $a = puzzle_stream_Stream::factory($content);
     $b = new puzzle_stream_InflateStream($a);
     $this->assertEquals('test', (string) $b);
 }
 public function setUp()
 {
     $this->c = fopen('php://temp', 'r+');
     fwrite($this->c, 'foo');
     fseek($this->c, 0);
     $this->a = puzzle_stream_Stream::factory($this->c);
     $this->b = new puzzle_test_stream_Str($this->a);
 }
Ejemplo n.º 9
0
 public function testDecoratesWithCustomizations()
 {
     $this->_closure_var_testDecoratesWithCustomizations_called = false;
     $this->_closure_var_testDecoratesWithCustomizations_a = puzzle_stream_Stream::factory('foo');
     $b = puzzle_stream_FnStream::decorate($this->_closure_var_testDecoratesWithCustomizations_a, array('read' => array($this, '__callback_testDecoratesWithCustomizations')));
     $this->assertEquals('foo', $b->read(3));
     $this->assertTrue($this->_closure_var_testDecoratesWithCustomizations_called);
 }
Ejemplo n.º 10
0
 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());
 }
Ejemplo n.º 11
0
 public function testReadsRequestBody()
 {
     $response = new puzzle_message_Response(200);
     $m = new puzzle_adapter_MockAdapter($response);
     $m->setResponse($response);
     $body = puzzle_stream_Stream::factory('foo');
     $request = new puzzle_message_Request('PUT', 'http://httpbin.org/put', array(), $body);
     $this->assertSame($response, $m->send(new puzzle_adapter_Transaction(new puzzle_Client(), $request)));
     $this->assertEquals(3, $body->tell());
 }
Ejemplo n.º 12
0
 public function testCastsToString()
 {
     $r = new puzzle_message_Request('GET', 'http://test.com/test', array('foo' => 'baz'), puzzle_stream_Stream::factory('body'));
     $s = explode("\r\n", (string) $r);
     $this->assertEquals("GET /test HTTP/1.1", $s[0]);
     $this->assertContains('Host: test.com', $s);
     $this->assertContains('foo: baz', $s);
     $this->assertContains('', $s);
     $this->assertContains('body', $s);
 }
Ejemplo n.º 13
0
 public function testCanDetermineSizeFromMultipleStreams()
 {
     $a = new puzzle_stream_AppendStream(array(puzzle_stream_Stream::factory('foo'), puzzle_stream_Stream::factory('bar')));
     $this->assertEquals(6, $a->getSize());
     $s = $this->getMockBuilder('puzzle_stream_StreamInterface')->setMethods(array('isSeekable', 'isReadable'))->getMockForAbstractClass();
     $s->expects($this->once())->method('isSeekable')->will($this->returnValue(null));
     $s->expects($this->once())->method('isReadable')->will($this->returnValue(true));
     $a->addStream($s);
     $this->assertNull($a->getSize());
 }
Ejemplo n.º 14
0
 public function testPreventsComplexExternalEntities()
 {
     $xml = '<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=ResponseTest.php">]><scan>&test;</scan>';
     $response = new puzzle_message_Response(200, array(), puzzle_stream_Stream::factory($xml));
     $oldCwd = getcwd();
     chdir(dirname(__FILE__));
     try {
         $xml = $response->xml();
         chdir($oldCwd);
         $this->markTestIncomplete('Did not throw the expected exception! XML resolved as: ' . $xml->asXML());
     } catch (Exception $e) {
         chdir($oldCwd);
     }
 }
Ejemplo n.º 15
0
 public function testThrowsExceptionWhenFailsToParseJsonResponse()
 {
     try {
         $response = new tubepress_http_impl_puzzle_PuzzleBasedResponse(new puzzle_message_Response(200, array(), puzzle_stream_Stream::factory('{"foo": "')));
         $response->toJson();
     } catch (puzzle_exception_ParseException $e) {
         if (version_compare(PHP_VERSION, '7.0') >= 0) {
             $this->assertEquals('Unable to parse JSON data: JSON_ERROR_CTRL_CHAR - Unexpected control character found', $e->getMessage());
         } else {
             $this->assertEquals('Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON', $e->getMessage());
         }
         return;
     }
     $this->fail('Should have thrown exception');
 }
Ejemplo n.º 16
0
 /**
  * Prepares the contents of a POST file.
  *
  * @param mixed $content Content of the POST file
  */
 private function prepareContent($content)
 {
     $this->content = $content;
     if (!$this->content instanceof puzzle_stream_StreamInterface) {
         $this->content = puzzle_stream_Stream::factory($this->content);
     } elseif ($this->content instanceof puzzle_post_MultipartBody) {
         if (!$this->hasHeader('Content-Disposition')) {
             $disposition = 'form-data; name="' . $this->name . '"';
             $this->headers['Content-Disposition'] = $disposition;
         }
         if (!$this->hasHeader('Content-Type')) {
             $this->headers['Content-Type'] = sprintf("multipart/form-data; boundary=%s", $this->content->getBoundary());
         }
     }
 }
Ejemplo n.º 17
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);
 }
Ejemplo n.º 18
0
 public function testResource()
 {
     $stream = puzzle_stream_Stream::factory('foo');
     $handle = puzzle_stream_GuzzleStreamWrapper::getResource($stream);
     $this->assertSame('foo', fread($handle, 3));
     $this->assertSame(3, ftell($handle));
     $this->assertSame(3, fwrite($handle, 'bar'));
     $this->assertSame(0, fseek($handle, 0));
     $this->assertSame('foobar', fread($handle, 6));
     $this->assertTrue(feof($handle));
     // This fails on HHVM for some reason
     if (!defined('HHVM_VERSION')) {
         $this->assertEquals(array('dev' => 0, 'ino' => 0, 'mode' => 33206, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 6, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0, 0 => 0, 1 => 0, 2 => 33206, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 6, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0), fstat($handle));
     }
     $this->assertTrue(fclose($handle));
     $this->assertSame('foobar', (string) $stream);
 }
Ejemplo n.º 19
0
 public function onRequest(tubepress_api_event_EventInterface $event)
 {
     /*
      * @var tubepress_api_http_message_RequestInterface
      */
     $httpRequest = $event->getSubject();
     if (!$this->_shouldExecute($httpRequest)) {
         return;
     }
     $url = $httpRequest->getUrl();
     $item = $this->_getCachedItem($url);
     if ($item->isMiss()) {
         return;
     }
     $response = new tubepress_http_impl_puzzle_PuzzleBasedResponse(new puzzle_message_Response(200, array(self::HTTP_HEADER_CACHE_HIT => 'true'), puzzle_stream_Stream::factory($item->get())));
     $event->setArgument('response', $response);
     $event->stopPropagation();
 }
Ejemplo n.º 20
0
 public function onComplete(puzzle_event_CompleteEvent $event)
 {
     $transferInfo = $event->getTransferInfo();
     if (array_key_exists('http_code', $transferInfo)) {
         //curl
         return;
     }
     $response = $event->getResponse();
     if (!$response->hasHeader('Transfer-Encoding')) {
         return;
     }
     $encoding = $response->getHeader('Transfer-Encoding');
     if (strcasecmp($encoding, 'chunked') !== 0) {
         return;
     }
     $body = $response->getBody()->__toString();
     $decodedBody = $this->_decode($body);
     $response->setBody(puzzle_stream_Stream::factory($decodedBody));
 }
Ejemplo n.º 21
0
 /**
  * Creates the underlying stream lazily when required.
  *
  * @return puzzle_stream_StreamInterface
  */
 protected function createStream()
 {
     return puzzle_stream_Stream::factory(puzzle_stream_Utils::open($this->filename, $this->mode));
 }
Ejemplo n.º 22
0
 /**
  * Creates an application/x-www-form-urlencoded stream body
  *
  * @return puzzle_stream_StreamInterface
  */
 private function createUrlEncoded()
 {
     return puzzle_stream_Stream::factory($this->getFields(true));
 }
Ejemplo n.º 23
0
 private function add_json(puzzle_message_RequestInterface $request, $value)
 {
     if (!$request->hasHeader('Content-Type')) {
         $request->setHeader('Content-Type', 'application/json');
     }
     $request->setBody(puzzle_stream_Stream::factory(json_encode($value)));
 }
Ejemplo n.º 24
0
 /**
  * Drain the stream into the destination stream
  */
 private function getSaveToBody(puzzle_message_RequestInterface $request, puzzle_stream_StreamInterface $stream)
 {
     $config = $request->getConfig();
     if ($saveTo = $config['save_to']) {
         // Stream the response into the destination stream
         $saveTo = is_string($saveTo) ? new puzzle_stream_Stream(puzzle_stream_Utils::open($saveTo, 'r+')) : puzzle_stream_Stream::factory($saveTo);
     } else {
         // Stream into the default temp stream
         $saveTo = puzzle_stream_Stream::factory();
     }
     puzzle_stream_Utils::copyToStream($stream, $saveTo);
     $saveTo->seek(0);
     $stream->close();
     return $saveTo;
 }
Ejemplo n.º 25
0
 public function testRetriesRewindableStreamsWhenClosedConnectionErrors()
 {
     $this->runConnectionTest(array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n"), puzzle_stream_Stream::factory('foo'), false, 201);
 }
Ejemplo n.º 26
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testThrowsExceptionForUnknown()
 {
     puzzle_stream_Stream::factory(new stdClass());
 }
Ejemplo n.º 27
0
 private function add_save_to(puzzle_message_RequestInterface $request, puzzle_adapter_curl_RequestMediator $mediator, $value)
 {
     $mediator->setResponseBody(is_string($value) ? new puzzle_stream_LazyOpenStream($value, 'w') : puzzle_stream_Stream::factory($value));
 }
Ejemplo n.º 28
0
 /**
  * Alias of puzzle_stream_Stream::factory.
  *
  * @param mixed $resource Resource to create
  * @param int   $size     Size if known up front
  *
  * @return puzzle_stream_MetadataStreamInterface
  *
  * @see puzzle_stream_Stream::factory
  */
 public static function create($resource, $size = null)
 {
     return puzzle_stream_Stream::factory($resource, $size);
 }
Ejemplo n.º 29
0
 public function testLengthLessOffsetWhenNoLimitSize()
 {
     $a = puzzle_stream_Stream::factory('foo_bar');
     $b = new puzzle_stream_LimitStream($a, -1, 4);
     $this->assertEquals(3, $b->getSize());
 }
Ejemplo n.º 30
0
 /**
  * @deprecated Moved to puzzle_stream_Stream::factory
  */
 function puzzle_stream_create($resource = '', $size = null)
 {
     return puzzle_stream_Stream::factory($resource, $size);
 }