/
RedirectRouteTest.php
112 lines (99 loc) · 4.22 KB
/
RedirectRouteTest.php
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
<?php
/**
* Request Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Routing.Route
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Routing\Route;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Network\Response;
use Cake\Routing\Route\RedirectRoute;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
/**
* test case for RedirectRoute
*
* @package Cake.Test.Case.Routing.Route
*/
class RedirectRouteTest extends TestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
Router::reload();
}
/**
* test the parsing of routes.
*
* @return void
*/
public function testParsing() {
$route = new RedirectRoute('/home', array('controller' => 'posts'));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/home');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts', true), $header['Location']);
$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/home');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts', true), $header['Location']);
$this->assertEquals(301, $route->response->statusCode());
$route = new RedirectRoute('/google', 'http://google.com');
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/google');
$header = $route->response->header();
$this->assertEquals('http://google.com', $header['Location']);
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts/view', true), $header['Location']);
$this->assertEquals(302, $route->response->statusCode());
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/posts/view/2', true), $header['Location']);
$route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/posts/2');
$header = $route->response->header();
$this->assertEquals(Router::url('/test', true), $header['Location']);
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add/passme', true), $header['Location']);
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
$route->stop = false;
$route->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme');
$header = $route->response->header();
$this->assertEquals(Router::url('/tags/add', true), $header['Location']);
}
}