Exemple #1
0
 public function testInvoke()
 {
     $router = new Router();
     $router->enableRewrite();
     $router->addScheme(new Route\UrlScheme());
     $router->match('foo/bar', 'http://example.com/bar');
     $request1 = Message\Request::create('/foo/bar');
     $response1 = new Message\Response(Message\Status::OK);
     $response2 = $router($request1, $response1);
     $this->assertEquals(Message\Status::SEE_OTHER, $response2->getStatusCode());
     $this->assertEquals('http://example.com/bar', $response2->getHeaderLine('Location'));
     $request2 = Message\Request::create('/foo');
     $this->assertThrows('Jivoo\\Http\\Route\\RouteException', function () use($router, $request2, $response1) {
         $router($request2, $response1);
     });
     // Test removal of trailing slash
     $request3 = Message\Request::create('/foo/');
     $response3 = $router($request3, $response1);
     $this->assertEquals(Message\Status::MOVED_PERMANENTLY, $response3->getStatusCode());
     $this->assertEquals('/foo', $response3->getHeaderLine('Location'));
     $router->disableRewrite();
     // Test index.php redirects
     $request4 = Message\Request::create('/foo/bar')->withServerParams(['SCRIPT_NAME' => '/index.php']);
     $response4 = $router($request4, $response1);
     $this->assertEquals(Message\Status::MOVED_PERMANENTLY, $response4->getStatusCode());
     $this->assertEquals('/index.php/foo/bar', $response4->getHeaderLine('Location'));
     // Test index.php removal from path
     $request5 = Message\Request::create('/index.php/foo/bar')->withServerParams(['SCRIPT_NAME' => '/index.php']);
     $response5 = $router($request5, $response1);
     $this->assertEquals(Message\Status::SEE_OTHER, $response5->getStatusCode());
     $this->assertEquals('http://example.com/bar', $response5->getHeaderLine('Location'));
 }
Exemple #2
0
 public function testFindPath()
 {
     $request = Message\Request::create('/foo/index.php/foo/bar');
     $this->assertEquals(['foo', 'index.php', 'foo', 'bar'], ActionRequest::findPath($request));
     $request = $request->withServerParams(['SCRIPT_NAME' => '/foo/index.php']);
     $this->assertEquals(['index.php', 'foo', 'bar'], ActionRequest::findPath($request));
     $request = $request->withUri(new Message\Uri('/foo'));
     $this->assertEquals([], ActionRequest::findPath($request));
     $request = $request->withUri(new Message\Uri('/foo/'));
     $this->assertEquals([], ActionRequest::findPath($request));
 }
Exemple #3
0
 public function testRouting()
 {
     $router = new \Jivoo\Http\Router();
     $assets = new AssetScheme('tests/data/assets');
     $router->addScheme($assets);
     $router->match('assets/**', 'asset:');
     $route = $router->findMatch(['assets', 'css', 'foo.css'], 'GET');
     $this->assertInstanceOf('Jivoo\\Http\\Route\\AssetRoute', $route);
     $this->assertEquals(['css', 'foo.css'], $route->getParameters());
     $this->assertEquals('asset:css/foo.css', $route->__toString());
     $response = $router(\Jivoo\Http\Message\Request::create('/index.php/assets/css/foo.css'), new Response(Status::OK));
     $this->assertEquals('/* Empty test file used in AssetSchemeTest */', $response->getBody()->getContents());
     $this->assertEquals('text/css', $response->getHeaderLine('Content-Type'));
 }
Exemple #4
0
 /**
  * Construct SAPI server.
  *
  * @param callable|null $handler Optional request handler. Must accept two
  * parameters, a {@see ServerRequestInterface} object and a
  * {@see ResponseInterface} object, and return a {@see ResponseInterface}
  * object.
  * @param ServerRequestInterface|null $request Optional request to handle.
  * The default value is created from PHP's superglobals, see
  * {@see Request::createGlobal}.
  * @param string $path Cookie default path.
  * @param string $domain Cookie default domain.
  */
 public function __construct($handler = null, ServerRequestInterface $request = null, $path = '/', $domain = '')
 {
     parent::__construct();
     if (!isset($handler)) {
         $handler = function (ServerRequestInterface $request, ResponseInterface $response) {
             return $response;
         };
     }
     $this->handler = $handler;
     if (!isset($request)) {
         $request = Request::createGlobal();
     }
     $this->request = $request;
     $this->path = $path;
     $this->domain = $domain;
 }