Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpTest.php
Go to the documentation of this file.
1 <?php
9 
12 
17 class HttpTest extends \PHPUnit\Framework\TestCase
18 {
22  private $model;
23 
27  private $routerListMock;
28 
32  private $infoProcessorMock;
33 
37  private $pathInfo;
38 
42  private $objectManagerMock;
43 
47  private $converterMock;
48 
52  private $objectManager;
53 
57  private $serverArray;
58 
59  protected function setUp()
60  {
61  $this->routerListMock = $this->createPartialMock(
62  \Magento\Framework\App\Route\ConfigInterface\Proxy::class,
63  ['getRouteFrontName', 'getRouteByFrontName', '__wakeup']
64  );
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'])
71  ->getMock();
72  $this->converterMock->expects($this->any())->method('cleanString')->will($this->returnArgument(0));
73 
74  // Stash the $_SERVER array to protect it from modification in test
75  $this->serverArray = $_SERVER;
76 
77  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
78  $this->pathInfo = $this->objectManager->getObject(\Magento\Framework\App\Request\PathInfo::class);
79  }
80 
81  public function tearDown()
82  {
83  $_SERVER = $this->serverArray;
84  }
85 
89  private function getModel($uri = null, $appConfigMock = true)
90  {
91  $model = $this->objectManager->getObject(
92  \Magento\Framework\App\Request\Http::class,
93  [
94  'routeConfig' => $this->routerListMock,
95  'pathInfoProcessor' => $this->infoProcessorMock,
96  'pathInfoService' => $this->pathInfo,
97  'objectManager' => $this->objectManagerMock,
98  'converter' => $this->converterMock,
99  'uri' => $uri,
100  ]
101  );
102 
103  if ($appConfigMock) {
104  $configMock = $this->createMock(\Magento\Framework\App\Config::class);
105  $this->objectManager->setBackwardCompatibleProperty($model, 'appConfig', $configMock);
106  }
107 
108  return $model;
109  }
110 
112  {
113  $uri = 'http://test.com/value?key=value';
114  $this->model = $this->getModel($uri);
115  $this->assertEquals('/value', $this->model->getOriginalPathInfo());
116  }
117 
119  {
120  $this->model = $this->getModel();
121  $this->assertEmpty($this->model->getOriginalPathInfo());
122  }
123 
124  public function testGetBasePathWithPath()
125  {
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());
129  }
130 
131  public function testGetBasePathWithoutPath()
132  {
133  $this->model = $this->getModel();
134  $this->model->setBasePath(null);
135  $this->assertEquals('/', $this->model->getBasePath());
136  }
137 
138  public function testSetRouteNameWithRouter()
139  {
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());
145  }
146 
148  {
149  $this->model = $this->getModel();
150  $this->routerListMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue(null));
151  $this->model->setRouteName('RouterName');
152  }
153 
154  public function testGetFrontName()
155  {
156  $uri = 'http://test.com/one/two';
157  $this->model = $this->getModel($uri);
158  $this->assertEquals('one', $this->model->getFrontName());
159  }
160 
162  {
163  $this->model = $this->getModel();
164  $this->model->setRouteName('RouteName');
165  $this->assertEquals('RouteName', $this->model->getRouteName());
166  }
167 
168  public function testGetRouteName()
169  {
170  $this->model = $this->getModel();
171  $expected = 'RouteName';
172  $this->model->setRouteName($expected);
173  $this->assertEquals($expected, $this->model->getRouteName());
174  }
175 
176  public function testGetFullActionName()
177  {
178  $this->model = $this->getModel();
179  /* empty request */
180  $this->assertEquals('__', $this->model->getFullActionName());
181  $this->model->setRouteName('test')->setControllerName('controller')->setActionName('action');
182  $this->assertEquals('test/controller/action', $this->model->getFullActionName('/'));
183  }
184 
185  public function testInitForward()
186  {
187  $expected = $this->initForward();
188  $this->assertEquals($expected, $this->model->getBeforeForwardInfo());
189  }
190 
191  public function testGetBeforeForwardInfo()
192  {
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));
197  }
198  $this->assertEquals($beforeForwardInfo, $this->model->getBeforeForwardInfo());
199  }
200 
206  private function initForward()
207  {
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'
215  ];
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;
223  }
224 
225  public function testIsAjax()
226  {
227  $this->model = $this->getModel();
228 
229  $this->assertFalse($this->model->isAjax());
230 
231  $this->model->clearParams();
232  $this->model->setParam('ajax', 1);
233  $this->assertTrue($this->model->isAjax());
234 
235  $this->model->clearParams();
236  $this->model->setParam('isAjax', 1);
237  $this->assertTrue($this->model->isAjax());
238 
239  $this->model->clearParams();
240  $this->model->getHeaders()->addHeaderLine('X-Requested-With', 'XMLHttpRequest');
241  $this->assertTrue($this->model->isAjax());
242 
243  $this->model->getHeaders()->clearHeaders();
244  $this->model->getHeaders()->addHeaderLine('X-Requested-With', 'NotXMLHttpRequest');
245  $this->assertFalse($this->model->isAjax());
246  }
247 
253  public function testGetDistroBaseUrl($serverVariables, $expectedResult)
254  {
255  $originalServerValue = $_SERVER;
256  $_SERVER = $serverVariables;
257  $this->model = $this->getModel();
258  $this->assertEquals($expectedResult, $this->model->getDistroBaseUrl());
259 
260  $_SERVER = $originalServerValue;
261  }
262 
268  public function testGetDistroBaseUrlPath($scriptName, $expected)
269  {
270  $this->assertEquals($expected, Http::getDistroBaseUrlPath(['SCRIPT_NAME' => $scriptName]));
271  }
272 
277  {
278  return [
279  [null, '/'],
280  ['./index.php', '/'],
281  ['.\\index.php', '/'],
282  ['/index.php', '/'],
283  ['\\index.php', '/'],
284  ['subdir/script.php', 'subdir/'],
285  ['subdir\\script.php', 'subdir/'],
286  ['sub\\dir\\script.php', 'sub/dir/'],
287  ];
288  }
289 
293  public function serverVariablesProvider()
294  {
295  $returnValue = [];
296  $defaultServerData = [
297  'SCRIPT_NAME' => 'index.php',
298  'HTTP_HOST' => 'sample.host.com',
299  'SERVER_PORT' => '80',
300  'HTTPS' => '1'
301  ];
302 
303  $secureUnusualPort = $noHttpsData = $httpsOffData = $noHostData = $noScriptNameData = $defaultServerData;
304 
305  unset($noScriptNameData['SCRIPT_NAME']);
306  $returnValue['no SCRIPT_NAME'] = [$noScriptNameData, 'http://localhost/'];
307 
308  unset($noHostData['HTTP_HOST']);
309  $returnValue['no HTTP_HOST'] = [$noHostData, 'http://localhost/'];
310 
311  $httpsOffData['HTTPS'] = 'off';
312  $returnValue['HTTPS off'] = [$httpsOffData, 'http://sample.host.com/'];
313 
314  unset($noHttpsData['HTTPS']);
315  $returnValue['no HTTPS'] = [$noHttpsData, 'http://sample.host.com/'];
316 
317  $noHttpsNoServerPort = $noHttpsData;
318  unset($noHttpsNoServerPort['SERVER_PORT']);
319  $returnValue['no SERVER_PORT'] = [$noHttpsNoServerPort, 'http://sample.host.com/'];
320 
321  $noHttpsButSecurePort = $noHttpsData;
322  $noHttpsButSecurePort['SERVER_PORT'] = 443;
323  $returnValue['no HTTP but secure port'] = [$noHttpsButSecurePort, 'https://sample.host.com/'];
324 
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/'];
329 
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/'];
333 
334  $customUrlPathData = $noHttpsData;
335  $customUrlPathData['SCRIPT_FILENAME'] = '/some/dir/custom.php';
336  $returnValue['custom path'] = [$customUrlPathData, 'http://sample.host.com/'];
337 
338  return $returnValue;
339  }
340 
350  public function testIsSecure($isSecure, $serverHttps, $headerOffloadKey, $headerOffloadValue, $configCall)
351  {
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'])
357  ->getMock();
358  $configMock->expects($this->exactly($configCall))
359  ->method('getValue')
360  ->with(
361  \Magento\Framework\App\Request\Http::XML_PATH_OFFLOADER_HEADER,
363  )->willReturn($configOffloadHeader);
364 
365  $this->objectManager->setBackwardCompatibleProperty($this->model, 'appConfig', $configMock);
366  $this->objectManager->setBackwardCompatibleProperty($this->model, 'sslOffloadHeader', null);
367 
368  $this->model->getServer()->set($headerOffloadKey, $headerOffloadValue);
369  $this->model->getServer()->set('HTTPS', $serverHttps);
370 
371  $this->assertSame($isSecure, $this->model->isSecure());
372  }
373 
379  public function testIsSafeMethodTrue($httpMethod)
380  {
381  $this->model = $this->getModel();
382  $_SERVER['REQUEST_METHOD'] = $httpMethod;
383  $this->assertEquals(true, $this->model->isSafeMethod());
384  }
385 
391  public function testIsSafeMethodFalse($httpMethod)
392  {
393  $this->model = $this->getModel();
394  $_SERVER['REQUEST_METHOD'] = $httpMethod;
395  $this->assertEquals(false, $this->model->isSafeMethod());
396  }
397 
401  public function httpSafeMethodProvider()
402  {
403  return [
404  'Test 1' => ['GET'],
405  'Test 2' => ['HEAD'],
406  'Test 3' => ['TRACE'],
407  'Test 4' => ['OPTIONS']
408  ];
409  }
410 
414  public function httpNotSafeMethodProvider()
415  {
416  return [
417  'Test 1' => ['POST'],
418  'Test 2' => ['PUT'],
419  'Test 3' => ['DELETE'],
420  'Test 4' => ['PATCH'],
421  'Test 5' => ['CONNECT'],
422  'Test 6' => [null]
423  ];
424  }
425 
429  public function isSecureDataProvider()
430  {
441  return [
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],
454  ];
455  }
456 
463  public function testGetPathInfo($requestUri, $basePath, $expected)
464  {
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());
469  }
470 
471  public function testSetPathInfo()
472  {
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());
482  }
483 
487  public function setPathInfoDataProvider()
488  {
489  return [
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&param2=2', '', ''],
495  ['http://svr.com/?param1=1&param2=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'],
507  ];
508  }
509 }
static getDistroBaseUrlPath($server)
Definition: Http.php:362
testGetPathInfo($requestUri, $basePath, $expected)
Definition: HttpTest.php:463
testGetDistroBaseUrlPath($scriptName, $expected)
Definition: HttpTest.php:268
testIsSecure($isSecure, $serverHttps, $headerOffloadKey, $headerOffloadValue, $configCall)
Definition: HttpTest.php:350
testGetDistroBaseUrl($serverVariables, $expectedResult)
Definition: HttpTest.php:253