diff --git a/RequestContext.php b/RequestContext.php index 3ced6550..e3f4831b 100644 --- a/RequestContext.php +++ b/RequestContext.php @@ -96,7 +96,7 @@ public function getBaseUrl(): string */ public function setBaseUrl(string $baseUrl): static { - $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');