From e07817bb6244ea33ef5ad31abc4a9288bef3f2f7 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Mon, 18 Apr 2022 21:30:11 +0200 Subject: [PATCH] [Routing] fix router base url when default uri has trailing slash --- RequestContext.php | 2 +- Tests/RequestContextTest.php | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/RequestContext.php b/RequestContext.php index 8994b266..f54c430e 100644 --- a/RequestContext.php +++ b/RequestContext.php @@ -98,7 +98,7 @@ public function getBaseUrl() */ public function setBaseUrl(string $baseUrl) { - $this->baseUrl = $baseUrl; + $this->baseUrl = rtrim($baseUrl, '/'); return $this; } diff --git a/Tests/RequestContextTest.php b/Tests/RequestContextTest.php index 3d23b0e8..179ef33d 100644 --- a/Tests/RequestContextTest.php +++ b/Tests/RequestContextTest.php @@ -40,6 +40,51 @@ public function testConstruct() $this->assertEquals('bar=foobar', $requestContext->getQueryString()); } + public function testFromUriWithBaseUrl() + { + $requestContext = RequestContext::fromUri('https://test.com:444/index.php'); + + $this->assertSame('GET', $requestContext->getMethod()); + $this->assertSame('https', $requestContext->getScheme()); + $this->assertSame('test.com', $requestContext->getHost()); + $this->assertSame('/index.php', $requestContext->getBaseUrl()); + $this->assertSame('/', $requestContext->getPathInfo()); + $this->assertSame(80, $requestContext->getHttpPort()); + $this->assertSame(444, $requestContext->getHttpsPort()); + } + + public function testFromUriWithTrailingSlash() + { + $requestContext = RequestContext::fromUri('http://test.com:8080/'); + + $this->assertSame('http', $requestContext->getScheme()); + $this->assertSame('test.com', $requestContext->getHost()); + $this->assertSame(8080, $requestContext->getHttpPort()); + $this->assertSame(443, $requestContext->getHttpsPort()); + $this->assertSame('', $requestContext->getBaseUrl()); + $this->assertSame('/', $requestContext->getPathInfo()); + } + + public function testFromUriWithoutTrailingSlash() + { + $requestContext = RequestContext::fromUri('https://test.com'); + + $this->assertSame('https', $requestContext->getScheme()); + $this->assertSame('test.com', $requestContext->getHost()); + $this->assertSame('', $requestContext->getBaseUrl()); + $this->assertSame('/', $requestContext->getPathInfo()); + } + + public function testFromUriBeingEmpty() + { + $requestContext = RequestContext::fromUri(''); + + $this->assertSame('http', $requestContext->getScheme()); + $this->assertSame('localhost', $requestContext->getHost()); + $this->assertSame('', $requestContext->getBaseUrl()); + $this->assertSame('/', $requestContext->getPathInfo()); + } + public function testFromRequest() { $request = Request::create('https://test.com:444/foo?bar=baz');