/** * Constructor. * * @param Exception $e * @param int $code */ public function __construct(Exception $e, $code = 400) { $this->exception = $e; $this->pushTrace($e); $this->setContent($e->getMessage()); parent::__construct(null, $code); }
public function testResponse() { $expires = new DateTime('+1 hour'); $response = new Response('Demo'); $response->headers->set(new ContentLanguageHeader('en_US')); $response->cookies->set(new SetCookieHeader('username', 'iqbal')); $response->cookies->set((new SetCookieHeader('loggedIn', 'true'))->setExpires($expires)); $response->setContentType('text/html'); $this->assertEquals('text/html', $response->getContentType()); $this->assertTrue($response->isOk()); $this->assertEquals('OK', $response->getStatusText()); $this->assertEquals('Demo', $response->getContent()); $this->assertEquals('HTTP/1.1 200 OK' . "\r\n" . 'Cache-Control: no-cache' . "\r\n" . 'Content-Language: en_US' . "\r\n" . 'Content-Length: 4' . "\r\n" . 'Content-Type: text/html;charset=UTF-8' . "\r\n" . 'Set-Cookie: username=iqbal' . "\r\n" . 'Set-Cookie: loggedIn=true;expires=' . $expires->format(SetCookieHeader::DATE_FORMAT) . "\r\n" . 'Demo', (string) $response); $this->assertEquals(Response::HTTP_VERSION_11, $response->getProtocolVersion()); $this->assertEquals('UTF-8', $response->setCharset('UTF-8')->getCharset()); $this->assertEquals('gzip', $response->setContentEncoding('gzip')->getContentEncoding()); }
/** * {@inheritdoc} */ protected function computeContent() { // Force application/json header. $this->headers->set(new ContentTypeHeader('application/json'), true); return parent::computeContent(); }
/** * {@inheritdoc} */ public function send() { $data = array_merge(array('success' => $this->statusCode >= 200 && $this->statusCode <= 300, 'data' => $this->normalize($this->data)), $this->extra); switch (strtolower($this->request->get('format'))) { case 'debug': $this->setContent(sprintf('<pre>%s</pre>', print_r($data, true))); return parent::send(); break; case 'rjson': $response = new RJsonResponse($data, $this->statusCode, $this->headers->all()); break; default: $response = new JsonResponse($data, $this->statusCode, $this->headers->all()); } return $response->send(); }
/** * Set header location and exit immediately. */ public function sendHeaders() { $this->headers->set(new LocationHeader($this->url), true); return parent::sendHeaders(); }