Пример #1
0
 /**
  * Decide on a route to use and dispatch our module's controller action.
  *
  * @param HttpRequest  $request
  * @param HttpResponse $response
  *
  * @throws \Exception
  *
  * @return HttpResponse
  */
 public function dispatch(HttpRequest $request, HttpResponse $response)
 {
     if (false === $this->booted) {
         $this->boot();
     }
     // Routing
     $routeParams = $this->handleRouting($request);
     $request->attributes->add($routeParams);
     // Resolve our Controller
     $resolver = $this->serviceManager->get('ControllerResolver');
     if (false === ($controller = $resolver->getController($request))) {
         throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s".', $request->getPathInfo()));
     }
     $controllerArguments = $resolver->getArguments($request, $controller);
     $result = call_user_func_array($controller, $controllerArguments);
     if ($result === null) {
         throw new \Exception('Your action returned null. It must always return something');
     } elseif (is_string($result)) {
         $response->setContent($result);
     } elseif ($result instanceof SymfonyResponse || $response instanceof HttpResponse) {
         $response = $result;
     } else {
         throw new \Exception('Invalid response type returned from controller');
     }
     $this->response = $response;
     return $response;
 }
Пример #2
0
 /**
  * @group http
  */
 public function testConstructorIgnoresInvalidHeaders()
 {
     $invalidHeaders = array(array('INVALID'), 'x-invalid-null' => null, 'x-invalid-true' => true, 'x-invalid-false' => false, 'x-invalid-int' => 1, 'x-invalid-object' => (object) array('INVALID'));
     $validHeaders = array('x-valid-string' => array('VALID'), 'x-valid-array' => array('VALID'));
     $response = new Response('', 200, array_merge($invalidHeaders, $validHeaders));
     $responseHeaders = $response->getHeaders();
     foreach (array_keys($invalidHeaders) as $k) {
         $this->assertArrayNotHasKey($k, $responseHeaders);
     }
     foreach ($validHeaders as $k => $v) {
         $this->assertEquals($v, $responseHeaders[$k]);
     }
 }