From 3e062ed3a7aa278e3e7349ffcd5f1da0e498b908 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 5 Sep 2016 15:40:46 +0800 Subject: [PATCH] =?UTF-8?q?Request=E7=B1=BBcreate=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 2 +- tests/thinkphp/library/think/requestTest.php | 189 +++++++++++++++++++ 2 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 tests/thinkphp/library/think/requestTest.php diff --git a/library/think/Request.php b/library/think/Request.php index 5b588cde..d4ce9fa8 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -240,7 +240,7 @@ class Request $options['baseUrl'] = $info['path']; $options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/'); $options['method'] = $server['REQUEST_METHOD']; - $options['domain'] = $server['HTTP_HOST']; + $options['domain'] = $info['scheme'] . '://' . $server['HTTP_HOST']; $options['content'] = $content; self::$instance = new self($options); return self::$instance; diff --git a/tests/thinkphp/library/think/requestTest.php b/tests/thinkphp/library/think/requestTest.php new file mode 100644 index 00000000..25cf094d --- /dev/null +++ b/tests/thinkphp/library/think/requestTest.php @@ -0,0 +1,189 @@ + +// +---------------------------------------------------------------------- + +/** + * Db类测试 + */ + +namespace tests\thinkphp\library\think; + +use think\Config; +use think\Request; +use think\Route; + +class requestTest extends \PHPUnit_Framework_TestCase +{ + protected $request; + + public function setUp() + { + //$request = Request::create('http://www.domain.com/index/index/hello/?name=thinkphp'); + + } + + public function testCreate() + { + $request = Request::create('http://www.thinkphp.cn/index/index/hello.html?name=thinkphp'); + $this->assertEquals('http://www.thinkphp.cn', $request->domain()); + $this->assertEquals('/index/index/hello.html?name=thinkphp', $request->url()); + $this->assertEquals('/index/index/hello.html', $request->baseurl()); + $this->assertEquals('index/index/hello.html', $request->pathinfo()); + $this->assertEquals('index/index/hello', $request->path()); + $this->assertEquals('html', $request->ext()); + $this->assertEquals('name=thinkphp', $request->query()); + $this->assertEquals('www.thinkphp.cn', $request->host()); + $this->assertEquals(80, $request->port()); + $this->assertEquals($_SERVER['REQUEST_TIME'], $request->time()); + $this->assertEquals($_SERVER['REQUEST_TIME_FLOAT'], $request->time(true)); + $this->assertEquals('GET', $request->method()); + $this->assertEquals(['name' => 'thinkphp'], $request->param()); + $this->assertFalse($request->isSsl()); + $this->assertEquals('http', $request->scheme()); + } + + public function testDomain() + { + $request = Request::instance(); + $request->domain('http://thinkphp.cn'); + $this->assertEquals('http://thinkphp.cn', $request->domain()); + } + + public function testUrl() + { + $request = Request::instance(); + $request->url('/index.php/index/hello?name=thinkphp'); + $this->assertEquals('/index.php/index/hello?name=thinkphp', $request->url()); + $this->assertEquals('http://thinkphp.cn/index.php/index/hello?name=thinkphp', $request->url(true)); + } + + public function testBaseUrl() + { + $request = Request::instance(); + $request->baseurl('/index.php/index/hello'); + $this->assertEquals('/index.php/index/hello', $request->baseurl()); + $this->assertEquals('http://thinkphp.cn/index.php/index/hello', $request->baseurl(true)); + } + + public function testbaseFile() + { + $request = Request::instance(); + $request->basefile('/index.php'); + $this->assertEquals('/index.php', $request->basefile()); + $this->assertEquals('http://thinkphp.cn/index.php', $request->basefile(true)); + } + + public function testroot() + { + $request = Request::instance(); + $request->root('/index.php'); + $this->assertEquals('/index.php', $request->root()); + $this->assertEquals('http://thinkphp.cn/index.php', $request->root(true)); + } + + public function testType() + { + $request = Request::instance(); + $_SERVER['HTTP_ACCEPT'] = 'application/json'; + $this->assertEquals('json', $request->type()); + $request->mimeType('test', 'application/test'); + $request->mimeType(['test' => 'application/test']); + $_SERVER['HTTP_ACCEPT'] = 'application/test'; + $this->assertEquals('test', $request->type()); + } + + public function testmethod() + { + $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'DELETE'; + + $request = new Request(); + $this->assertEquals('DELETE', $request->method()); + $this->assertEquals('GET', $request->method(true)); + + Config::set('var_method', '_method'); + $_POST['_method'] = 'POST'; + $request = new Request(); + $this->assertEquals('POST', $request->method()); + $this->assertEquals('GET', $request->method(true)); + $this->assertTrue($request->isPost()); + $this->assertFalse($request->isGet()); + $this->assertFalse($request->isPut()); + $this->assertFalse($request->isDelete()); + $this->assertFalse($request->isHead()); + $this->assertFalse($request->isPatch()); + $this->assertFalse($request->isOptions()); + } + + public function testCli() + { + $request = Request::instance(); + $this->assertTrue($request->isCli()); + } + + public function testVar() + { + Config::set('app_multi_module', true); + $request = new Request(); + $request->route(['name' => 'thinkphp', 'id' => 6]); + $request->get(['id' => 10]); + $request->post(['id' => 8]); + $request->put(['id' => 7]); + $request->request(['test' => 'value']); + $this->assertEquals(['name' => 'thinkphp', 'id' => 6], $request->route()); + //$this->assertEquals(['id' => 10], $request->get()); + $this->assertEquals('thinkphp', $request->route('name')); + $this->assertEquals('default', $request->route('test', 'default')); + $this->assertEquals(10, $request->get('id')); + $this->assertEquals(0, $request->get('ids', 0)); + $this->assertEquals(8, $request->post('id')); + $this->assertEquals(7, $request->put('id')); + $this->assertEquals('value', $request->request('test')); + $this->assertEquals('thinkphp', $request->param('name')); + $this->assertEquals(6, $request->param('id')); + $this->assertFalse($request->has('user_id')); + $this->assertTrue($request->has('test', 'request')); + $this->assertEquals(['id' => 6], $request->only('id')); + $this->assertEquals(['name' => 'thinkphp'], $request->except('id')); + $this->assertEquals('THINKPHP', $request->param('name', '', 'strtoupper')); + } + + public function testIsAjax() + { + $request = new Request(); + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; + $this->assertTrue($request->isAjax()); + } + + public function testIsPjax() + { + $request = new Request(); + $_SERVER['HTTP_X_PJAX'] = true; + $this->assertTrue($request->isPjax()); + } + + public function testIsMobile() + { + $request = new Request(); + $_SERVER['HTTP_VIA'] = 'wap'; + $this->assertTrue($request->isMobile()); + } + + public function testRouteInfo() + { + Config::set('app_multi_module', true); + $request = new Request(); + $request->url('/hello/thinkphp'); + Route::rule('hello/:name', 'index/hello'); + Route::check($request, $request->url()); + $this->assertEquals('', $request->module()); + $this->assertEquals('index', $request->controller()); + $this->assertEquals('hello', $request->action()); + } +}