Exemplo n.º 1
0
 /**
  * Returns a readable JSON stream for the given ResponseInterface
  *
  * @param PromiseInterface $promise Promise<ResponseInterface>
  * @return ReadableStreamInterface
  * @uses self::parsePlainSream()
  */
 public function parseJsonStream(PromiseInterface $promise)
 {
     // application/json
     $in = $this->parsePlainStream($promise);
     $out = new ReadableStream();
     // invalid/closing input stream => return closed output stream
     if (!$in->isReadable()) {
         $out->close();
         return $out;
     }
     // forward each data chunk to the streaming JSON parser
     $parser = new StreamingJsonParser();
     $in->on('data', function ($data) use($parser, $out) {
         $objects = $parser->push($data);
         foreach ($objects as $object) {
             if (isset($object['error'])) {
                 $out->emit('error', array(new JsonProgressException($object), $out));
                 $out->close();
                 return;
             }
             $out->emit('progress', array($object, $out));
         }
     });
     // forward error and make sure stream closes
     $in->on('error', function ($error) use($out) {
         $out->emit('error', array($error, $out));
         $out->close();
     });
     // closing either stream closes the other one
     $in->on('close', array($out, 'close'));
     $out->on('close', array($in, 'close'));
     return $out;
 }
Exemplo n.º 2
0
 /** @test */
 public function doubleCloseShouldWork()
 {
     $readable = new ReadableStream();
     $readable->close();
     $readable->close();
     $this->assertFalse($readable->isReadable());
 }
Exemplo n.º 3
0
 /** @test */
 public function pipingStuffIntoItShouldWorkButDoNothing()
 {
     $readable = new ReadableStream();
     $through = new WritableStream();
     $readable->pipe($through);
     $readable->emit(Event::DATA, array('foo'));
 }
Exemplo n.º 4
0
 public function testClosedReadableStreamRejects()
 {
     $stream = new ReadableStream();
     $stream->close();
     $promise = Stream\first($stream);
     $this->expectPromiseReject($promise);
 }
Exemplo n.º 5
0
 public function testClosedStreamResolvesWithEmptyBuffer()
 {
     $stream = new ReadableStream();
     $stream->close();
     $promise = Stream\all($stream);
     $this->expectPromiseResolveWith(array(), $promise);
 }
Exemplo n.º 6
0
 /** @test */
 public function pipingStuffIntoItShouldWork()
 {
     $readable = new ReadableStream();
     $through = new ThroughStream();
     $through->on('data', $this->expectCallableOnceWith('foo'));
     $readable->pipe($through);
     $readable->emit('data', array('foo'));
 }
 public function testReturnsStreamThatWillBeClosedWhenPromiseResolvesWithClosedInputStream()
 {
     $input = new ReadableStream();
     $input->close();
     $promise = Timer\resolve(0.001, $this->loop)->then(function () use($input) {
         return $input;
     });
     $stream = Stream\unwrapReadable($promise);
     $this->assertTrue($stream->isReadable());
     $stream->on('close', $this->expectCallableOnce());
     $this->loop->run();
     $this->assertFalse($stream->isReadable());
 }
Exemplo n.º 8
0
 public function parsePlainStream(PromiseInterface $promise)
 {
     // text/plain
     $out = new ReadableStream();
     // try to cancel promise once the stream closes
     if ($promise instanceof CancellablePromiseInterface) {
         $out->on('close', function () use($promise) {
             $promise->cancel();
         });
     }
     $promise->then(function ($response) use($out) {
         $out->close();
     }, function ($error) use($out) {
         $out->emit('error', array($error, $out));
         $out->close();
     }, function ($progress) use($out) {
         if (is_array($progress) && isset($progress['responseStream'])) {
             $stream = $progress['responseStream'];
             /* @var $stream React\Stream\Stream */
             // hack to do not buffer stream contents in body
             $stream->removeAllListeners('data');
             // got a streaming HTTP response => forward each data chunk to the resulting output stream
             $stream->on('data', function ($data) use($out) {
                 $out->emit('data', array($data, $out));
             });
         }
     });
     return $out;
 }
Exemplo n.º 9
0
 public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefault()
 {
     $messageFactory = new MessageFactory();
     $loop = Factory::create();
     $stream = new ReadableStream();
     $loop->addTimer(0.001, function () use($stream) {
         $stream->emit('data', array('hello world'));
         $stream->close();
     });
     $request = $this->getMock('Psr\\Http\\Message\\RequestInterface');
     $response = $messageFactory->response(1.0, 200, 'OK', array(), $stream);
     // mock sender to resolve promise with the given $response in response to the given $request
     $sender = $this->getMockBuilder('Clue\\React\\Buzz\\Io\\Sender')->disableOriginalConstructor()->getMock();
     $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response));
     $transaction = new Transaction($request, $sender, array(), $messageFactory);
     $promise = $transaction->send();
     $response = Block\await($promise, $loop);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('hello world', (string) $response->getBody());
 }
Exemplo n.º 10
0
 /** @test */
 public function pipeShouldSucceedAndResolve()
 {
     $callback = $this->expectCallableOnceWith('foobar');
     $sink = new BufferedSink();
     $sink->promise()->then($callback);
     $readable = new ReadableStream();
     $readable->pipe($sink);
     $readable->emit(Event::DATA, array('foo'));
     $readable->emit(Event::DATA, array('bar'));
     $readable->close();
 }
Exemplo n.º 11
0
 public static function createPromise(ReadableStream $stream)
 {
     $sink = new static();
     $stream->pipe($sink);
     return $sink->promise();
 }
Exemplo n.º 12
0
 /** @test */
 public function itShouldForwardPipeCallsToReadableStream()
 {
     $readable = new ReadableStream();
     $writable = $this->getMock('React\\Stream\\WritableStreamInterface');
     $composite = new CompositeStream($readable, $writable);
     $output = $this->getMock('React\\Stream\\WritableStreamInterface');
     $output->expects($this->once())->method('write')->with('foo');
     $composite->pipe($output);
     $readable->emit('data', array('foo'));
 }
Exemplo n.º 13
0
 public function testPostStreamClosed()
 {
     $stream = new ReadableStream();
     $stream->close();
     $response = Block\await($this->browser->post($this->base . 'post', array(), $stream), $this->loop);
     $data = json_decode((string) $response->getBody(), true);
     $this->assertEquals('', $data['data']);
 }
Exemplo n.º 14
0
 public function testErrorEventFromInputWillBeForwarded()
 {
     $input = new ReadableStream();
     $output = $this->getMock('React\\Stream\\WritableStreamInterface');
     //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
     $readline = new Readline($input, $output);
     $stdio = new Stdio($this->loop, $input, $output, $readline);
     $stdio->on('error', $this->expectCallableOnce());
     $input->emit('error', array(new \RuntimeException()));
 }