1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<?php
namespace Illuminate\Tests\Routing;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\HeaderBag;
class RoutingRedirectorTest extends TestCase
{
protected $headers;
protected $request;
protected $url;
protected $session;
protected $redirect;
protected function setUp(): void
{
$this->headers = m::mock(HeaderBag::class);
$this->request = m::mock(Request::class);
$this->request->shouldReceive('isMethod')->andReturn(true)->byDefault();
$this->request->shouldReceive('method')->andReturn('GET')->byDefault();
$this->request->shouldReceive('route')->andReturn(true)->byDefault();
$this->request->shouldReceive('ajax')->andReturn(false)->byDefault();
$this->request->shouldReceive('expectsJson')->andReturn(false)->byDefault();
$this->request->headers = $this->headers;
$this->url = m::mock(UrlGenerator::class);
$this->url->shouldReceive('getRequest')->andReturn($this->request);
$this->url->shouldReceive('to')->with('bar', [], null)->andReturn('http://foo.com/bar');
$this->url->shouldReceive('to')->with('bar', [], true)->andReturn('https://foo.com/bar');
$this->url->shouldReceive('to')->with('login', [], null)->andReturn('http://foo.com/login');
$this->url->shouldReceive('to')->with('http://foo.com/bar', [], null)->andReturn('http://foo.com/bar');
$this->url->shouldReceive('to')->with('/', [], null)->andReturn('http://foo.com/');
$this->session = m::mock(Store::class);
$this->redirect = new Redirector($this->url);
$this->redirect->setSession($this->session);
}
protected function tearDown(): void
{
m::close();
}
public function testBasicRedirectTo()
{
$response = $this->redirect->to('bar');
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals($this->session, $response->getSession());
}
public function testComplexRedirectTo()
{
$response = $this->redirect->to('bar', 303, ['X-RateLimit-Limit' => 60, 'X-RateLimit-Remaining' => 59], true);
$this->assertSame('https://foo.com/bar', $response->getTargetUrl());
$this->assertEquals(303, $response->getStatusCode());
$this->assertEquals(60, $response->headers->get('X-RateLimit-Limit'));
$this->assertEquals(59, $response->headers->get('X-RateLimit-Remaining'));
}
public function testGuestPutCurrentUrlInSession()
{
$this->url->shouldReceive('full')->andReturn('http://foo.com/bar');
$this->session->shouldReceive('put')->once()->with('url.intended', 'http://foo.com/bar');
$response = $this->redirect->guest('login');
$this->assertSame('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->assertSame('http://foo.com/login', $response->getTargetUrl());
}
public function testIntendedRedirectToIntendedUrlInSession()
{
$this->session->shouldReceive('pull')->with('url.intended', '/')->andReturn('http://foo.com/bar');
$response = $this->redirect->intended();
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testIntendedWithoutIntendedUrlInSession()
{
$this->session->shouldReceive('forget')->with('url.intended');
// without fallback url
$this->session->shouldReceive('pull')->with('url.intended', '/')->andReturn('/');
$response = $this->redirect->intended();
$this->assertSame('http://foo.com/', $response->getTargetUrl());
// with a fallback url
$this->session->shouldReceive('pull')->with('url.intended', 'bar')->andReturn('bar');
$response = $this->redirect->intended('bar');
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testRefreshRedirectToCurrentUrl()
{
$this->request->shouldReceive('path')->andReturn('http://foo.com/bar');
$response = $this->redirect->refresh();
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testBackRedirectToHttpReferer()
{
$this->headers->shouldReceive('has')->with('referer')->andReturn(true);
$this->url->shouldReceive('previous')->andReturn('http://foo.com/bar');
$response = $this->redirect->back();
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testAwayDoesntValidateTheUrl()
{
$response = $this->redirect->away('bar');
$this->assertSame('bar', $response->getTargetUrl());
}
public function testSecureRedirectToHttpsUrl()
{
$response = $this->redirect->secure('bar');
$this->assertSame('https://foo.com/bar', $response->getTargetUrl());
}
public function testAction()
{
$this->url->shouldReceive('action')->with('bar@index', [])->andReturn('http://foo.com/bar');
$response = $this->redirect->action('bar@index');
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testRoute()
{
$this->url->shouldReceive('route')->with('home')->andReturn('http://foo.com/bar');
$this->url->shouldReceive('route')->with('home', [])->andReturn('http://foo.com/bar');
$response = $this->redirect->route('home');
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
$response = $this->redirect->home();
$this->assertSame('http://foo.com/bar', $response->getTargetUrl());
}
public function testItSetsValidIntendedUrl()
{
$this->session->shouldReceive('put')->once()->with('url.intended', 'http://foo.com/bar');
$result = $this->redirect->setIntendedUrl('http://foo.com/bar');
$this->assertNull($result);
}
}
|