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

[5.7] MethodNotAllowedHTTPException on Intended Redirect #25739

Merged
merged 10 commits into from Sep 23, 2018
10 changes: 9 additions & 1 deletion src/Illuminate/Routing/Redirector.php
Expand Up @@ -82,7 +82,15 @@ public function refresh($status = 302, $headers = [])
*/
public function guest($path, $status = 302, $headers = [], $secure = null)
{
$this->session->put('url.intended', $this->generator->full());
$request = $this->generator->getRequest();

$intended = $request->method() === 'GET' && $request->route() && ! $request->ajax() ?
$this->generator->full() :
$this->generator->previous();

if ($intended) {
$this->session->put('url.intended', $intended);
}

return $this->to($path, $status, $headers, $secure);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Routing/RoutingRedirectorTest.php
Expand Up @@ -19,6 +19,9 @@ public function setUp()
$this->headers = m::mock('Symfony\Component\HttpFoundation\HeaderBag');

$this->request = m::mock('Illuminate\Http\Request');
$this->request->shouldReceive('method')->andReturn('GET')->byDefault();
$this->request->shouldReceive('route')->andReturn(true)->byDefault();
$this->request->shouldReceive('ajax')->andReturn(false)->byDefault();
$this->request->headers = $this->headers;

$this->url = m::mock('Illuminate\Routing\UrlGenerator');
Expand Down Expand Up @@ -70,6 +73,17 @@ public function testGuestPutCurrentUrlInSession()
$this->assertEquals('http://foo.com/login', $response->getTargetUrl());
}

public function testGuestPutPreviousUrlInSession()
{
$this->request->shouldReceive('method')->once()->andReturn('POST');
$this->session->shouldReceive('put')->once()->with('url.intended', 'http://foo.com/bar');
$this->url->shouldReceive('previous')->once()->andReturn('http://foo.com/bar');

$response = $this->redirect->guest('login');

$this->assertEquals('http://foo.com/login', $response->getTargetUrl());
}

public function testIntendedRedirectToIntendedUrlInSession()
{
$this->session->shouldReceive('pull')->with('url.intended', '/')->andReturn('http://foo.com/bar');
Expand Down