public function testCallsPumpForMoreDataWhenRequested()
 {
     $called = 0;
     $buffer = new BufferStream();
     $a = new AsyncReadStream($buffer, ['pump' => function ($size) use(&$called) {
         $called++;
         return str_repeat('.', $size);
     }]);
     $buffer->write('foobar');
     $this->assertEquals('foo', $a->read(3));
     $this->assertEquals(0, $called);
     $this->assertEquals('bar.....', $a->read(8));
     $this->assertEquals(1, $called);
     $this->assertEquals('..', $a->read(2));
     $this->assertEquals(2, $called);
 }
Example #2
0
 public function testBeginsDroppingWhenSizeExceeded()
 {
     $stream = new BufferStream();
     $drop = new DroppingStream($stream, 5);
     $this->assertEquals(3, $drop->write('hel'));
     $this->assertFalse($drop->write('lo'));
     $this->assertEquals(5, $drop->getSize());
     $this->assertEquals('hello', $drop->read(5));
     $this->assertEquals(0, $drop->getSize());
     $drop->write('12345678910');
     $this->assertEquals(5, $stream->getSize());
     $this->assertEquals(5, $drop->getSize());
     $this->assertEquals('12345', (string) $drop);
     $this->assertEquals(0, $drop->getSize());
     $drop->write('hello');
     $this->assertFalse($drop->write('test'));
 }
Example #3
0
 public function request($method, $path, $body = [])
 {
     $url = $this->getUrlOfPath($path);
     if (is_array($body) && !empty($body)) {
         $body = Json::encode($body);
     }
     $cacheKey = md5($method . $url . $body);
     $result = Yii::$app->cache->get($cacheKey);
     if ($result !== false) {
         return $result;
     }
     try {
         $result = $this->httpClient->request($url, $method, function (Event $event) use($body) {
             $request = $event->message;
             $authString = base64_encode($this->username . ':' . $this->password);
             $request->addHeader("Authorization", "Basic " . $authString);
             $request->addHeader("Accept", "application/json");
             $request->addHeader("Content-Type", "application/json");
             if (!empty($body)) {
                 $stream = new BufferStream();
                 $stream->write($body);
                 $request->setBody($stream);
             }
         });
         if (is_string($result)) {
             $result = Json::decode($result);
         }
         \Yii::trace($url . "\n" . $body, __CLASS__);
     } catch (RequestException $e) {
         $result = $e->getResponse()->getBody()->__toString();
         $contentType = $e->getResponse()->getHeader('Content-Type');
         if (strpos($contentType, 'application/json') !== false) {
             $result = Json::decode($result);
         }
         \Yii::error($result, __CLASS__);
     }
     Yii::$app->cache->set($cacheKey, $result, $this->cacheDuration);
     return $result;
 }
Example #4
0
 private function pump($length)
 {
     if ($this->source) {
         do {
             $data = call_user_func($this->source, $length);
             if ($data === false || $data === null) {
                 $this->source = null;
                 return;
             }
             $this->buffer->write($data);
             $length -= strlen($data);
         } while ($length > 0);
     }
 }
Example #5
0
 /**
  * @expectedException \GuzzleHttp\Stream\Exception\CannotAttachException
  */
 public function testCannotAttach()
 {
     $p = new BufferStream();
     $p->attach('a');
 }