Example #1
0
 /**
  * @param string $destination
  * @param array $params
  * @param array $post
  *
  * @return \Nette\Application\IResponse
  * @throws \Exception
  */
 protected function check($destination, $params = [], $post = [])
 {
     $destination = ltrim($destination, ':');
     $pos = strrpos($destination, ':');
     $presenter = substr($destination, 0, $pos);
     $action = substr($destination, $pos + 1) ?: 'default';
     $container = \Testbench\ContainerFactory::create(FALSE);
     $container->removeService('httpRequest');
     $headers = $this->__testbench_ajaxMode ? ['X-Requested-With' => 'XMLHttpRequest'] : [];
     $container->addService('httpRequest', new HttpRequestMock(NULL, NULL, [], [], [], $headers));
     $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory');
     $class = $presenterFactory->getPresenterClass($presenter);
     $this->__testbench_presenter = $container->createInstance($class);
     $this->__testbench_presenter->autoCanonicalize = FALSE;
     $this->__testbench_presenter->invalidLinkMode = \Nette\Application\UI\Presenter::INVALID_LINK_EXCEPTION;
     $container->callInjects($this->__testbench_presenter);
     /** @var \Kdyby\FakeSession\Session $session */
     $session = $this->__testbench_presenter->getSession();
     $session->setFakeId('testbench.fakeId');
     $session->getSection('Nette\\Forms\\Controls\\CsrfProtection')->token = 'testbench.fakeToken';
     $post = $post + ['_token_' => 'goVdCQ1jk0UQuVArz15RzkW6vpDU9YqTRILjE='];
     //CSRF magic! ¯\_(ツ)_/¯
     $request = new ApplicationRequestMock($presenter, $post ? 'POST' : 'GET', ['action' => $action] + $params, $post);
     try {
         $this->__testbench_httpCode = 200;
         $response = $this->__testbench_presenter->run($request);
         return $response;
     } catch (\Exception $exc) {
         $this->__testbench_exception = $exc;
         $this->__testbench_httpCode = $exc->getCode();
         throw $exc;
     }
 }
Example #2
0
 /**
  * @return void
  */
 public function processRequest(Request $request)
 {
     process:
     if (count($this->requests) > self::$maxLoop) {
         throw new ApplicationException('Too many loops detected in application life cycle.');
     }
     $this->requests[] = $request;
     $this->onRequest($this, $request);
     if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) {
         throw new BadRequestException('Invalid request. Presenter is not achievable.');
     }
     try {
         $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
     } catch (InvalidPresenterException $e) {
         throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e);
     }
     $this->onPresenter($this, $this->presenter);
     $response = $this->presenter->run(clone $request);
     if ($response instanceof Responses\ForwardResponse) {
         $request = $response->getRequest();
         goto process;
     } elseif ($response) {
         $this->onResponse($this, $response);
         $response->send($this->httpRequest, $this->httpResponse);
     }
 }
Example #3
0
 /**
  * @return void
  */
 public function processRequest(Request $request)
 {
     if (count($this->requests) > self::$maxLoop) {
         throw new ApplicationException('Too many loops detected in application life cycle.');
     }
     $this->requests[] = $request;
     $this->onRequest($this, $request);
     $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
     $response = $this->presenter->run($request);
     if ($response instanceof Responses\ForwardResponse) {
         $this->processRequest($response->getRequest());
     } elseif ($response) {
         $this->onResponse($this, $response);
         $response->send($this->httpRequest, $this->httpResponse);
     }
 }
Example #4
0
 /**
  * Dispatch a HTTP request to a front controller.
  * @return void
  */
 public function run()
 {
     $httpRequest = $this->context->httpRequest;
     $httpResponse = $this->context->httpResponse;
     // check HTTP method
     if ($this->allowedMethods) {
         $method = $httpRequest->getMethod();
         if (!in_array($method, $this->allowedMethods, TRUE)) {
             $httpResponse->setCode(Nette\Http\IResponse::S501_NOT_IMPLEMENTED);
             $httpResponse->setHeader('Allow', implode(',', $this->allowedMethods));
             echo '<h1>Method ' . htmlSpecialChars($method) . ' is not implemented</h1>';
             return;
         }
     }
     // dispatching
     $request = NULL;
     $repeatedError = FALSE;
     do {
         try {
             if (count($this->requests) > self::$maxLoop) {
                 throw new ApplicationException('Too many loops detected in application life cycle.');
             }
             if (!$request) {
                 $this->onStartup($this);
                 // routing
                 $router = $this->getRouter();
                 // enable routing debugger
                 Diagnostics\RoutingPanel::initialize($this, $httpRequest);
                 $request = $router->match($httpRequest);
                 if (!$request instanceof Request) {
                     $request = NULL;
                     throw new BadRequestException('No route for HTTP request.');
                 }
                 if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
                     throw new BadRequestException('Invalid request. Presenter is not achievable.');
                 }
             }
             $this->requests[] = $request;
             $this->onRequest($this, $request);
             // Instantiate presenter
             $presenterName = $request->getPresenterName();
             try {
                 $this->presenter = $this->getPresenterFactory()->createPresenter($presenterName);
             } catch (InvalidPresenterException $e) {
                 throw new BadRequestException($e->getMessage(), 404, $e);
             }
             $this->getPresenterFactory()->getPresenterClass($presenterName);
             $request->setPresenterName($presenterName);
             $request->freeze();
             // Execute presenter
             $response = $this->presenter->run($request);
             $this->onResponse($this, $response);
             // Send response
             if ($response instanceof Responses\ForwardResponse) {
                 $request = $response->getRequest();
                 continue;
             } elseif ($response instanceof IResponse) {
                 $response->send($httpRequest, $httpResponse);
             }
             break;
         } catch (\Exception $e) {
             // fault barrier
             $this->onError($this, $e);
             if (!$this->catchExceptions) {
                 $this->onShutdown($this, $e);
                 throw $e;
             }
             if ($repeatedError) {
                 $e = new ApplicationException('An error occurred while executing error-presenter', 0, $e);
             }
             if (!$httpResponse->isSent()) {
                 $httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500);
             }
             if (!$repeatedError && $this->errorPresenter) {
                 $repeatedError = TRUE;
                 if ($this->presenter instanceof UI\Presenter) {
                     try {
                         $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e));
                     } catch (AbortException $foo) {
                         $request = $this->presenter->getLastCreatedRequest();
                     }
                 } else {
                     $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e));
                 }
                 // continue
             } else {
                 // default error handler
                 if ($e instanceof BadRequestException) {
                     $code = $e->getCode();
                 } else {
                     $code = 500;
                     Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR);
                 }
                 require __DIR__ . '/templates/error.phtml';
                 break;
             }
         }
     } while (1);
     $this->onShutdown($this, isset($e) ? $e : NULL);
 }
 /**
  * Dispatch a HTTP request to a front controller.
  *
  * @return void
  */
 public function run()
 {
     $request = null;
     $repeatedError = false;
     do {
         try {
             if (count($this->requests) > self::$maxLoop) {
                 throw new ApplicationException('Too many loops detected in application life cycle.');
             }
             if (!$request) {
                 $this->onStartup($this);
                 $request = $this->router->match($this->httpRequest);
                 if (!$request instanceof Request) {
                     $request = null;
                     throw new BadRequestException('No route for HTTP request.');
                 }
                 if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
                     throw new BadRequestException('Invalid request. Presenter is not achievable.');
                 }
             }
             $this->requests[] = $request;
             $this->onRequest($this, $request);
             // Instantiate presenter
             $presenterName = $request->getPresenterName();
             try {
                 $this->presenter = $this->presenterFactory->createPresenter($presenterName);
             } catch (InvalidPresenterException $e) {
                 throw new BadRequestException($e->getMessage(), 404, $e);
             }
             $this->presenterFactory->getPresenterClass($presenterName);
             $request->setPresenterName($presenterName);
             $request->freeze();
             // Execute presenter
             $response = $this->presenter->run($request);
             if ($response) {
                 $this->onResponse($this, $response);
             }
             // Send response
             if ($response instanceof Responses\ForwardResponse) {
                 $request = $response->getRequest();
                 continue;
             } elseif ($response instanceof IResponse) {
                 $response->send($this->httpRequest, $this->httpResponse);
             }
             break;
         } catch (\Exception $e) {
             // fault barrier
             $this->onError($this, $e);
             if (!$this->catchExceptions) {
                 $this->onShutdown($this, $e);
                 throw $e;
             }
             if ($repeatedError) {
                 $e = new ApplicationException('An error occurred while executing error-presenter', 0, $e);
             }
             if (!$this->httpResponse->isSent()) {
                 $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500);
             }
             if (!$repeatedError && $this->errorPresenter) {
                 $repeatedError = true;
                 if ($this->presenter instanceof UI\Presenter) {
                     try {
                         $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e));
                     } catch (AbortException $foo) {
                         $request = $this->presenter->getLastCreatedRequest();
                     }
                 } else {
                     $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e));
                 }
                 // continue
             } else {
                 // default error handler
                 if ($e instanceof BadRequestException) {
                     $code = $e->getCode();
                 } else {
                     $code = 500;
                     Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR);
                 }
                 require __DIR__ . '/templates/error.phtml';
                 break;
             }
         }
     } while (1);
     $this->onShutdown($this, isset($e) ? $e : null);
 }
Example #6
0
 /**
  * @param  Application\IPresenter $presenter
  * @param  string                 $presenterName is fully qualified presenter name (module:module:presenter)
  * @param  string                 $action
  * @param  string                 $method
  * @param  array                  $params
  * @param  array                  $post
  * @return Application\IResponse
  */
 private function processRequest(Application\IPresenter $presenter, $presenterName, $action, $method, $params, $post)
 {
     $params['action'] = $action;
     $req = new Application\Request($presenterName, $method, $params, $post);
     return $presenter->run($req);
 }
Example #7
0
 /**
  * @param string $destination fully qualified presenter name (module:module:presenter)
  * @param array $params provided to the presenter usually via URL
  * @param array $post provided to the presenter via POST
  *
  * @return \Nette\Application\IResponse
  * @throws \Exception
  */
 protected function check($destination, $params = [], $post = [])
 {
     $destination = ltrim($destination, ':');
     $pos = strrpos($destination, ':');
     $presenter = substr($destination, 0, $pos);
     $action = substr($destination, $pos + 1) ?: 'default';
     $container = \Testbench\ContainerFactory::create(FALSE);
     $container->removeService('httpRequest');
     $headers = $this->__testbench_ajaxMode ? ['X-Requested-With' => 'XMLHttpRequest'] : [];
     $url = new \Nette\Http\UrlScript($container->parameters['testbench']['url']);
     $container->addService('httpRequest', new Mocks\HttpRequestMock($url, $params, $post, [], [], $headers));
     $presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory');
     $this->__testbench_presenter = $presenterFactory->createPresenter($presenter);
     $this->__testbench_presenter->autoCanonicalize = FALSE;
     $this->__testbench_presenter->invalidLinkMode = \Nette\Application\UI\Presenter::INVALID_LINK_EXCEPTION;
     $postCopy = $post;
     if (isset($params['do'])) {
         foreach ($post as $key => $field) {
             if (is_array($field) && array_key_exists(\Nette\Forms\Form::REQUIRED, $field)) {
                 $post[$key] = $field[0];
             }
         }
     }
     /** @var \Kdyby\FakeSession\Session $session */
     $session = $this->__testbench_presenter->getSession();
     $session->setFakeId('testbench.fakeId');
     $session->getSection('Nette\\Forms\\Controls\\CsrfProtection')->token = 'testbench.fakeToken';
     $post = $post + ['_token_' => 'goVdCQ1jk0UQuVArz15RzkW6vpDU9YqTRILjE='];
     //CSRF magic! ¯\_(ツ)_/¯
     $request = new Mocks\ApplicationRequestMock($presenter, $post ? 'POST' : 'GET', ['action' => $action] + $params, $post);
     try {
         $this->__testbench_httpCode = 200;
         $this->__testbench_exception = NULL;
         $response = $this->__testbench_presenter->run($request);
         if (isset($params['do'])) {
             if (preg_match('~(.+)-submit$~', $params['do'], $matches)) {
                 /** @var \Nette\Application\UI\Form $form */
                 $form = $this->__testbench_presenter->getComponent($matches[1]);
                 foreach ($form->getControls() as $control) {
                     if (array_key_exists($control->getName(), $postCopy)) {
                         $subvalues = $postCopy[$control->getName()];
                         $rq = \Nette\Forms\Form::REQUIRED;
                         if (is_array($subvalues) && array_key_exists($rq, $subvalues) && $subvalues[$rq]) {
                             if ($control->isRequired() !== TRUE) {
                                 Assert::fail("field '{$control->name}' should be defined as required, but it's not");
                             }
                         }
                     }
                     if ($control->hasErrors()) {
                         $errors = '';
                         $counter = 1;
                         foreach ($control->getErrors() as $error) {
                             $errors .= "  - {$error}\n";
                             $counter++;
                         }
                         Assert::fail("field '{$control->name}' returned this error(s):\n{$errors}");
                     }
                 }
                 foreach ($form->getErrors() as $error) {
                     Assert::fail($error);
                 }
             }
         }
         return $response;
     } catch (\Exception $exc) {
         $this->__testbench_exception = $exc;
         $this->__testbench_httpCode = $exc->getCode();
         throw $exc;
     }
 }
Example #8
0
 public function request($method, $params, $post = [], $files = [], $flags = [])
 {
     $request = new Request(static::$presenterName, $method, $params, $post, $files, $flags);
     $response = $this->presenter->run($request);
     return $response;
 }
Example #9
0
 /**
  * Dispatch a HTTP request to a front controller.
  * @return void
  */
 public function run()
 {
     $request = NULL;
     $repeatedError = FALSE;
     do {
         try {
             if (count($this->requests) > self::$maxLoop) {
                 throw new ApplicationException('Too many loops detected in application life cycle.');
             }
             if (!$request) {
                 $this->onStartup($this);
                 $request = $this->router->match($this->httpRequest);
                 if (!$request instanceof Request) {
                     $request = NULL;
                     throw new BadRequestException('No route for HTTP request.');
                 }
                 if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
                     throw new BadRequestException('Invalid request. Presenter is not achievable.');
                 }
             }
             $this->requests[] = $request;
             $this->onRequest($this, $request);
             // Instantiate presenter
             $presenterName = $request->getPresenterName();
             try {
                 $this->presenter = $this->presenterFactory->createPresenter($presenterName);
             } catch (InvalidPresenterException $e) {
                 throw new BadRequestException($e->getMessage(), 404, $e);
             }
             $this->presenterFactory->getPresenterClass($presenterName);
             $request->setPresenterName($presenterName);
             $request->freeze();
             // Execute presenter
             $response = $this->presenter->run($request);
             if ($response) {
                 $this->onResponse($this, $response);
             }
             // Send response
             if ($response instanceof Responses\ForwardResponse) {
                 $request = $response->getRequest();
                 continue;
             } elseif ($response instanceof IResponse) {
                 $response->send($this->httpRequest, $this->httpResponse);
             }
             break;
         } catch (\Exception $e) {
             $this->onError($this, $e);
             if ($repeatedError) {
                 $e = new ApplicationException("An error occurred while executing error-presenter '{$this->errorPresenter}'.", 0, $e);
             }
             if ($repeatedError || !$this->catchExceptions) {
                 $this->onShutdown($this, $e);
                 throw $e;
             }
             $repeatedError = TRUE;
             $this->errorPresenter = $this->errorPresenter ?: 'Nette:Error';
             if (!$this->httpResponse->isSent()) {
                 $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500);
             }
             if ($this->presenter instanceof UI\Presenter) {
                 try {
                     $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e));
                 } catch (AbortException $foo) {
                     $request = $this->presenter->getLastCreatedRequest();
                 }
             } else {
                 $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e));
             }
             // continue
         }
     } while (1);
     $this->onShutdown($this, isset($e) ? $e : NULL);
 }