17 class HttpTest extends \PHPUnit\Framework\TestCase
27 private $routerListMock;
32 private $infoProcessorMock;
42 private $objectManagerMock;
47 private $converterMock;
52 private $objectManager;
61 $this->routerListMock = $this->createPartialMock(
63 [
'getRouteFrontName',
'getRouteByFrontName',
'__wakeup']
65 $this->infoProcessorMock = $this->createMock(\
Magento\Framework\
App\Request\PathInfoProcessorInterface::class);
66 $this->infoProcessorMock->expects($this->any())->method(
'process')->will($this->returnArgument(1));
67 $this->objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
68 $this->converterMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\StringUtils::class)
69 ->disableOriginalConstructor()
70 ->setMethods([
'cleanString'])
72 $this->converterMock->expects($this->any())->method(
'cleanString')->will($this->returnArgument(0));
75 $this->serverArray = $_SERVER;
77 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
78 $this->pathInfo = $this->objectManager->getObject(\
Magento\Framework\
App\Request\PathInfo::class);
83 $_SERVER = $this->serverArray;
89 private function getModel($uri =
null, $appConfigMock =
true)
91 $model = $this->objectManager->getObject(
94 'routeConfig' => $this->routerListMock,
95 'pathInfoProcessor' => $this->infoProcessorMock,
96 'pathInfoService' => $this->pathInfo,
97 'objectManager' => $this->objectManagerMock,
98 'converter' => $this->converterMock,
103 if ($appConfigMock) {
104 $configMock = $this->createMock(\
Magento\Framework\
App\Config::class);
105 $this->objectManager->setBackwardCompatibleProperty($model,
'appConfig', $configMock);
113 $uri =
'http://test.com/value?key=value';
114 $this->model = $this->getModel($uri);
115 $this->assertEquals(
'/value', $this->model->getOriginalPathInfo());
120 $this->model = $this->getModel();
121 $this->assertEmpty($this->model->getOriginalPathInfo());
126 $this->model = $this->getModel();
127 $this->model->setBasePath(
'http:\/test.com\one/two');
128 $this->assertEquals(
'http://test.com/one/two', $this->model->getBasePath());
133 $this->model = $this->getModel();
134 $this->model->setBasePath(
null);
135 $this->assertEquals(
'/', $this->model->getBasePath());
140 $router = $this->createMock(\
Magento\Framework\
App\Route\ConfigInterface::class);
141 $this->routerListMock->expects($this->any())->method(
'getRouteFrontName')->will($this->returnValue($router));
142 $this->model = $this->getModel();
143 $this->model->setRouteName(
'RouterName');
144 $this->assertEquals(
'RouterName', $this->model->getRouteName());
149 $this->model = $this->getModel();
150 $this->routerListMock->expects($this->once())->method(
'getRouteFrontName')->will($this->returnValue(
null));
151 $this->model->setRouteName(
'RouterName');
156 $uri =
'http://test.com/one/two';
157 $this->model = $this->getModel($uri);
158 $this->assertEquals(
'one', $this->model->getFrontName());
163 $this->model = $this->getModel();
164 $this->model->setRouteName(
'RouteName');
165 $this->assertEquals(
'RouteName', $this->model->getRouteName());
170 $this->model = $this->getModel();
171 $expected =
'RouteName';
172 $this->model->setRouteName($expected);
173 $this->assertEquals($expected, $this->model->getRouteName());
178 $this->model = $this->getModel();
180 $this->assertEquals(
'__', $this->model->getFullActionName());
181 $this->model->setRouteName(
'test')->setControllerName(
'controller')->setActionName(
'action');
182 $this->assertEquals(
'test/controller/action', $this->model->getFullActionName(
'/'));
187 $expected = $this->initForward();
188 $this->assertEquals($expected, $this->model->getBeforeForwardInfo());
193 $beforeForwardInfo = $this->initForward();
194 $this->assertNull($this->model->getBeforeForwardInfo(
'not_existing_forward_info_key'));
195 foreach (array_keys($beforeForwardInfo) as $key) {
196 $this->assertEquals($beforeForwardInfo[$key], $this->model->getBeforeForwardInfo($key));
198 $this->assertEquals($beforeForwardInfo, $this->model->getBeforeForwardInfo());
206 private function initForward()
208 $this->model = $this->getModel();
209 $beforeForwardInfo = [
210 'params' => [
'one' =>
'111',
'two' =>
'222'],
211 'action_name' =>
'ActionName',
212 'controller_name' =>
'ControllerName',
213 'module_name' =>
'ModuleName',
214 'route_name' =>
'RouteName' 216 $this->model->setParams($beforeForwardInfo[
'params']);
217 $this->model->setActionName($beforeForwardInfo[
'action_name']);
218 $this->model->setControllerName($beforeForwardInfo[
'controller_name']);
219 $this->model->setModuleName($beforeForwardInfo[
'module_name']);
220 $this->model->setRouteName($beforeForwardInfo[
'route_name']);
221 $this->model->initForward();
222 return $beforeForwardInfo;
227 $this->model = $this->getModel();
229 $this->assertFalse($this->model->isAjax());
231 $this->model->clearParams();
232 $this->model->setParam(
'ajax', 1);
233 $this->assertTrue($this->model->isAjax());
235 $this->model->clearParams();
236 $this->model->setParam(
'isAjax', 1);
237 $this->assertTrue($this->model->isAjax());
239 $this->model->clearParams();
240 $this->model->getHeaders()->addHeaderLine(
'X-Requested-With',
'XMLHttpRequest');
241 $this->assertTrue($this->model->isAjax());
243 $this->model->getHeaders()->clearHeaders();
244 $this->model->getHeaders()->addHeaderLine(
'X-Requested-With',
'NotXMLHttpRequest');
245 $this->assertFalse($this->model->isAjax());
255 $originalServerValue = $_SERVER;
256 $_SERVER = $serverVariables;
257 $this->model = $this->getModel();
258 $this->assertEquals($expectedResult, $this->model->getDistroBaseUrl());
260 $_SERVER = $originalServerValue;
280 [
'./index.php',
'/'],
281 [
'.\\index.php',
'/'],
283 [
'\\index.php',
'/'],
284 [
'subdir/script.php',
'subdir/'],
285 [
'subdir\\script.php',
'subdir/'],
286 [
'sub\\dir\\script.php',
'sub/dir/'],
296 $defaultServerData = [
297 'SCRIPT_NAME' =>
'index.php',
298 'HTTP_HOST' =>
'sample.host.com',
299 'SERVER_PORT' =>
'80',
303 $secureUnusualPort = $noHttpsData = $httpsOffData = $noHostData = $noScriptNameData = $defaultServerData;
305 unset($noScriptNameData[
'SCRIPT_NAME']);
306 $returnValue[
'no SCRIPT_NAME'] = [$noScriptNameData,
'http://localhost/'];
308 unset($noHostData[
'HTTP_HOST']);
309 $returnValue[
'no HTTP_HOST'] = [$noHostData,
'http://localhost/'];
311 $httpsOffData[
'HTTPS'] =
'off';
312 $returnValue[
'HTTPS off'] = [$httpsOffData,
'http://sample.host.com/'];
314 unset($noHttpsData[
'HTTPS']);
315 $returnValue[
'no HTTPS'] = [$noHttpsData,
'http://sample.host.com/'];
317 $noHttpsNoServerPort = $noHttpsData;
318 unset($noHttpsNoServerPort[
'SERVER_PORT']);
319 $returnValue[
'no SERVER_PORT'] = [$noHttpsNoServerPort,
'http://sample.host.com/'];
321 $noHttpsButSecurePort = $noHttpsData;
322 $noHttpsButSecurePort[
'SERVER_PORT'] = 443;
323 $returnValue[
'no HTTP but secure port'] = [$noHttpsButSecurePort,
'https://sample.host.com/'];
325 $notSecurePort = $noHttpsData;
326 $notSecurePort[
'SERVER_PORT'] = 81;
327 $notSecurePort[
'HTTP_HOST'] =
'sample.host.com:81';
328 $returnValue[
'not secure not standard port'] = [$notSecurePort,
'http://sample.host.com:81/'];
330 $secureUnusualPort[
'SERVER_PORT'] = 441;
331 $secureUnusualPort[
'HTTP_HOST'] =
'sample.host.com:441';
332 $returnValue[
'not standard secure port'] = [$secureUnusualPort,
'https://sample.host.com:441/'];
334 $customUrlPathData = $noHttpsData;
335 $customUrlPathData[
'SCRIPT_FILENAME'] =
'/some/dir/custom.php';
336 $returnValue[
'custom path'] = [$customUrlPathData,
'http://sample.host.com/'];
350 public function testIsSecure($isSecure, $serverHttps, $headerOffloadKey, $headerOffloadValue, $configCall)
352 $this->model = $this->getModel(
null,
false);
353 $configOffloadHeader =
'Header-From-Proxy';
354 $configMock = $this->getMockBuilder(\
Magento\Framework\
App\Config::class)
355 ->disableOriginalConstructor()
356 ->setMethods([
'getValue'])
358 $configMock->expects($this->exactly($configCall))
363 )->willReturn($configOffloadHeader);
365 $this->objectManager->setBackwardCompatibleProperty($this->model,
'appConfig', $configMock);
366 $this->objectManager->setBackwardCompatibleProperty($this->model,
'sslOffloadHeader',
null);
368 $this->model->getServer()->set($headerOffloadKey, $headerOffloadValue);
369 $this->model->getServer()->set(
'HTTPS', $serverHttps);
371 $this->assertSame($isSecure, $this->model->isSecure());
381 $this->model = $this->getModel();
382 $_SERVER[
'REQUEST_METHOD'] = $httpMethod;
383 $this->assertEquals(
true, $this->model->isSafeMethod());
393 $this->model = $this->getModel();
394 $_SERVER[
'REQUEST_METHOD'] = $httpMethod;
395 $this->assertEquals(
false, $this->model->isSafeMethod());
405 'Test 2' => [
'HEAD'],
406 'Test 3' => [
'TRACE'],
407 'Test 4' => [
'OPTIONS']
417 'Test 1' => [
'POST'],
419 'Test 3' => [
'DELETE'],
420 'Test 4' => [
'PATCH'],
421 'Test 5' => [
'CONNECT'],
442 'Test 1' => [
true,
'on',
'HEADER_FROM_PROXY',
'https', 0],
443 'Test 2' => [
true,
'off',
'HEADER_FROM_PROXY',
'https', 1],
444 'Test 3' => [
true,
'any-string',
'HEADER_FROM_PROXY',
'https', 0],
445 'Test 4' => [
true,
'on',
'HEADER_FROM_PROXY',
'http', 0],
446 'Test 5' => [
false,
'off',
'HEADER_FROM_PROXY',
'http', 1],
447 'Test 6' => [
true,
'any-string',
'HEADER_FROM_PROXY',
'http', 0],
448 'Test 7' => [
true,
'on',
'HEADER_FROM_PROXY',
'any-string', 0],
449 'Test 8' => [
false,
'off',
'HEADER_FROM_PROXY',
'any-string', 1],
450 'Test 9' => [
true,
'any-string',
'HEADER_FROM_PROXY',
'any-string', 0],
451 'blank HTTPS with proxy set https' => [
true,
'',
'HEADER_FROM_PROXY',
'https', 1],
452 'blank HTTPS with proxy set http' => [
false,
'',
'HEADER_FROM_PROXY',
'http', 1],
453 'HTTPS off with HTTP_ prefixed proxy set to https' => [
true,
'off',
'HTTP_HEADER_FROM_PROXY',
'https', 1],
465 $this->model = $this->getModel($requestUri);
466 $this->model->setBaseUrl($basePath);
467 $this->assertEquals($expected, $this->model->getPathInfo());
468 $this->assertEquals($expected, $this->model->getOriginalPathInfo());
473 $requestUri =
'http://svr.com//module/route/mypage/myproduct?param1=1';
474 $basePath =
'/module/route/';
475 $this->model = $this->getModel($requestUri);
476 $this->model->setBaseUrl($basePath);
477 $expected =
'/mypage/myproduct';
478 $this->assertEquals($expected, $this->model->getOriginalPathInfo());
479 $this->model->setPathInfo(
'http://svr.com/something/route?param1=1');
480 $this->assertEquals(
'http://svr.com/something/route?param1=1', $this->model->getPathInfo());
481 $this->assertEquals($expected, $this->model->getOriginalPathInfo());
490 [
'http://svr.com/',
'',
''],
491 [
'http://svr.com',
'',
''],
492 [
'http://svr.com?param1=1',
'',
''],
493 [
'http://svr.com/?param1=1',
'',
'/'],
494 [
'http://svr.com?param1=1¶m2=2',
'',
''],
495 [
'http://svr.com/?param1=1¶m2=2',
'',
'/'],
496 [
'http://svr.com/module',
'',
'/module'],
497 [
'http://svr.com/module/',
'',
'/module/'],
498 [
'http://svr.com/module/route',
'',
'/module/route'],
499 [
'http://svr.com/module/route/',
'',
'/module/route/'],
500 [
'http://svr.com/index.php',
'/index.php',
''],
501 [
'http://svr.com/index.php/',
'/index.php',
'/'],
502 [
'http://svr.com/index.phpmodule',
'/index.php',
'noroute'],
503 [
'http://svr.com/index.phpmodule/contact',
'/index.php/',
'noroute'],
504 [
'http://svr.com//index.phpmodule/contact',
'index.php',
'noroute'],
505 [
'http://svr.com/index.phpmodule/contact/',
'/index.php/',
'noroute'],
506 [
'http://svr.com//index.phpmodule/contact/',
'index.php',
'noroute'],
static getDistroBaseUrlPath($server)
testIsSafeMethodFalse($httpMethod)
testSetRouteNameWithNullRouterValue()
testIsSafeMethodTrue($httpMethod)
testGetRouteNameWithNullValueRouteName()
testGetPathInfo($requestUri, $basePath, $expected)
const XML_PATH_OFFLOADER_HEADER
testGetBasePathWithoutPath()
testSetRouteNameWithRouter()
testGetBasePathWithPath()
testGetOriginalPathInfoWithEmptyUri()
testGetDistroBaseUrlPath($scriptName, $expected)
setPathInfoDataProvider()
serverVariablesProvider()
testGetBeforeForwardInfo()
getDistroBaseUrlPathDataProvider()
testIsSecure($isSecure, $serverHttps, $headerOffloadKey, $headerOffloadValue, $configCall)
testGetOriginalPathInfoWithTestUri()
httpNotSafeMethodProvider()
testGetDistroBaseUrl($serverVariables, $expectedResult)