Exemple #1
0
 public function testStringContent()
 {
     $this->setExpectedException('photon\\task\\Exception');
     $req = \photon\test\HTTP::baseRequest();
     $res = new Response('A String');
     PhotonDownload::createDownload($req, $res);
 }
Exemple #2
0
 public function testAuth()
 {
     Conf::set('auth_config_users', array('foo' => 'hashed', 'foobar' => Hash::hashPass('secret')));
     $user = \photon\auth\Auth::authenticate(array('login' => 'foobar', 'password' => 'secret'));
     $this->assertEquals('foobar', $user->login);
     $req = \photon\test\HTTP::baseRequest();
     $req->session = array();
     $this->assertEquals(true, \photon\auth\Auth::login($req, $user));
     $this->assertEquals(false, \photon\auth\Auth::login($req, false));
     $user->is_anonymous = true;
     $this->assertEquals(false, \photon\auth\Auth::login($req, $user));
 }
 /**
  * Dump from a URL
  *
  * @param string path of the file to create (i.e. /home/foobar/index.html).
  * @param string url.
  */
 static function fromURL($path, $url)
 {
     echo 'Saving "' . $url . '" into "' . $path . '"' . PHP_EOL;
     // Generate the view content
     $request = \photon\test\HTTP::baseRequest('GET', $url);
     $response = \photon\core\Dispatcher::match($request);
     // If the output is NOT HTML, write the content on disk
     $type = $response->headers['Content-Type'];
     if (strstr($type, 'text/html') === false) {
         file_put_contents($path, $response->content, LOCK_EX);
         return;
     }
     // If the output is HTML, generate extra data to write on disk
     echo 'Downloading extra content and patching links' . PHP_EOL;
     $dom = new \DOMDocument();
     @$dom->loadHTML($response->content);
     // Lot of warning on HTML5 tags like nav
     $xpath = new \DOMXpath($dom);
     foreach ($xpath->query('//script[@src]') as $script) {
         $src = $script->getAttribute('src');
         $script->setAttribute('src', static::downloadExtra($path, $src));
     }
     foreach ($xpath->query('//a[@href]') as $a) {
         $href = $a->getAttribute('href');
         if (substr($href, -1) === '/') {
             continue;
         }
         $a->setAttribute('href', static::downloadExtra($path, $href));
     }
     foreach ($xpath->query('//link[@href]') as $link) {
         $href = $link->getAttribute('href');
         $link->setAttribute('href', static::downloadExtra($path, $href));
     }
     foreach ($xpath->query('//img[@src]') as $img) {
         $src = $img->getAttribute('src');
         $img->setAttribute('src', static::downloadExtra($path, $src));
     }
     $content = $dom->saveHTML();
     file_put_contents($path, $content, LOCK_EX);
 }
Exemple #4
0
 public function testSimpleExchange()
 {
     /*
      * Request 1 :  Receive a request without session cookie
      *              Create a session and store a counter into it
      */
     $req = \photon\test\HTTP::baseRequest();
     $mid = new \photon\session\Middleware();
     $this->assertEquals(false, $mid->process_request($req));
     $req->session['cpt'] = 1234;
     $res = new \photon\http\Response('Hello!');
     $mid->process_response($req, $res);
     $this->assertEquals(true, isset($res->COOKIE['sid']));
     $sid = $this->getSessionId($res);
     unset($req);
     unset($res);
     unset($mid);
     /*
      * Request 2 :  Receive a request with the previous session cookie
      *              Access to the counter
      */
     $req = \photon\test\HTTP::baseRequest('GET', '/', '', '', array(), array('cookie' => 'sid=' . $sid));
     $mid = new \photon\session\Middleware();
     $this->assertEquals(false, $mid->process_request($req));
     $cpt = $req->session['cpt'];
     $this->assertEquals(1234, $cpt);
     $res = new \photon\http\Response('Hello!');
     $mid->process_response($req, $res);
     $this->assertEquals(true, isset($res->headers['Vary']));
     unset($req);
     unset($res);
     unset($mid);
     /*
      * Request 3 :  Receive a request with the previous session cookie
      *              Access to the counter and edit it
      */
     $req = \photon\test\HTTP::baseRequest('GET', '/', '', '', array(), array('cookie' => 'sid=' . $sid));
     $mid = new \photon\session\Middleware();
     $this->assertEquals(false, $mid->process_request($req));
     $cpt = $req->session['cpt'];
     $this->assertEquals(1234, $cpt);
     $req->session['cpt'] = 5678;
     $res = new \photon\http\Response('Hello!');
     $mid->process_response($req, $res);
     $this->assertEquals(true, isset($res->headers['Vary']));
     unset($req);
     unset($res);
     unset($mid);
     /*
      * Request 4 :  Receive a request with the previous session cookie
      *              Access to the new value of the counter
      */
     $req = \photon\test\HTTP::baseRequest('GET', '/', '', '', array(), array('cookie' => 'sid=' . $sid));
     $mid = new \photon\session\Middleware();
     $this->assertEquals(false, $mid->process_request($req));
     $cpt = $req->session['cpt'];
     $this->assertEquals(5678, $cpt);
     $res = new \photon\http\Response('Hello!');
     $mid->process_response($req, $res);
     $this->assertEquals(true, isset($res->headers['Vary']));
     unset($req);
     unset($res);
     unset($mid);
 }
Exemple #5
0
 /**
  * Just testing the high level interface.
  */
 public function testHTTP()
 {
     list($uri, $query) = \photon\test\HTTP::getUriQuery('/', array('foo' => 'bar'));
     $this->assertEquals('/?foo=bar', $uri);
     $this->assertEquals('foo=bar', $query);
 }
Exemple #6
0
 public function testHPKP_enable()
 {
     Conf::set('middleware_security', array('hpkp' => true, 'hpkp_options' => array('pin-sha256' => array("MASTER_KEY_IN_BASE64", "BACKUP_KEY_IN_BASE64"))));
     $req = HTTP::baseRequest('GET', '/');
     list($req, $resp) = Dispatcher::dispatch($req);
     $this->assertEquals(200, $resp->status_code);
     $this->assertArrayHasKey('Public-Key-Pins', $resp->headers);
 }