Commit 1eb1809a by Qiang Xue

finished URL testing.

parent 2c991833
...@@ -409,6 +409,11 @@ class Request extends \yii\base\Request ...@@ -409,6 +409,11 @@ class Request extends \yii\base\Request
return $this->_pathInfo; return $this->_pathInfo;
} }
public function setPathInfo($value)
{
$this->_pathInfo = trim($value, '/');
}
/** /**
* Resolves the path info part of the currently requested URL. * Resolves the path info part of the currently requested URL.
* A path info refers to the part that is after the entry script and before the question mark (query string). * A path info refers to the part that is after the entry script and before the question mark (query string).
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace yii\web; namespace yii\web;
use Yii; use Yii;
use \yii\base\Component; use yii\base\Component;
/** /**
* UrlManager handles HTTP request parsing and creation of URLs based on a set of rules. * UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
...@@ -141,8 +141,11 @@ class UrlManager extends Component ...@@ -141,8 +141,11 @@ class UrlManager extends Component
return array($pathInfo, array()); return array($pathInfo, array());
} else { } else {
$route = (string)$request->getParam($this->routeVar); $route = $request->getParam($this->routeVar);
return array($route, array()); if (is_array($route)) {
$route = '';
}
return array((string)$route, array());
} }
} }
...@@ -230,9 +233,7 @@ class UrlManager extends Component ...@@ -230,9 +233,7 @@ class UrlManager extends Component
public function getHostInfo() public function getHostInfo()
{ {
if ($this->_hostInfo === null) { if ($this->_hostInfo === null) {
/** @var $request \yii\web\Request */ $this->_hostInfo = Yii::$app->getRequest()->getHostInfo();
$request = Yii::$app->getRequest();
$this->_hostInfo = $request->getHostInfo();
} }
return $this->_hostInfo; return $this->_hostInfo;
} }
......
<?php <?php
namespace yiiunit\framework\web; namespace yiiunit\framework\web;
use yii\web\Application; use yii\web\Request;
use yii\web\UrlManager; use yii\web\UrlManager;
class UrlManagerTest extends \yiiunit\TestCase class UrlManagerTest extends \yiiunit\TestCase
{ {
public function testCreateUrl() public function testCreateUrl()
{ {
new Application('test', __DIR__ . '/../..');
// default setting with '/' as base url // default setting with '/' as base url
$manager = new UrlManager(array( $manager = new UrlManager(array(
'baseUrl' => '/', 'baseUrl' => '/',
...@@ -50,6 +49,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -50,6 +49,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules // pretty URL with rules
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cacheID' => false,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
...@@ -66,6 +66,7 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -66,6 +66,7 @@ class UrlManagerTest extends \yiiunit\TestCase
// pretty URL with rules and suffix // pretty URL with rules and suffix
$manager = new UrlManager(array( $manager = new UrlManager(array(
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'cacheID' => false,
'rules' => array( 'rules' => array(
array( array(
'pattern' => 'post/<id>/<title>', 'pattern' => 'post/<id>/<title>',
...@@ -83,11 +84,118 @@ class UrlManagerTest extends \yiiunit\TestCase ...@@ -83,11 +84,118 @@ class UrlManagerTest extends \yiiunit\TestCase
public function testCreateAbsoluteUrl() public function testCreateAbsoluteUrl()
{ {
$manager = new UrlManager(array(
'baseUrl' => '/',
'hostInfo' => 'http://www.example.com',
));
$url = $manager->createAbsoluteUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('http://www.example.com/?r=post/view&id=1&title=sample+post', $url);
} }
public function testParseRequest() public function testParseRequest()
{ {
$manager = new UrlManager;
$request = new Request;
// default setting without 'r' param
unset($_GET['r']);
$result = $manager->parseRequest($request);
$this->assertEquals(array('', array()), $result);
// default setting with 'r' param
$_GET['r'] = 'site/index';
$result = $manager->parseRequest($request);
$this->assertEquals(array('site/index', array()), $result);
// default setting with 'r' param as an array
$_GET['r'] = array('site/index');
$result = $manager->parseRequest($request);
$this->assertEquals(array('', array()), $result);
// pretty URL without rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
));
// empty pathinfo
$request->pathInfo = '';
$result = $manager->parseRequest($request);
$this->assertEquals(array('', array()), $result);
// normal pathinfo
$request->pathInfo = 'site/index';
$result = $manager->parseRequest($request);
$this->assertEquals(array('site/index', array()), $result);
// pathinfo with module
$request->pathInfo = 'module/site/index';
$result = $manager->parseRequest($request);
$this->assertEquals(array('module/site/index', array()), $result);
// pathinfo with trailing slashes
$request->pathInfo = 'module/site/index/';
$result = $manager->parseRequest($request);
$this->assertEquals(array('module/site/index', array()), $result);
// pretty URL rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cacheID' => false,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
));
// matching pathinfo
$request->pathInfo = 'post/123/this+is+sample';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result);
// matching pathinfo with trailing slashes
$request->pathInfo = 'post/123/this+is+sample/';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result);
// empty pathinfo
$request->pathInfo = '';
$result = $manager->parseRequest($request);
$this->assertEquals(array('', array()), $result);
// normal pathinfo
$request->pathInfo = 'site/index';
$result = $manager->parseRequest($request);
$this->assertEquals(array('site/index', array()), $result);
// pathinfo with module
$request->pathInfo = 'module/site/index';
$result = $manager->parseRequest($request);
$this->assertEquals(array('module/site/index', array()), $result);
// pretty URL rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'suffix' => '.html',
'cacheID' => false,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
));
// matching pathinfo
$request->pathInfo = 'post/123/this+is+sample.html';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result);
// matching pathinfo without suffix
$request->pathInfo = 'post/123/this+is+sample';
$result = $manager->parseRequest($request);
$this->assertFalse($result);
// empty pathinfo
$request->pathInfo = '';
$result = $manager->parseRequest($request);
$this->assertEquals(array('', array()), $result);
// normal pathinfo
$request->pathInfo = 'site/index.html';
$result = $manager->parseRequest($request);
$this->assertEquals(array('site/index', array()), $result);
// pathinfo without suffix
$request->pathInfo = 'site/index';
$result = $manager->parseRequest($request);
$this->assertFalse($result);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment