Inheritance: implements Psr\Http\Message\StreamableInterface
Ejemplo n.º 1
0
 public function testSerializesRequestWithBody()
 {
     $body = json_encode(['test' => 'value']);
     $stream = new Stream('php://memory', 'wb+');
     $stream->write($body);
     $request = (new Request())->withMethod('POST')->withUri(new Uri('http://example.com/foo/bar'))->withAddedHeader('Accept', 'application/json')->withAddedHeader('Content-Type', 'application/json')->withBody($stream);
     $message = Serializer::toString($request);
     $this->assertContains("POST /foo/bar HTTP/1.1\r\n", $message);
     $this->assertContains("\r\n\r\n" . $body, $message);
 }
Ejemplo n.º 2
0
 /**
  * @group 42
  */
 public function testGetSizeReturnsStreamSize()
 {
     $resource = fopen(__FILE__, 'r');
     $expected = fstat($resource);
     $stream = new Stream($resource);
     $this->assertEquals($expected['size'], $stream->getSize());
 }
Ejemplo n.º 3
0
 /**
  * Deserialize a request string to a request instance.
  *
  * Internally, casts the message to a stream and invokes fromStream().
  *
  * @param string $message
  * @return Request
  * @throws UnexpectedValueException when errors occur parsing the message.
  */
 public static function fromString($message)
 {
     $stream = new Stream('php://temp', 'wb+');
     $stream->write($message);
     return self::fromStream($stream);
 }
Ejemplo n.º 4
0
 public function testCannotRetrieveStreamAfterMove()
 {
     $stream = new Stream('php://temp', 'wb+');
     $stream->write('Foo bar!');
     $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
     $this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'phly');
     $upload->moveTo($to);
     $this->assertTrue(file_exists($to));
     $this->setExpectedException('RuntimeException', 'moved');
     $upload->getStream();
 }
Ejemplo n.º 5
0
 /**
  * Creates a stream.
  *
  * @param null|resource|string|\Psr\Http\Message\StreamableInterface|null $body The body.
  *
  * @return \Psr\Http\Message\StreamableInterface The stream.
  */
 private function createStream($body)
 {
     if ($body instanceof StreamableInterface) {
         return $body;
     }
     if (is_resource($body)) {
         return new Stream($body);
     }
     $stream = new Stream('php://memory', 'rw');
     if ($body === null) {
         return $stream;
     }
     $stream->write((string) $body);
     return $stream;
 }
Ejemplo n.º 6
0
 /**
  * Split the stream into headers and body content.
  *
  * Returns an array containing two elements
  *
  * - The first is an array of headers
  * - The second is a StreamInterface containing the body content
  *
  * @param StreamInterface $stream
  * @return array
  * @throws UnexpectedValueException For invalid headers.
  */
 protected static function splitStream(StreamInterface $stream)
 {
     $headers = [];
     $currentHeader = false;
     while ($line = self::getLine($stream)) {
         if (preg_match(';^(?P<name>[!#$%&\'*+.^_`\\|~0-9a-zA-Z-]+):(?P<value>.*)$;', $line, $matches)) {
             $currentHeader = $matches['name'];
             if (!isset($headers[$currentHeader])) {
                 $headers[$currentHeader] = [];
             }
             $headers[$currentHeader][] = ltrim($matches['value']);
             continue;
         }
         if (!$currentHeader) {
             throw new UnexpectedValueException('Invalid header detected');
         }
         if (!preg_match('#^[ \\t]#', $line)) {
             throw new UnexpectedValueException('Invalid header continuation');
         }
         // Append continuation to last header value found
         $value = array_pop($headers[$currentHeader]);
         $headers[$currentHeader][] = $value . ltrim($line);
     }
     $body = new Stream('php://temp', 'wb+');
     if (!$stream->eof()) {
         while ($data = $stream->read(4096)) {
             $body->write($data);
         }
         $body->rewind();
     }
     return [$headers, $body];
 }