Exemplo n.º 1
0
 /**
  * Create a JSON response with the given data.
  *
  * Default JSON encoding is performed with the following options, which
  * produces RFC4627-compliant JSON, capable of embedding into HTML.
  *
  * - JSON_HEX_TAG
  * - JSON_HEX_APOS
  * - JSON_HEX_AMP
  * - JSON_HEX_QUOT
  * - JSON_UNESCAPED_SLASHES
  *
  * @param mixed $data Data to convert to JSON.
  * @param int $status Integer status code for the response; 200 by default.
  * @param array $headers Array of headers to use at initialization.
  * @param int $encodingOptions JSON encoding options to use.
  * @throws InvalidArgumentException if unable to encode the $data to JSON.
  */
 public function __construct($data, $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS)
 {
     $body = new Stream('php://temp', 'wb+');
     $body->write($this->jsonEncode($data, $encodingOptions));
     $headers = $this->injectContentType('application/json', $headers);
     parent::__construct($body, $status, $headers);
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
 /**
  * Create the message body.
  *
  * @param string|StreamInterface $html
  * @return StreamInterface
  * @throws InvalidArgumentException if $html is neither a string or stream.
  */
 private function createBody($html)
 {
     if ($html instanceof StreamInterface) {
         return $html;
     }
     if (!is_string($html)) {
         throw new InvalidArgumentException(sprintf('Invalid content (%s) provided to %s', is_object($html) ? get_class($html) : gettype($html), __CLASS__));
     }
     $body = new Stream('php://temp', 'wb+');
     $body->write($html);
     return $body;
 }
Exemplo n.º 4
0
 /**
  * Deserialize a response string to a response instance.
  *
  * @param string $message
  * @return Response
  * @throws UnexpectedValueException when errors occur parsing the message.
  */
 public static function fromString($message)
 {
     $stream = new Stream('php://temp', 'wb+');
     $stream->write($message);
     return static::fromStream($stream);
 }
Exemplo n.º 5
0
 /**
  * @group 67
  */
 public function testRaisesExceptionOnAttachForNonStreamResources()
 {
     $resource = $this->getResourceFor67();
     if (false === $resource) {
         $this->markTestSkipped('No acceptable resource available to test ' . __METHOD__);
     }
     $stream = new Stream(__FILE__);
     $this->setExpectedException('InvalidArgumentException', 'stream');
     $stream->attach($resource);
 }
Exemplo n.º 6
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(), 'diac');
     $upload->moveTo($to);
     $this->assertTrue(file_exists($to));
     $this->setExpectedException('RuntimeException', 'moved');
     $upload->getStream();
 }