Example #1
0
 public function testRebuild()
 {
     $expectedContent = ['foo' => 'bar'];
     $expectedHeaders = new \Phalcon\Http\Response\Headers();
     $expectedHeaders->set('Status', '200 OK');
     $expectedHeaders->set('HTTP/1.1 200 OK', null);
     $this->response->rebuild(['foo' => 'bar']);
     $this->assertEquals($expectedContent, $this->response->getContent());
     $this->assertEquals($expectedHeaders, $this->response->getHeaders());
 }
Example #2
0
 /**
  * @param Response $response
  */
 public function handle(Response $response)
 {
     foreach ($response->getHeaders() as $header) {
         header($header);
     }
     echo $response->getContent();
 }
 /**
  * 格式化指定的响应
  *
  * @param Response $response
  */
 public function format($response)
 {
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $response->charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $response->content = $response->data;
 }
 public function buildMessage(Response $response)
 {
     $str = '';
     $str .= $response->getStatus();
     $str .= "\nURL:\n" . $response->getUrl();
     $str .= "\nPARAMS:\n" . var_export($response->getParams(), true);
     $str .= "\nRESPONSE HEADER:\n" . var_export($response->getHeaders(), true);
     $str .= "\nRESPONSE CONTENT:\n" . substr($response->getContent(), 0, 200);
     return $str;
 }
 /**
  * Verify basic functionality of the response object.
  *
  * @test
  * @covers ::__construct
  * @covers ::getHttpCode
  * @covers ::getHeaders
  * @covers ::getBody
  *
  * @return void
  */
 public function construct()
 {
     $httpCode = 200;
     $headers = ['Content-Type' => 'text/json'];
     $body = ['doesnt' => 'matter'];
     $response = new Response($httpCode, $headers, $body);
     $this->assertSame($httpCode, $response->getHttpCode());
     $this->assertSame($headers, $response->getHeaders());
     $this->assertSame($body, $response->getBody());
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function render(Request $request, Response $response)
 {
     if (!$this->environment->isSilent()) {
         header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage());
         foreach ($response->getHeaders() as $name => $value) {
             header($name . ': ' . $value);
         }
     }
     echo $this->createTemplate($response);
 }
Example #7
0
 /** Renders a response. */
 public final function render(Response $response)
 {
     foreach ($response->getHeaders() as $h) {
         header($h);
     }
     http_response_code($response->getStatus());
     $e = $response->getEntity();
     if (!is_null($e)) {
         $this->renderEntity($e);
     }
 }
Example #8
0
 /**
  * Sends the headers of the response to the client.
  *
  * @return void
  */
 protected function sendHeaders()
 {
     if (!headers_sent()) {
         $status = $this->response->getMessage() ?: $this->response->getStatus();
         header(sprintf("%s %s", $this->response->getProtocol(), $status));
         foreach ($this->response->getHeaders() as $name => $value) {
             header("{$name}: {$value}", true);
         }
         foreach ($this->response->getCookies() as $cookie) {
             header("Set-Cookie: {$cookie}", false);
         }
     }
 }
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format($response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $dom = new DOMDocument($this->version, $charset);
     $root = $dom->createElement($this->rootTag);
     $root->setAttribute('xmlns', $this->xmlns);
     $dom->appendChild($root);
     $this->buildXml($root, $response->data);
     $response->content = $dom->saveXML();
 }
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format($response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if ($this->gzip) {
         $this->contentType = $this->gzipContentType;
     } elseif (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     $dom = new DOMDocument($this->version, $charset);
     $root = $dom->createElement($this->rootTag);
     $root->setAttribute('xmlns', $this->xmlns);
     $dom->appendChild($root);
     $this->buildXml($root, $response->data);
     $xmlData = $dom->saveXML();
     //output
     if ($this->gzip) {
         $response->content = gzencode($xmlData);
         $response->getHeaders()->set('Content-Disposition', "attachment; filename=\"{$this->gzipFilename}\"");
     } else {
         $response->content = $xmlData;
     }
 }
Example #11
0
 public function __construct(Response $parent)
 {
     $status = $parent->getStatus();
     $convertToJson = false;
     $ok = self::statusIsOK($status);
     if ($ok) {
         $contentType = $parent->getHeader('Content-Type');
         if (!empty($contentType)) {
             $convertToJson = stristr($contentType, '/json') !== false;
         }
     }
     $bodyString = $parent->getBody();
     parent::__construct($convertToJson ? json_decode($bodyString) : $bodyString, $parent->getHeaders(), $status);
     $this->ok = $ok;
 }
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  * @throws \RuntimeException
  */
 public function format($response)
 {
     $response->getHeaders()->set('Content-Type', 'text/csv; charset=UTF-8');
     $handle = fopen('php://temp/maxmemory:' . intval($this->maxMemory), 'w+');
     $response->stream = $handle;
     if ($this->includeColumnNames && $this->checkAllRows) {
         $columns = $this->getColumnNames($response->data);
         if (empty($columns)) {
             return;
         }
         $outputHeader = false;
         $this->put($handle, $columns);
     } else {
         $outputHeader = true;
     }
     if (!$response->data instanceof \Traversable && !is_array($response->data)) {
         throw new \InvalidArgumentException('Response data must be traversable.');
     }
     foreach ($response->data as $row) {
         if ($outputHeader && $this->includeColumnNames && !$this->checkAllRows && \yii\helpers\ArrayHelper::isAssociative($row)) {
             $this->put($handle, array_keys($row));
             $outputHeader = false;
         }
         if ($row instanceof Arrayable) {
             $row = $row->toArray();
         }
         $rowData = [];
         if (isset($columns)) {
             // Map columns.
             foreach ($columns as $column) {
                 if (array_key_exists($column, $row)) {
                     $rowData[] = isset($row[$column]) ? $row[$column] : $this->nullValue;
                 } else {
                     $rowData[] = $this->missingValue;
                 }
             }
         } else {
             foreach ($row as $column => $value) {
                 $rowData[] = isset($value) ? $value : $this->nullValue;
             }
         }
         $this->put($handle, $rowData);
     }
     rewind($handle);
 }
Example #13
0
 /**
  * Formats the specified response.
  * @param Response $response the response to be formatted.
  */
 public function format($response)
 {
     $charset = $this->encoding === null ? $response->charset : $this->encoding;
     if (stripos($this->contentType, 'charset') === false) {
         $this->contentType .= '; charset=' . $charset;
     }
     $response->getHeaders()->set('Content-Type', $this->contentType);
     if ($response->data !== null) {
         $dom = new DOMDocument($this->version, $charset);
         $root = new DOMElement($this->rootTag);
         $result = new DOMElement($this->itemTag);
         $code = new DOMElement($this->itemTag);
         $status = new DOMElement($this->itemTag);
         $dom->appendChild($root);
         $code->appendChild($response->getStatusCode());
         $status->appendChild($response->getIsSuccessful() ? 'success' : 'error');
         $messageText = '';
         if (!$response->getIsOk()) {
             $messageText = $response->statusText;
         }
         if (is_string($response->data)) {
             //For string result we send it like 'message'
             $messageText = $response->data;
         } elseif ($response->getIsClientError() && isset($response->data['message'])) {
             //For HttpExceptions we save message field only to 'message'
             $messageText = $response->data['message'];
             unset($response->data['message']);
             $result->appendChild($response->data);
         } else {
             //Otherwise send all as result
             $result->appendChild($response->data);
         }
         if ($messageText !== '') {
             $message = new DOMElement($this->itemTag);
             $message->appendChild($messageText);
             $root->appendChild($message);
         }
         $root->appendChild($result);
         $root->appendChild($code);
         $root->appendChild($status);
         $this->buildXml($root, $response->data);
         $response->content = $dom->saveXML();
     }
 }
Example #14
0
 /**
  * @param int $status_code OPTIONAL Defaults to `200`.
  * @param boolean $read_all_buffers OPTIONAL Whether to get and clean the content of all open buffers.
  *                                  Defaults to `true`.
  */
 public static function createFromBuffer($status_code = 200, $read_all_buffers = true)
 {
     # Get our body from the buffer
     $body = "";
     while (($more_body = ob_get_clean()) !== false) {
         $body .= $more_body;
         if (!$read_all_buffers) {
             break;
         }
     }
     $response = new Response($body, $status_code);
     # Add all headers in the buffer and guess response encoding.
     $response->addSentHeaders();
     foreach ($response->getHeaders() as $header) {
         if (preg_match("/^Content-Type:.+;\\s*charset=(.+)\$/i", $header, $matches) === 0) {
             continue;
         }
         $response->setEncoding($matches[1]);
     }
     return $response;
 }
Example #15
0
 public function testConstructHeaders()
 {
     $response = new Response(["test" => ["value"]]);
     $this->assertEquals(["test" => ["value"]], $response->getHeaders());
 }
 /**
  * Use Response object to modify headers and output body
  * @param  Response $response
  */
 private function respond(Response $response)
 {
     foreach ($response->getHeaders() as $header) {
         header($header);
     }
     echo $response->getBody();
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function encodeResponse(Response $response) : string
 {
     return sprintf("HTTP/%s %d %s\r\n%s\r\n", $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase(), $this->encodeHeaders($response->getHeaders()));
 }
 /**
  * @covers  Response::addHeader
  * @depends testNoHeadersAreInitiallySet
  */
 public function testAddingHeadersWorks(Response $response)
 {
     $response->addHeader('HTTP/1.0 404 Not Found');
     $this->assertContains('HTTP/1.0 404 Not Found', $response->getHeaders());
 }
Example #19
0
 /**
  * Format the content type of the response.
  * @param Response $response
  */
 public function format($response)
 {
     $response->getHeaders()->set('Content-Type', $this->contentType);
 }
 public function testSetHeaders()
 {
     $this->object->setHeaders(array('Location' => 'http://www.example.com/', 'Content-type' => 'text/html'));
     $this->assertThat($this->object->getHeaders(), $this->equalTo(array('location' => 'http://www.example.com/', 'content-type' => 'text/html')));
 }