HttpTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Response;
  7. use Magento\Framework\App\Response\Http;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\Session\Config\ConfigInterface;
  10. use Magento\Framework\Stdlib\Cookie\CookieMetadata;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class HttpTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Http
  18. */
  19. protected $model;
  20. /**
  21. * @var \Magento\Framework\Stdlib\CookieManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $cookieManagerMock;
  24. /**
  25. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $cookieMetadataFactoryMock;
  28. /**
  29. * @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $contextMock;
  32. /**
  33. * @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $dateTimeMock;
  36. /**
  37. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $requestMock;
  40. /**
  41. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  42. */
  43. protected $objectManager;
  44. /**
  45. * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $sessionConfigMock;
  48. /**
  49. * @var int
  50. */
  51. private $cookieLifeTime = 3600;
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function setUp()
  56. {
  57. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->cookieMetadataFactoryMock = $this->getMockBuilder(
  62. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  63. )->disableOriginalConstructor()->getMock();
  64. $this->cookieManagerMock = $this->createMock(\Magento\Framework\Stdlib\CookieManagerInterface::class);
  65. $this->contextMock = $this->getMockBuilder(
  66. \Magento\Framework\App\Http\Context::class
  67. )->disableOriginalConstructor()
  68. ->getMock();
  69. $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->sessionConfigMock = $this->getMockBuilder(ConfigInterface::class)
  73. ->disableOriginalConstructor()
  74. ->getMockForAbstractClass();
  75. $this->model = $this->objectManager->getObject(
  76. \Magento\Framework\App\Response\Http::class,
  77. [
  78. 'request' => $this->requestMock,
  79. 'cookieManager' => $this->cookieManagerMock,
  80. 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
  81. 'context' => $this->contextMock,
  82. 'dateTime' => $this->dateTimeMock,
  83. 'sessionConfig' => $this->sessionConfigMock,
  84. ]
  85. );
  86. $this->model->headersSentThrowsException = false;
  87. $this->model->setHeader('Name', 'Value');
  88. }
  89. /**
  90. * @inheritdoc
  91. */
  92. protected function tearDown()
  93. {
  94. unset($this->model);
  95. /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManagerMock*/
  96. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  97. \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
  98. }
  99. public function testSendVary()
  100. {
  101. $expectedCookieName = Http::COOKIE_VARY_STRING;
  102. $expectedCookieValue = 'SHA1 Serialized String';
  103. $sensitiveCookieMetadataMock = $this->getMockBuilder(
  104. \Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata::class
  105. )
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $sensitiveCookieMetadataMock->expects($this->once())
  109. ->method('setPath')
  110. ->with('/')
  111. ->will($this->returnSelf());
  112. $this->contextMock->expects($this->once())
  113. ->method('getVaryString')
  114. ->will($this->returnValue($expectedCookieValue));
  115. $this->sessionConfigMock->expects($this->once())
  116. ->method('getCookieLifetime')
  117. ->willReturn($this->cookieLifeTime);
  118. $this->cookieMetadataFactoryMock->expects($this->once())
  119. ->method('createSensitiveCookieMetadata')
  120. ->with([CookieMetadata::KEY_DURATION => $this->cookieLifeTime])
  121. ->willReturn($sensitiveCookieMetadataMock);
  122. $this->cookieManagerMock->expects($this->once())
  123. ->method('setSensitiveCookie')
  124. ->with($expectedCookieName, $expectedCookieValue, $sensitiveCookieMetadataMock);
  125. $this->model->sendVary();
  126. }
  127. public function testSendVaryEmptyDataDeleteCookie()
  128. {
  129. $expectedCookieName = Http::COOKIE_VARY_STRING;
  130. $cookieMetadataMock = $this->createMock(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class);
  131. $cookieMetadataMock->expects($this->once())
  132. ->method('setPath')
  133. ->with('/')
  134. ->will($this->returnSelf());
  135. $this->contextMock->expects($this->once())
  136. ->method('getVaryString')
  137. ->willReturn(null);
  138. $this->cookieMetadataFactoryMock->expects($this->once())
  139. ->method('createSensitiveCookieMetadata')
  140. ->willReturn($cookieMetadataMock);
  141. $this->cookieManagerMock->expects($this->once())
  142. ->method('deleteCookie')
  143. ->with($expectedCookieName, $cookieMetadataMock);
  144. $this->requestMock->expects($this->once())
  145. ->method('get')
  146. ->willReturn('value');
  147. $this->model->sendVary();
  148. }
  149. public function testSendVaryEmptyData()
  150. {
  151. $this->contextMock->expects($this->once())
  152. ->method('getVaryString')
  153. ->willReturn(null);
  154. $this->cookieMetadataFactoryMock->expects($this->never())
  155. ->method('createSensitiveCookieMetadata');
  156. $this->requestMock->expects($this->once())
  157. ->method('get')
  158. ->willReturn(null);
  159. $this->model->sendVary();
  160. }
  161. /**
  162. * Test setting public cache headers
  163. */
  164. public function testSetPublicHeaders()
  165. {
  166. $ttl = 120;
  167. $timestamp = 1000000;
  168. $pragma = 'cache';
  169. $cacheControl = 'max-age=' . $ttl . ', public, s-maxage=' . $ttl;
  170. $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
  171. $this->dateTimeMock->expects($this->once())
  172. ->method('strToTime')
  173. ->with('+' . $ttl . ' seconds')
  174. ->willReturn($timestamp);
  175. $this->dateTimeMock->expects($this->once())
  176. ->method('gmDate')
  177. ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
  178. ->willReturn($expiresResult);
  179. $this->model->setPublicHeaders($ttl);
  180. $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
  181. $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
  182. $this->assertSame($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
  183. }
  184. /**
  185. * Test for setting public headers without time to live parameter
  186. */
  187. public function testSetPublicHeadersWithoutTtl()
  188. {
  189. $this->expectException('InvalidArgumentException');
  190. $this->expectExceptionMessage('Time to live is a mandatory parameter for set public headers');
  191. $this->model->setPublicHeaders(null);
  192. }
  193. /**
  194. * Test setting public cache headers
  195. */
  196. public function testSetPrivateHeaders()
  197. {
  198. $ttl = 120;
  199. $timestamp = 1000000;
  200. $pragma = 'cache';
  201. $cacheControl = 'max-age=' . $ttl . ', private';
  202. $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
  203. $this->dateTimeMock->expects($this->once())
  204. ->method('strToTime')
  205. ->with('+' . $ttl . ' seconds')
  206. ->willReturn($timestamp);
  207. $this->dateTimeMock->expects($this->once())
  208. ->method('gmDate')
  209. ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
  210. ->willReturn($expiresResult);
  211. $this->model->setPrivateHeaders($ttl);
  212. $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
  213. $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
  214. $this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
  215. }
  216. /**
  217. * Test for setting public headers without time to live parameter
  218. */
  219. public function testSetPrivateHeadersWithoutTtl()
  220. {
  221. $this->expectException('InvalidArgumentException');
  222. $this->expectExceptionMessage('Time to live is a mandatory parameter for set private headers');
  223. $this->model->setPrivateHeaders(null);
  224. }
  225. /**
  226. * Test setting public cache headers
  227. */
  228. public function testSetNoCacheHeaders()
  229. {
  230. $timestamp = 1000000;
  231. $pragma = 'no-cache';
  232. $cacheControl = 'max-age=0, must-revalidate, no-cache, no-store';
  233. $expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
  234. $this->dateTimeMock->expects($this->once())
  235. ->method('strToTime')
  236. ->with('-1 year')
  237. ->willReturn($timestamp);
  238. $this->dateTimeMock->expects($this->once())
  239. ->method('gmDate')
  240. ->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
  241. ->willReturn($expiresResult);
  242. $this->model->setNoCacheHeaders();
  243. $this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
  244. $this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
  245. $this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
  246. }
  247. /**
  248. * Test setting body in JSON format
  249. */
  250. public function testRepresentJson()
  251. {
  252. $this->model->setHeader('Content-Type', 'text/javascript');
  253. $this->model->representJson('json_string');
  254. $this->assertEquals('application/json', $this->model->getHeader('Content-Type')->getFieldValue());
  255. $this->assertEquals('json_string', $this->model->getBody('default'));
  256. }
  257. /**
  258. *
  259. * @expectedException \RuntimeException
  260. * @expectedExceptionMessage ObjectManager isn't initialized
  261. */
  262. public function testWakeUpWithException()
  263. {
  264. /* ensure that the test preconditions are met */
  265. $objectManagerClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class);
  266. $instanceProperty = $objectManagerClass->getProperty('_instance');
  267. $instanceProperty->setAccessible(true);
  268. $instanceProperty->setValue(null);
  269. $this->model->__wakeup();
  270. $this->assertNull($this->cookieMetadataFactoryMock);
  271. $this->assertNull($this->cookieManagerMock);
  272. }
  273. /**
  274. * Test for the magic method __wakeup
  275. *
  276. * @covers \Magento\Framework\App\Response\Http::__wakeup
  277. */
  278. public function testWakeUpWith()
  279. {
  280. $objectManagerMock = $this->createMock(\Magento\Framework\App\ObjectManager::class);
  281. $objectManagerMock->expects($this->once())
  282. ->method('create')
  283. ->with(\Magento\Framework\Stdlib\CookieManagerInterface::class)
  284. ->will($this->returnValue($this->cookieManagerMock));
  285. $objectManagerMock->expects($this->at(1))
  286. ->method('get')
  287. ->with(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
  288. ->will($this->returnValue($this->cookieMetadataFactoryMock));
  289. \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
  290. $this->model->__wakeup();
  291. }
  292. public function testSetXFrameOptions()
  293. {
  294. $value = 'DENY';
  295. $this->model->setXFrameOptions($value);
  296. $this->assertSame($value, $this->model->getHeader(Http::HEADER_X_FRAME_OPT)->getFieldValue());
  297. }
  298. }