Beispiel #1
0
 public function testCanUseSeekEndWithUnknownSize()
 {
     $baseStream = Psr7\stream_for('testing');
     $decorated = Psr7\FnStream::decorate($baseStream, ['getSize' => function () {
         return null;
     }]);
     $cached = new CachingStream($decorated);
     $cached->seek(-1, SEEK_END);
     $this->assertEquals('g', $cached->read(1));
 }
Beispiel #2
0
 public function testDecoratesWithCustomizations()
 {
     $called = false;
     $a = Psr7\stream_for('foo');
     $b = FnStream::decorate($a, array('read' => function ($len) use(&$called, $a) {
         $called = true;
         return $a->read($len);
     }));
     $this->assertEquals('foo', $b->read(3));
     $this->assertTrue($called);
 }
 public function testConstructorDoesNotReadStreamBody()
 {
     $streamIsRead = false;
     $body = Psr7\FnStream::decorate(Psr7\stream_for(''), ['__toString' => function () use(&$streamIsRead) {
         $streamIsRead = true;
         return '';
     }]);
     $r = new Request('GET', '/', [], $body);
     $this->assertFalse($streamIsRead);
     $this->assertSame($body, $r->getBody());
 }
 /**
  * @param StreamInterface $stream
  * @return StreamInterface
  */
 private static function wrapBodyStream(StreamInterface $stream)
 {
     $eofStream = false;
     $methods = ['close' => function () use($stream, &$eofStream) {
         $stream->close();
         $eofStream = true;
     }, 'eof' => function () use($stream, &$eofStream) {
         return $eofStream || $stream->eof();
     }];
     //\GuzzleHttp\Stream\FnStream::decorate($stream, $methods);
     return \GuzzleHttp\Psr7\FnStream::decorate($stream, $methods);
 }
 public function testAddsTransferEncodingWhenNoContentLength()
 {
     $body = FnStream::decorate(Psr7\stream_for('foo'), ['getSize' => function () {
         return null;
     }]);
     $h = new MockHandler([function (RequestInterface $request) {
         $this->assertFalse($request->hasHeader('Content-Length'));
         $this->assertEquals('chunked', $request->getHeaderLine('Transfer-Encoding'));
         return new Response(200);
     }]);
     $m = Middleware::prepareBody();
     $stack = new HandlerStack($h);
     $stack->push($m);
     $comp = $stack->resolve();
     $p = $comp(new Request('PUT', 'http://www.google.com', [], $body), []);
     $this->assertInstanceOf(PromiseInterface::class, $p);
     $response = $p->wait();
     $this->assertEquals(200, $response->getStatusCode());
 }
Beispiel #6
0
 /**
  * @expectedException \RuntimeException
  */
 public function testThrowsWhenBodyCannotBeRewound()
 {
     $body = Psr7\stream_for('abc');
     $body->read(1);
     $body = FnStream::decorate($body, ['rewind' => function () {
         throw new \RuntimeException('a');
     }]);
     $res = new Psr7\Response(200, [], $body);
     Psr7\rewind_body($res);
 }
    public function testSerializesFilesWithCustomHeadersAndMultipleValues()
    {
        $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), array('getMetadata' => function () {
            return '/foo/bar.txt';
        }));
        $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), array('getMetadata' => function () {
            return '/foo/baz.jpg';
        }));
        $b = new MultipartStream(array(array('name' => 'foo', 'contents' => $f1, 'headers' => array('x-foo' => 'bar', 'content-disposition' => 'custom')), array('name' => 'foo', 'contents' => $f2, 'headers' => array('cOntenT-Type' => 'custom'))), 'boundary');
        $expected = <<<EOT
--boundary
x-foo: bar
content-disposition: custom
Content-Length: 3
Content-Type: text/plain

foo
--boundary
cOntenT-Type: custom
Content-Disposition: form-data; name="foo"; filename="baz.jpg"
Content-Length: 3

baz
--boundary--

EOT;
        $this->assertEquals($expected, str_replace("\r", '', $b));
    }
 public function testSuccessfullyCallsOnHeadersBeforeWritingToSink()
 {
     Server::flush();
     Server::enqueue([new Psr7\Response(200, ['X-Foo' => 'bar'], 'abc 123')]);
     $req = new Psr7\Request('GET', Server::$url);
     $got = null;
     $stream = Psr7\stream_for();
     $stream = Psr7\FnStream::decorate($stream, ['write' => function ($data) use($stream, &$got) {
         $this->assertNotNull($got);
         return $stream->write($data);
     }]);
     $handler = new Handler\CurlHandler();
     $promise = $handler($req, ['sink' => $stream, 'on_headers' => function (ResponseInterface $res) use(&$got) {
         $got = $res;
         $this->assertEquals('bar', $res->getHeaderLine('X-Foo'));
     }]);
     $response = $promise->wait();
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('bar', $response->getHeaderLine('X-Foo'));
     $this->assertEquals('abc 123', (string) $response->getBody());
 }