From b286fa4de1e4c92858a0882cfd01f391f68c98fd Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 17 Mar 2019 19:32:27 +0100 Subject: [PATCH] Fix routing --- Message/Http/Request.php | 4 ++-- Uri/Argument.php | 2 +- Uri/Http.php | 8 ++++---- tests/Message/Http/RequestTest.php | 1 + tests/Uri/HttpTest.php | 8 ++++---- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 2e498d86c..266f2573d 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -263,12 +263,12 @@ final class Request extends RequestAbstract */ public function createRequestHashs(int $start = 0) : void { - $this->hash = []; + $this->hash = [\sha1('')]; $pathArray = $this->uri->getPathElements(); foreach ($pathArray as $key => $path) { $paths = []; - for ($i = $start; $i < $key + 1; ++$i) { + for ($i = $start; $i <= $key; ++$i) { $paths[] = $pathArray[$i]; } diff --git a/Uri/Argument.php b/Uri/Argument.php index 2eee20d95..3e5cd9c76 100644 --- a/Uri/Argument.php +++ b/Uri/Argument.php @@ -203,7 +203,7 @@ final class Argument implements UriInterface $this->path = $path; } - $this->pathElements = \explode('/', $this->path); + $this->pathElements = \explode('/', \ltrim($this->path, '/')); } /** diff --git a/Uri/Http.php b/Uri/Http.php index 0b316e396..e93ed8ce8 100644 --- a/Uri/Http.php +++ b/Uri/Http.php @@ -37,7 +37,7 @@ final class Http implements UriInterface * @var string * @since 1.0.0 */ - private $rootPath = '/'; + private $rootPath = ''; /** * Path offset. @@ -168,7 +168,7 @@ final class Http implements UriInterface $this->port = $url['port'] ?? 80; $this->user = $url['user'] ?? ''; $this->pass = $url['pass'] ?? ''; - $this->path = \ltrim($url['path'] ?? '', '/'); + $this->path = $url['path'] ?? ''; if (StringUtils::endsWith($this->path, '.php')) { $path = \substr($this->path, 0, -4); @@ -180,7 +180,7 @@ final class Http implements UriInterface $this->path = $path; } - $this->pathElements = \explode('/', $this->path); + $this->pathElements = \explode('/', \ltrim($this->path, '/')); $this->queryString = $url['query'] ?? ''; if (!empty($this->queryString)) { @@ -242,7 +242,7 @@ final class Http implements UriInterface */ public function setRootPath(string $root) : void { - $this->rootPath = $root; + $this->rootPath = \rtrim($root, '/'); } /** diff --git a/tests/Message/Http/RequestTest.php b/tests/Message/Http/RequestTest.php index 4a023e93d..ab1c4e81b 100644 --- a/tests/Message/Http/RequestTest.php +++ b/tests/Message/Http/RequestTest.php @@ -74,6 +74,7 @@ class RequestTest extends \PHPUnit\Framework\TestCase $request->createRequestHashs(0); self::assertEquals([ + 'da39a3ee5e6b4b0d3255bfef95601890afd80709', 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3', '328413d996ab9b79af9d4098af3a65b885c4ca64' ], $request->getHash()); diff --git a/tests/Uri/HttpTest.php b/tests/Uri/HttpTest.php index 5ef50502a..fbc346e38 100644 --- a/tests/Uri/HttpTest.php +++ b/tests/Uri/HttpTest.php @@ -50,21 +50,21 @@ class HttpTest extends \PHPUnit\Framework\TestCase { $obj = new Http($uri = 'https://www.google.com/test/path.php?para1=abc¶2=2#frag'); - self::assertEquals('/', $obj->getRootPath()); + self::assertEquals('', $obj->getRootPath()); self::assertEquals(0, $obj->getPathOffset()); self::assertEquals('https', $obj->getScheme()); self::assertEquals('www.google.com', $obj->getHost()); self::assertEquals(80, $obj->getPort()); self::assertEquals('', $obj->getPass()); self::assertEquals('', $obj->getUser()); - self::assertEquals('test/path', $obj->getPath()); - self::assertEquals('test/path?para1=abc¶2=2', $obj->getRoute()); + self::assertEquals('/test/path', $obj->getPath()); + self::assertEquals('/test/path?para1=abc¶2=2', $obj->getRoute()); self::assertEquals('test', $obj->getPathElement(0)); self::assertEquals('para1=abc¶2=2', $obj->getQuery()); self::assertEquals(['para1' => 'abc', 'para2' => '2'], $obj->getQueryArray()); self::assertEquals('2', $obj->getQuery('para2')); self::assertEquals('frag', $obj->getFragment()); - self::assertEquals('https://www.google.com/', $obj->getBase()); + self::assertEquals('https://www.google.com', $obj->getBase()); self::assertEquals($uri, $obj->__toString()); self::assertEquals('www.google.com:80', $obj->getAuthority()); self::assertEquals('', $obj->getUserInfo());