Skip to content

Commit 45ebc37

Browse files
committed
refactor: update handling of paths
1 parent cdfb6aa commit 45ebc37

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

packages/support/src/Uri/Uri.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private function with(
229229
password: $password ?? $this->password,
230230
host: $host ?? $this->host,
231231
port: $port ?? $this->port,
232-
path: Str\ensure_starts_with($path ?? $this->path, '/'),
232+
path: $path ?? $this->path,
233233
queryString: match (true) {
234234
$queryString !== null => $queryString,
235235
$query !== [] => $this->buildQueryString($query),
@@ -302,7 +302,13 @@ public function toString(): string
302302
}
303303

304304
if ($this->path !== null) {
305-
$uri .= $this->path;
305+
$path = $this->path;
306+
307+
if ($this->host !== null) {
308+
$path = Str\ensure_starts_with($path, '/');
309+
}
310+
311+
$uri .= $path;
306312
}
307313

308314
if ($this->queryString !== null && $this->queryString !== '') {

packages/support/tests/Uri/FunctionsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class FunctionsTest extends TestCase
3939
#[TestWith(['https://example.com', ['foo'], 'https://example.com?foo'])]
4040
#[TestWith(['https://example.com', ['foo' => true], 'https://example.com?foo=true'])]
4141
#[TestWith(['https://example.com', ['foo' => false], 'https://example.com?foo=false'])]
42+
#[TestWith(['https://example.com', ['foo' => ['bar' => 'baz']], 'https://example.com?foo%5Bbar%5D=baz'])]
4243
public function set_query(string $uri, array $query, string $expected): void
4344
{
4445
$this->assertSame($expected, set_query($uri, ...$query));
@@ -79,7 +80,7 @@ public function get_fragment(string $uri, ?string $expected): void
7980
#[TestWith(['https://example.com', 'flamme.org', 'https://flamme.org'])]
8081
#[TestWith(['https://old.com/path', 'neue.com', 'https://neue.com/path'])]
8182
#[TestWith(['http://user:[email protected]:8080', 'serie.com', 'http://user:[email protected]:8080'])]
82-
#[TestWith(['malformed-uri', 'domain.com', '//domain.commalformed-uri'])]
83+
#[TestWith(['malformed-uri', 'domain.com', '//domain.com/malformed-uri'])]
8384
public function set_host(string $uri, string $host, string $expected): void
8485
{
8586
$this->assertSame($expected, set_host($uri, $host));

packages/support/tests/Uri/UriTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,11 @@ public function path_manipulation(): void
103103

104104
$clone = $uri->withPath('foo/bar');
105105
$this->assertSame('https://example.com/foo/bar', $clone->toString());
106-
$this->assertSame('/foo/bar', $clone->path);
106+
$this->assertSame('foo/bar', $clone->path);
107107

108108
$this->assertNull($uri->path);
109109
}
110110

111-
#[Test]
112-
#[TestWith(['/foo/bar', '/foo/bar'])]
113-
#[TestWith(['foo/bar', '/foo/bar'])]
114-
#[TestWith(['foo-bar', '/foo-bar'])]
115-
public function path_prefix(string $path, string $expected): void
116-
{
117-
$this->assertSame($expected, Uri::from('https://example.com')->withPath($path)->path);
118-
}
119-
120111
#[Test]
121112
public function query_manipulation(): void
122113
{

0 commit comments

Comments
 (0)