Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: URI::setSegment() accepts the last +2 segment without Exception #7251

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ public function getSegment(int $number, string $default = ''): string
*/
public function setSegment(int $number, $value)
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number--;
if ($number < 1) {
throw HTTPException::forURISegmentOutOfRange($number);
}

if ($number > count($this->segments) + 1) {
if ($this->silent) {
Expand All @@ -574,6 +574,10 @@ public function setSegment(int $number, $value)
throw HTTPException::forURISegmentOutOfRange($number);
}

// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number--;

$this->segments[$number] = $value;
$this->refreshPath();

Expand Down
19 changes: 12 additions & 7 deletions tests/system/HTTP/URITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,32 +808,37 @@ public function testSetSegment()
$this->assertSame('foo/banana/baz', $uri->getPath());
}

public function testSetSegmentFallback()
public function testSetSegmentNewOne()
{
$base = 'http://example.com';
$uri = new URI($base);

$uri = new URI($base);
// Can set the next segment.
$uri->setSegment(1, 'first');
$uri->setSegment(3, 'third');
// Can set the next segment.
$uri->setSegment(2, 'third');

$this->assertSame('first/third', $uri->getPath());

// Can replace the existing segment.
$uri->setSegment(2, 'second');

$this->assertSame('first/second', $uri->getPath());

// Can set the next segment.
$uri->setSegment(3, 'third');

$this->assertSame('first/second/third', $uri->getPath());

$uri->setSegment(5, 'fifth');
// Can set the next segment.
$uri->setSegment(4, 'fourth');

$this->assertSame('first/second/third/fifth', $uri->getPath());
$this->assertSame('first/second/third/fourth', $uri->getPath());

// sixth or seventh was not set
// Cannot set the next next segment.
$this->expectException(HTTPException::class);

$uri->setSegment(8, 'eighth');
$uri->setSegment(6, 'six');
}

public function testSetBadSegment()
Expand Down