/**
  *
  * @param ServerRequestInterface $request  A server request object
  * @param ResponseInterface $response A response object
  * @param callable $next The next middleware to be executed
  * @return mixed
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
 {
     // Convert the request/response to CakePHP equivalents.
     $cakeRequest = RequestTransformer::toCake($request);
     $cakeResponse = ResponseTransformer::toCake($response);
     // Dispatch the request/response to CakePHP
     $cakeResponse = $this->getDispatcher()->dispatch($cakeRequest, $cakeResponse);
     // Convert the response back into a PSR7 object.
     return $next($request, ResponseTransformer::toPsr($cakeResponse));
 }
 /**
  * Handle an exception and generate an error response
  *
  * @param \Exception $exception The exception to handle.
  * @param \Psr\Http\Message\ServerRequestInterface $request The request.
  * @param \Psr\Http\Message\ResponseInterface $response The response.
  * @return \Psr\Http\Message\ResponseInterface A response
  */
 public function handleException($exception, $request, $response)
 {
     $renderer = $this->getRenderer($exception);
     try {
         $response = $renderer->render();
         return ResponseTransformer::toPsr($response);
     } catch (Exception $e) {
         $message = sprintf("[%s] %s\n%s", get_class($e), $e->getMessage(), $e->getTraceAsString());
         trigger_error($message, E_USER_ERROR);
     }
     return $response;
 }
 public function testToPsrBodyFileResponseFileRange()
 {
     $_SERVER['HTTP_RANGE'] = 'bytes=10-20';
     $cake = $this->getMock('Cake\\Network\\Response', ['_clearBuffer']);
     $path = dirname(__DIR__) . '/TestApp/asset.css';
     $cake->file($path, ['name' => 'test-asset.css', 'download' => true]);
     $result = ResponseTransformer::toPsr($cake);
     $this->assertEquals('bytes 10-20/38', $result->getHeaderLine('Content-Range'), 'Content-Range header missing');
 }