Example #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'));
 }