public function testSendHeadersMergesApplicationAndProblemHttpHeaders()
 {
     $appResponse = new HttpResponse();
     $appResponse->getHeaders()->addHeaderLine('Access-Control-Allow-Origin', '*');
     $listener = new SendApiProblemResponseListener();
     $listener->setApplicationResponse($appResponse);
     ob_start();
     $listener->sendHeaders($this->event);
     ob_get_clean();
     $headers = $this->response->getHeaders();
     $this->assertTrue($headers->has('Access-Control-Allow-Origin'));
 }
 /**
  * @param ContainerInterface $container
  * @return SendApiProblemResponseListener
  */
 public function __invoke(ContainerInterface $container)
 {
     $config = $container->get('config');
     $displayExceptions = isset($config['view_manager']) && isset($config['view_manager']['display_exceptions']) && $config['view_manager']['display_exceptions'];
     $listener = new SendApiProblemResponseListener();
     $listener->setDisplayExceptions($displayExceptions);
     if ($container->has('Response')) {
         $response = $container->get('Response');
         if ($response instanceof HttpResponse) {
             $listener->setApplicationResponse($response);
         }
     }
     return $listener;
 }
 /**
  * {@inheritDoc}
  * @return SendApiProblemResponseListener
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $config = $serviceLocator->get('Config');
     $displayExceptions = isset($config['view_manager']) && isset($config['view_manager']['display_exceptions']) && $config['view_manager']['display_exceptions'];
     $listener = new SendApiProblemResponseListener();
     $listener->setDisplayExceptions($displayExceptions);
     if ($serviceLocator->has('Response')) {
         $response = $serviceLocator->get('Response');
         if ($response instanceof HttpResponse) {
             $listener->setApplicationResponse($response);
         }
     }
     return $listener;
 }
 /**
  * Send HTTP response headers
  *
  * If an application response is composed, and is an HTTP response, merges
  * its headers with the ApiProblemResponse headers prior to sending them.
  *
  * @param  SendResponseEvent $e
  * @return self
  *
  * @throws \Zend\Http\Exception\InvalidArgumentException
  */
 public function sendHeaders(SendResponseEvent $e)
 {
     $response = $e->getResponse();
     if (!$response instanceof ApiProblemResponse) {
         return $this;
     }
     /** @var Request $request */
     $request = $this->getMvcEvent()->getRequest();
     /** @var Accept $accept */
     $accept = $request->getHeader('Accept');
     if ($accept instanceof Accept && $accept->hasMediaType('text/xml')) {
         $headers = $response->getHeaders();
         if ($headers->has('content-type')) {
             $contentTypeHeader = $headers->get('content-type');
             $headers->removeHeader($contentTypeHeader);
         }
         $headers->addHeaderLine('content-type', 'text/xml');
         if ($this->applicationResponse instanceof HttpResponse) {
             $this->mergeHeaders($this->applicationResponse, $response);
         }
     }
     return parent::sendHeaders($e);
 }