123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit\Request;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\Request\Http;
- /**
- * @SuppressWarnings(PHPMD.TooManyMethods)
- * @SuppressWarnings(PHPMD.TooManyPublicMethods)
- */
- class HttpTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\App\Request\Http
- */
- private $model;
- /**
- * @var \Magento\Framework\App\Route\ConfigInterface\Proxy | \PHPUnit_Framework_MockObject_MockObject
- */
- private $routerListMock;
- /**
- * @var \Magento\Framework\App\Request\PathInfoProcessorInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- private $infoProcessorMock;
- /**
- * @var \Magento\Framework\App\Request\PathInfo
- */
- private $pathInfo;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
- */
- private $objectManagerMock;
- /**
- * @var \Magento\Framework\Stdlib\StringUtils | \PHPUnit_Framework_MockObject_MockObject
- */
- private $converterMock;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- private $objectManager;
- /**
- * @var array
- */
- private $serverArray;
- protected function setUp()
- {
- $this->routerListMock = $this->createPartialMock(
- \Magento\Framework\App\Route\ConfigInterface\Proxy::class,
- ['getRouteFrontName', 'getRouteByFrontName', '__wakeup']
- );
- $this->infoProcessorMock = $this->createMock(\Magento\Framework\App\Request\PathInfoProcessorInterface::class);
- $this->infoProcessorMock->expects($this->any())->method('process')->will($this->returnArgument(1));
- $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $this->converterMock = $this->getMockBuilder(\Magento\Framework\Stdlib\StringUtils::class)
- ->disableOriginalConstructor()
- ->setMethods(['cleanString'])
- ->getMock();
- $this->converterMock->expects($this->any())->method('cleanString')->will($this->returnArgument(0));
- // Stash the $_SERVER array to protect it from modification in test
- $this->serverArray = $_SERVER;
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->pathInfo = $this->objectManager->getObject(\Magento\Framework\App\Request\PathInfo::class);
- }
- public function tearDown()
- {
- $_SERVER = $this->serverArray;
- }
- /**
- * @return \Magento\Framework\App\Request\Http
- */
- private function getModel($uri = null, $appConfigMock = true)
- {
- $model = $this->objectManager->getObject(
- \Magento\Framework\App\Request\Http::class,
- [
- 'routeConfig' => $this->routerListMock,
- 'pathInfoProcessor' => $this->infoProcessorMock,
- 'pathInfoService' => $this->pathInfo,
- 'objectManager' => $this->objectManagerMock,
- 'converter' => $this->converterMock,
- 'uri' => $uri,
- ]
- );
- if ($appConfigMock) {
- $configMock = $this->createMock(\Magento\Framework\App\Config::class);
- $this->objectManager->setBackwardCompatibleProperty($model, 'appConfig', $configMock);
- }
- return $model;
- }
- public function testGetOriginalPathInfoWithTestUri()
- {
- $uri = 'http://test.com/value?key=value';
- $this->model = $this->getModel($uri);
- $this->assertEquals('/value', $this->model->getOriginalPathInfo());
- }
- public function testGetOriginalPathInfoWithEmptyUri()
- {
- $this->model = $this->getModel();
- $this->assertEmpty($this->model->getOriginalPathInfo());
- }
- public function testGetBasePathWithPath()
- {
- $this->model = $this->getModel();
- $this->model->setBasePath('http:\/test.com\one/two');
- $this->assertEquals('http://test.com/one/two', $this->model->getBasePath());
- }
- public function testGetBasePathWithoutPath()
- {
- $this->model = $this->getModel();
- $this->model->setBasePath(null);
- $this->assertEquals('/', $this->model->getBasePath());
- }
- public function testSetRouteNameWithRouter()
- {
- $router = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
- $this->routerListMock->expects($this->any())->method('getRouteFrontName')->will($this->returnValue($router));
- $this->model = $this->getModel();
- $this->model->setRouteName('RouterName');
- $this->assertEquals('RouterName', $this->model->getRouteName());
- }
- public function testSetRouteNameWithNullRouterValue()
- {
- $this->model = $this->getModel();
- $this->routerListMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue(null));
- $this->model->setRouteName('RouterName');
- }
- public function testGetFrontName()
- {
- $uri = 'http://test.com/one/two';
- $this->model = $this->getModel($uri);
- $this->assertEquals('one', $this->model->getFrontName());
- }
- public function testGetRouteNameWithNullValueRouteName()
- {
- $this->model = $this->getModel();
- $this->model->setRouteName('RouteName');
- $this->assertEquals('RouteName', $this->model->getRouteName());
- }
- public function testGetRouteName()
- {
- $this->model = $this->getModel();
- $expected = 'RouteName';
- $this->model->setRouteName($expected);
- $this->assertEquals($expected, $this->model->getRouteName());
- }
- public function testGetFullActionName()
- {
- $this->model = $this->getModel();
- /* empty request */
- $this->assertEquals('__', $this->model->getFullActionName());
- $this->model->setRouteName('test')->setControllerName('controller')->setActionName('action');
- $this->assertEquals('test/controller/action', $this->model->getFullActionName('/'));
- }
- public function testInitForward()
- {
- $expected = $this->initForward();
- $this->assertEquals($expected, $this->model->getBeforeForwardInfo());
- }
- public function testGetBeforeForwardInfo()
- {
- $beforeForwardInfo = $this->initForward();
- $this->assertNull($this->model->getBeforeForwardInfo('not_existing_forward_info_key'));
- foreach (array_keys($beforeForwardInfo) as $key) {
- $this->assertEquals($beforeForwardInfo[$key], $this->model->getBeforeForwardInfo($key));
- }
- $this->assertEquals($beforeForwardInfo, $this->model->getBeforeForwardInfo());
- }
- /**
- * Initialize $_beforeForwardInfo
- *
- * @return array Contents of $_beforeForwardInfo
- */
- private function initForward()
- {
- $this->model = $this->getModel();
- $beforeForwardInfo = [
- 'params' => ['one' => '111', 'two' => '222'],
- 'action_name' => 'ActionName',
- 'controller_name' => 'ControllerName',
- 'module_name' => 'ModuleName',
- 'route_name' => 'RouteName'
- ];
- $this->model->setParams($beforeForwardInfo['params']);
- $this->model->setActionName($beforeForwardInfo['action_name']);
- $this->model->setControllerName($beforeForwardInfo['controller_name']);
- $this->model->setModuleName($beforeForwardInfo['module_name']);
- $this->model->setRouteName($beforeForwardInfo['route_name']);
- $this->model->initForward();
- return $beforeForwardInfo;
- }
- public function testIsAjax()
- {
- $this->model = $this->getModel();
- $this->assertFalse($this->model->isAjax());
- $this->model->clearParams();
- $this->model->setParam('ajax', 1);
- $this->assertTrue($this->model->isAjax());
- $this->model->clearParams();
- $this->model->setParam('isAjax', 1);
- $this->assertTrue($this->model->isAjax());
- $this->model->clearParams();
- $this->model->getHeaders()->addHeaderLine('X-Requested-With', 'XMLHttpRequest');
- $this->assertTrue($this->model->isAjax());
- $this->model->getHeaders()->clearHeaders();
- $this->model->getHeaders()->addHeaderLine('X-Requested-With', 'NotXMLHttpRequest');
- $this->assertFalse($this->model->isAjax());
- }
- /**
- * @param array $serverVariables
- * @param string $expectedResult
- * @dataProvider serverVariablesProvider
- */
- public function testGetDistroBaseUrl($serverVariables, $expectedResult)
- {
- $originalServerValue = $_SERVER;
- $_SERVER = $serverVariables;
- $this->model = $this->getModel();
- $this->assertEquals($expectedResult, $this->model->getDistroBaseUrl());
- $_SERVER = $originalServerValue;
- }
- /**
- * @param string $scriptName
- * @param string $expected
- * @dataProvider getDistroBaseUrlPathDataProvider
- */
- public function testGetDistroBaseUrlPath($scriptName, $expected)
- {
- $this->assertEquals($expected, Http::getDistroBaseUrlPath(['SCRIPT_NAME' => $scriptName]));
- }
- /**
- * @return array
- */
- public function getDistroBaseUrlPathDataProvider()
- {
- return [
- [null, '/'],
- ['./index.php', '/'],
- ['.\\index.php', '/'],
- ['/index.php', '/'],
- ['\\index.php', '/'],
- ['subdir/script.php', 'subdir/'],
- ['subdir\\script.php', 'subdir/'],
- ['sub\\dir\\script.php', 'sub/dir/'],
- ];
- }
- /**
- * @return array
- */
- public function serverVariablesProvider()
- {
- $returnValue = [];
- $defaultServerData = [
- 'SCRIPT_NAME' => 'index.php',
- 'HTTP_HOST' => 'sample.host.com',
- 'SERVER_PORT' => '80',
- 'HTTPS' => '1'
- ];
- $secureUnusualPort = $noHttpsData = $httpsOffData = $noHostData = $noScriptNameData = $defaultServerData;
- unset($noScriptNameData['SCRIPT_NAME']);
- $returnValue['no SCRIPT_NAME'] = [$noScriptNameData, 'http://localhost/'];
- unset($noHostData['HTTP_HOST']);
- $returnValue['no HTTP_HOST'] = [$noHostData, 'http://localhost/'];
- $httpsOffData['HTTPS'] = 'off';
- $returnValue['HTTPS off'] = [$httpsOffData, 'http://sample.host.com/'];
- unset($noHttpsData['HTTPS']);
- $returnValue['no HTTPS'] = [$noHttpsData, 'http://sample.host.com/'];
- $noHttpsNoServerPort = $noHttpsData;
- unset($noHttpsNoServerPort['SERVER_PORT']);
- $returnValue['no SERVER_PORT'] = [$noHttpsNoServerPort, 'http://sample.host.com/'];
- $noHttpsButSecurePort = $noHttpsData;
- $noHttpsButSecurePort['SERVER_PORT'] = 443;
- $returnValue['no HTTP but secure port'] = [$noHttpsButSecurePort, 'https://sample.host.com/'];
- $notSecurePort = $noHttpsData;
- $notSecurePort['SERVER_PORT'] = 81;
- $notSecurePort['HTTP_HOST'] = 'sample.host.com:81';
- $returnValue['not secure not standard port'] = [$notSecurePort, 'http://sample.host.com:81/'];
- $secureUnusualPort['SERVER_PORT'] = 441;
- $secureUnusualPort['HTTP_HOST'] = 'sample.host.com:441';
- $returnValue['not standard secure port'] = [$secureUnusualPort, 'https://sample.host.com:441/'];
- $customUrlPathData = $noHttpsData;
- $customUrlPathData['SCRIPT_FILENAME'] = '/some/dir/custom.php';
- $returnValue['custom path'] = [$customUrlPathData, 'http://sample.host.com/'];
- return $returnValue;
- }
- /**
- * @dataProvider isSecureDataProvider
- *
- * @param bool $isSecure expected output of isSecure method
- * @param string $serverHttps value of $_SERVER['HTTPS']
- * @param string $headerOffloadKey <Name-Of-Offload-Header>
- * @param string $headerOffloadValue value of $_SERVER[<Name-Of-Offload-Header>]
- * @param int $configCall number of times config->getValue is expected to be called
- */
- public function testIsSecure($isSecure, $serverHttps, $headerOffloadKey, $headerOffloadValue, $configCall)
- {
- $this->model = $this->getModel(null, false);
- $configOffloadHeader = 'Header-From-Proxy';
- $configMock = $this->getMockBuilder(\Magento\Framework\App\Config::class)
- ->disableOriginalConstructor()
- ->setMethods(['getValue'])
- ->getMock();
- $configMock->expects($this->exactly($configCall))
- ->method('getValue')
- ->with(
- \Magento\Framework\App\Request\Http::XML_PATH_OFFLOADER_HEADER,
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT
- )->willReturn($configOffloadHeader);
- $this->objectManager->setBackwardCompatibleProperty($this->model, 'appConfig', $configMock);
- $this->objectManager->setBackwardCompatibleProperty($this->model, 'sslOffloadHeader', null);
- $this->model->getServer()->set($headerOffloadKey, $headerOffloadValue);
- $this->model->getServer()->set('HTTPS', $serverHttps);
- $this->assertSame($isSecure, $this->model->isSecure());
- }
- /**
- * @dataProvider httpSafeMethodProvider
- * @backupGlobals enabled
- * @param string $httpMethod value of $_SERVER['REQUEST_METHOD']
- */
- public function testIsSafeMethodTrue($httpMethod)
- {
- $this->model = $this->getModel();
- $_SERVER['REQUEST_METHOD'] = $httpMethod;
- $this->assertEquals(true, $this->model->isSafeMethod());
- }
- /**
- * @dataProvider httpNotSafeMethodProvider
- * @backupGlobals enabled
- * @param string $httpMethod value of $_SERVER['REQUEST_METHOD']
- */
- public function testIsSafeMethodFalse($httpMethod)
- {
- $this->model = $this->getModel();
- $_SERVER['REQUEST_METHOD'] = $httpMethod;
- $this->assertEquals(false, $this->model->isSafeMethod());
- }
- /**
- * @return array
- */
- public function httpSafeMethodProvider()
- {
- return [
- 'Test 1' => ['GET'],
- 'Test 2' => ['HEAD'],
- 'Test 3' => ['TRACE'],
- 'Test 4' => ['OPTIONS']
- ];
- }
- /**
- * @return array
- */
- public function httpNotSafeMethodProvider()
- {
- return [
- 'Test 1' => ['POST'],
- 'Test 2' => ['PUT'],
- 'Test 3' => ['DELETE'],
- 'Test 4' => ['PATCH'],
- 'Test 5' => ['CONNECT'],
- 'Test 6' => [null]
- ];
- }
- /**
- * @return array
- */
- public function isSecureDataProvider()
- {
- /**
- * Data structure:
- * 'Test #' => [
- * expected output of isSecure method
- * value of $_SERVER['HTTPS'],
- * <Name-Of-Offload-Header>,
- * value of $_SERVER[<Name-Of-Offload-Header>]
- * number of times config->getValue is expected to be called
- * ]
- */
- return [
- 'Test 1' => [true, 'on', 'HEADER_FROM_PROXY', 'https', 0],
- 'Test 2' => [true, 'off', 'HEADER_FROM_PROXY', 'https', 1],
- 'Test 3' => [true, 'any-string', 'HEADER_FROM_PROXY', 'https', 0],
- 'Test 4' => [true, 'on', 'HEADER_FROM_PROXY', 'http', 0],
- 'Test 5' => [false, 'off', 'HEADER_FROM_PROXY', 'http', 1],
- 'Test 6' => [true, 'any-string', 'HEADER_FROM_PROXY', 'http', 0],
- 'Test 7' => [true, 'on', 'HEADER_FROM_PROXY', 'any-string', 0],
- 'Test 8' => [false, 'off', 'HEADER_FROM_PROXY', 'any-string', 1],
- 'Test 9' => [true, 'any-string', 'HEADER_FROM_PROXY', 'any-string', 0],
- 'blank HTTPS with proxy set https' => [true, '', 'HEADER_FROM_PROXY', 'https', 1],
- 'blank HTTPS with proxy set http' => [false, '', 'HEADER_FROM_PROXY', 'http', 1],
- 'HTTPS off with HTTP_ prefixed proxy set to https' => [true, 'off', 'HTTP_HEADER_FROM_PROXY', 'https', 1],
- ];
- }
-
- /**
- * @dataProvider setPathInfoDataProvider
- * @param string $requestUri
- * @param string $basePath$
- * @param string $expected
- */
- public function testGetPathInfo($requestUri, $basePath, $expected)
- {
- $this->model = $this->getModel($requestUri);
- $this->model->setBaseUrl($basePath);
- $this->assertEquals($expected, $this->model->getPathInfo());
- $this->assertEquals($expected, $this->model->getOriginalPathInfo());
- }
- public function testSetPathInfo()
- {
- $requestUri = 'http://svr.com//module/route/mypage/myproduct?param1=1';
- $basePath = '/module/route/';
- $this->model = $this->getModel($requestUri);
- $this->model->setBaseUrl($basePath);
- $expected = '/mypage/myproduct';
- $this->assertEquals($expected, $this->model->getOriginalPathInfo());
- $this->model->setPathInfo('http://svr.com/something/route?param1=1');
- $this->assertEquals('http://svr.com/something/route?param1=1', $this->model->getPathInfo());
- $this->assertEquals($expected, $this->model->getOriginalPathInfo());
- }
- /**
- * @return array
- */
- public function setPathInfoDataProvider()
- {
- return [
- ['http://svr.com/', '', ''],
- ['http://svr.com', '', ''],
- ['http://svr.com?param1=1', '', ''],
- ['http://svr.com/?param1=1', '', '/'],
- ['http://svr.com?param1=1¶m2=2', '', ''],
- ['http://svr.com/?param1=1¶m2=2', '', '/'],
- ['http://svr.com/module', '', '/module'],
- ['http://svr.com/module/', '', '/module/'],
- ['http://svr.com/module/route', '', '/module/route'],
- ['http://svr.com/module/route/', '', '/module/route/'],
- ['http://svr.com/index.php', '/index.php', ''],
- ['http://svr.com/index.php/', '/index.php', '/'],
- ['http://svr.com/index.phpmodule', '/index.php', 'noroute'],
- ['http://svr.com/index.phpmodule/contact', '/index.php/', 'noroute'],
- ['http://svr.com//index.phpmodule/contact', 'index.php', 'noroute'],
- ['http://svr.com/index.phpmodule/contact/', '/index.php/', 'noroute'],
- ['http://svr.com//index.phpmodule/contact/', 'index.php', 'noroute'],
- ];
- }
- }
|