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

[10.x] Fix assertRedirectToRoute when route uri is empty #48023

Merged
merged 2 commits into from Aug 10, 2023

Conversation

khernik93
Copy link
Contributor

Overview

There is a small issue with assertRedirectToRoute adding a trailing slash to the actual redirect url being tested, when the Location header has an empty path, and an existing query string.

Example

Having a route that returns a redirect response, to a route with an empty uri (and at least one parameter):

Route::get('sample-uri', function () {
  return new RedirectResponse(route('route-with-empty-uri', ['foo' => 'bar']));
})->name('perform-redirect');

Route::get('', fn () => 'ok')->name('route-with-empty-uri');

When we try to test the redirection:

$this->get(route('perform-redirect'))
  ->assertRedirectToRoute('route-with-empty-uri', ['foo' => 'bar']);

The actual redirection would be http://localhost?foo=bar, and this is what we expect, but the actual url generated by assertRedirectToRoute is http://localhost/?foo=bar and the test fails.

Problem

It happens due to the $request->fullUrl() adding the trailing slash when both baseUrl and requestUri are empty.

Solution

I changed the:

$request = Request::create($this->headers->get('Location'));

PHPUnit::assertEquals(
    app('url')->to($uri), $request->fullUrl()
);

to:

$this->assertLocation($uri);

which seems to solve the issue and also matches the assertRedirect implementation.

@taylorotwell taylorotwell merged commit fba827a into laravel:10.x Aug 10, 2023
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants