Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function handle()
 {
     $uri = '/' . ltrim($this->argument('uri'), '/');
     $method = $this->argument('method') ?: 'GET';
     $add_csrf_tokens = $this->option('tokens');
     $site_url = elgg_get_site_url();
     $uri = substr(elgg_normalize_url($uri), strlen($site_url));
     $path_key = Application::GET_PATH_KEY;
     $parameters = [];
     $query = trim((string) $this->option('query'), '?');
     parse_str($query, $parameters);
     if ($add_csrf_tokens) {
         $ts = time();
         $parameters['__elgg_ts'] = $ts;
         $parameters['__elgg_token'] = _elgg_services()->actions->generateActionToken($ts);
     }
     $request = Request::create("?{$path_key}=" . urlencode($uri), $method, $parameters);
     $cookie_name = _elgg_services()->config->getCookieConfig()['session']['name'];
     $session_id = _elgg_services()->session->getId();
     $request->cookies->set($cookie_name, $session_id);
     $request->headers->set('Referer', elgg_normalize_url());
     if ($this->option('export')) {
         elgg_set_viewtype('json');
         $request->headers->set('X-Elgg-Ajax-API', '2');
     }
     _elgg_services()->setValue('request', $request);
     Application::index();
 }
Ejemplo n.º 2
0
 /**
  * 1. Register a page handler for `/foo`
  * 2. Register a plugin hook that uses the "handler" result param
  *    to route all `/bar/*` requests to the `/foo` handler.
  * 3. Route a request for a `/bar` page.
  * 4. Check that the `/foo` handler was called.
  */
 function testRouteSupportsSettingHandlerInHookResultForBackwardsCompatibility()
 {
     $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
     $this->hooks->registerHandler('route', 'bar', array($this, 'bar_route_handler'));
     $query = http_build_query(array('__elgg_uri' => 'bar/baz'));
     ob_start();
     $this->router->route(\Elgg\Http\Request::create("http://localhost/?{$query}"));
     ob_end_clean();
     $this->assertEquals(1, $this->fooHandlerCalls);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 protected function handle()
 {
     $action = trim($this->argument('action_name'), '/');
     $path_key = Application::GET_PATH_KEY;
     $uri = "action/{$action}";
     $parameters = [];
     $query = trim((string) $this->option('query'), '?');
     parse_str($query, $parameters);
     $ts = time();
     $parameters['__elgg_ts'] = $ts;
     $parameters['__elgg_token'] = _elgg_services()->actions->generateActionToken($ts);
     $request = Request::create("?{$path_key}=" . urlencode($uri), 'POST', $parameters);
     $cookie_name = _elgg_services()->config->getCookieConfig()['session']['name'];
     $session_id = _elgg_services()->session->getId();
     $request->cookies->set($cookie_name, $session_id);
     $request->headers->set('Referer', elgg_normalize_url('cli'));
     $request->headers->set('X-Elgg-Ajax-API', 2);
     elgg_set_viewtype('json');
     _elgg_services()->setValue('request', $request);
     Application::index();
 }
Ejemplo n.º 4
0
 public function createRequest($uri = '', $method = 'POST', $parameters = [], $xhr = false)
 {
     $site_url = elgg_get_site_url();
     $path = substr(elgg_normalize_url($uri), strlen($site_url));
     $path_key = Application::GET_PATH_KEY;
     $request = Request::create("?{$path_key}={$path}", $method, $parameters);
     $cookie_name = $this->config->getCookieConfig()['session']['name'];
     $session_id = $this->session->getId();
     $request->cookies->set($cookie_name, $session_id);
     if ($xhr) {
         $request->headers->set('X-Requested-With', 'XMLHttpRequest');
     }
     return $request;
 }
Ejemplo n.º 5
0
 function testRouteOverridenFromHook()
 {
     $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
     $this->hooks->registerHandler('route', 'foo', array($this, 'bar_route_override'));
     $query = http_build_query(array(Application::GET_PATH_KEY => 'foo'));
     ob_start();
     $this->router->route(\Elgg\Http\Request::create("http://localhost/?{$query}"));
     $result = ob_get_contents();
     ob_end_clean();
     $this->assertEquals("Page handler override from hook", $result);
     $this->assertEquals(0, $this->fooHandlerCalls);
 }
Ejemplo n.º 6
0
 /**
  * @group FileService
  */
 public function testSend400WhenUrlIsMalformmatted()
 {
     $request = \Elgg\Http\Request::create('');
     $response = $this->handler->getResponse($request);
     $this->assertEquals(400, $response->getStatusCode());
 }
Ejemplo n.º 7
0
 /**
  * @group IconService
  */
 public function testCanHandleServeIconRequest()
 {
     $file = new \ElggFile();
     $file->owner_guid = 1;
     $file->setFilename('600x300.jpg');
     $path_key = \Elgg\Application::GET_PATH_KEY;
     $this->request = \Elgg\Http\Request::create("?{$path_key}=serve-icon/{$this->entity->guid}/small");
     $service = $this->createService();
     $service->setCurrentTime();
     $service->saveIconFromElggFile($this->entity, $file);
     $response = $service->handleServeIconRequest(false);
     $icon = $service->getIcon($this->entity, 'small');
     $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('image/jpeg', $response->headers->get('Content-Type'));
     $filesize = filesize($icon->getFilenameOnFilestore());
     $this->assertEquals($filesize, $response->headers->get('Content-Length'));
     $this->assertContains('inline', $response->headers->get('Content-Disposition'));
     $this->assertEquals('"' . $icon->getModifiedTime() . '"', $response->headers->get('Etag'));
     $this->assertEquals($service->getCurrentTime('+1 day'), $response->getExpires());
     $this->assertEquals('max-age=86400, private', $response->headers->get('cache-control'));
     // now try conditional request
     $this->request->headers->set('if-none-match', '"' . $icon->getModifiedTime() . '"');
     $response = $service->handleServeIconRequest(false);
     $this->assertEquals(304, $response->getStatusCode());
     $this->assertEquals($service->getCurrentTime('+1 day'), $response->getExpires());
     $this->assertEquals('max-age=86400, private', $response->headers->get('cache-control'));
 }
Ejemplo n.º 8
0
 /**
  * Create an HTTP request
  *
  * @param string $uri             URI of the request
  * @param string $method          HTTP method
  * @param array  $parameters      Query/Post parameters
  * @param int    $ajax            AJAX api version (0 for non-ajax)
  * @param bool   $add_csrf_tokens Add CSRF tokens
  * @return Request
  */
 public static function prepareHttpRequest($uri = '', $method = 'GET', $parameters = [], $ajax = 0, $add_csrf_tokens = false)
 {
     $site_url = elgg_get_site_url();
     $path = substr(elgg_normalize_url($uri), strlen($site_url));
     $path_key = Application::GET_PATH_KEY;
     if ($add_csrf_tokens) {
         $ts = time();
         $parameters['__elgg_ts'] = $ts;
         $parameters['__elgg_token'] = _elgg_services()->actions->generateActionToken($ts);
     }
     $request = Request::create("?{$path_key}=" . urlencode($path), $method, $parameters);
     $cookie_name = _elgg_services()->config->getCookieConfig()['session']['name'];
     $session_id = _elgg_services()->session->getId();
     $request->cookies->set($cookie_name, $session_id);
     $request->headers->set('Referer', elgg_normalize_url('phpunit'));
     if ($ajax) {
         $request->headers->set('X-Requested-With', 'XMLHttpRequest');
         if ($ajax >= 2) {
             $request->headers->set('X-Elgg-Ajax-API', (string) $ajax);
         }
     }
     return $request;
 }