Browse Source

Include leading slash in return value of ServerRequest::getPath().

ADmad 8 years ago
parent
commit
eb9ebae0e1
2 changed files with 8 additions and 11 deletions
  1. 5 8
      src/Http/ServerRequest.php
  2. 3 3
      tests/TestCase/Http/ServerRequestTest.php

+ 5 - 8
src/Http/ServerRequest.php

@@ -2347,21 +2347,18 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
     /**
      * Get the path of current request.
      *
-     * The returned value does not contain leading slash and is identical to
-     * the deprecated ServerRequest::$url property.
-     *
      * @return string
      * @since 3.6.1
      */
     public function getPath()
     {
-        $path = $this->uri->getPath();
-
-        if ($this->requestTarget !== null) {
-            list($path) = explode('?', $this->requestTarget);
+        if ($this->requestTarget === null) {
+            return $this->uri->getPath();
         }
 
-        return substr($path, 1);
+        list($path) = explode('?', $this->requestTarget);
+
+        return $path;
     }
 
     /**

+ 3 - 3
tests/TestCase/Http/ServerRequestTest.php

@@ -219,13 +219,13 @@ class ServerRequestTest extends TestCase
     public function testGetPath()
     {
         $request = new ServerRequest(['url' => '']);
-        $this->assertSame('', $request->getPath());
+        $this->assertSame('/', $request->getPath());
 
         $request = new ServerRequest(['url' => 'some/path?one=something&two=else']);
-        $this->assertEquals('some/path', $request->getPath());
+        $this->assertEquals('/some/path', $request->getPath());
 
         $request = $request->withRequestTarget('/foo/bar?x=y');
-        $this->assertEquals('foo/bar', $request->getPath());
+        $this->assertEquals('/foo/bar', $request->getPath());
     }
 
     /**