17 class HttpTest extends \PHPUnit\Framework\TestCase
57 private $sessionConfigMock;
62 private $cookieLifeTime = 3600;
69 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70 $this->requestMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
71 ->disableOriginalConstructor()
73 $this->cookieMetadataFactoryMock = $this->getMockBuilder(
74 \
Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
75 )->disableOriginalConstructor()->getMock();
76 $this->cookieManagerMock = $this->createMock(\
Magento\Framework\Stdlib\CookieManagerInterface::class);
77 $this->contextMock = $this->getMockBuilder(
79 )->disableOriginalConstructor()
82 $this->dateTimeMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\DateTime::class)
83 ->disableOriginalConstructor()
86 $this->sessionConfigMock = $this->getMockBuilder(ConfigInterface::class)
87 ->disableOriginalConstructor()
88 ->getMockForAbstractClass();
90 $this->model = $this->objectManager->getObject(
93 'request' => $this->requestMock,
94 'cookieManager' => $this->cookieManagerMock,
95 'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
96 'context' => $this->contextMock,
97 'dateTime' => $this->dateTimeMock,
98 'sessionConfig' => $this->sessionConfigMock,
101 $this->model->headersSentThrowsException =
false;
102 $this->model->setHeader(
'Name',
'Value');
108 protected function tearDown()
112 $objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
119 $expectedCookieValue =
'SHA1 Serialized String';
120 $sensitiveCookieMetadataMock = $this->getMockBuilder(
121 \
Magento\Framework\Stdlib\Cookie\SensitiveCookieMetadata::class
123 ->disableOriginalConstructor()
125 $sensitiveCookieMetadataMock->expects($this->once())
128 ->will($this->returnSelf());
130 $this->contextMock->expects($this->once())
131 ->method(
'getVaryString')
132 ->will($this->returnValue($expectedCookieValue));
134 $this->sessionConfigMock->expects($this->once())
135 ->method(
'getCookieLifetime')
136 ->willReturn($this->cookieLifeTime);
138 $this->cookieMetadataFactoryMock->expects($this->once())
139 ->method(
'createSensitiveCookieMetadata')
141 ->willReturn($sensitiveCookieMetadataMock);
143 $this->cookieManagerMock->expects($this->once())
144 ->method(
'setSensitiveCookie')
145 ->with($expectedCookieName, $expectedCookieValue, $sensitiveCookieMetadataMock);
146 $this->model->sendVary();
152 $cookieMetadataMock = $this->createMock(\
Magento\Framework\Stdlib\Cookie\CookieMetadata::class);
153 $cookieMetadataMock->expects($this->once())
156 ->will($this->returnSelf());
157 $this->contextMock->expects($this->once())
158 ->method(
'getVaryString')
160 $this->cookieMetadataFactoryMock->expects($this->once())
161 ->method(
'createSensitiveCookieMetadata')
162 ->willReturn($cookieMetadataMock);
163 $this->cookieManagerMock->expects($this->once())
164 ->method(
'deleteCookie')
165 ->with($expectedCookieName, $cookieMetadataMock);
166 $this->requestMock->expects($this->once())
168 ->willReturn(
'value');
169 $this->model->sendVary();
174 $this->contextMock->expects($this->once())
175 ->method(
'getVaryString')
177 $this->cookieMetadataFactoryMock->expects($this->never())
178 ->method(
'createSensitiveCookieMetadata');
179 $this->requestMock->expects($this->once())
182 $this->model->sendVary();
191 $timestamp = 1000000;
193 $cacheControl =
'max-age=' . $ttl .
', public, s-maxage=' . $ttl;
194 $expiresResult =
'Thu, 01 Jan 1970 00:00:00 GMT';
196 $this->dateTimeMock->expects($this->once())
197 ->method(
'strToTime')
198 ->with(
'+' . $ttl .
' seconds')
199 ->willReturn($timestamp);
200 $this->dateTimeMock->expects($this->once())
203 ->willReturn($expiresResult);
205 $this->model->setPublicHeaders($ttl);
206 $this->assertEquals($pragma, $this->model->getHeader(
'Pragma')->getFieldValue());
207 $this->assertEquals($cacheControl, $this->model->getHeader(
'Cache-Control')->getFieldValue());
208 $this->assertSame($expiresResult, $this->model->getHeader(
'Expires')->getFieldValue());
216 $this->expectException(
'InvalidArgumentException');
217 $this->expectExceptionMessage(
'Time to live is a mandatory parameter for set public headers');
218 $this->model->setPublicHeaders(
null);
227 $timestamp = 1000000;
229 $cacheControl =
'max-age=' . $ttl .
', private';
230 $expiresResult =
'Thu, 01 Jan 1970 00:00:00 GMT';
232 $this->dateTimeMock->expects($this->once())
233 ->method(
'strToTime')
234 ->with(
'+' . $ttl .
' seconds')
235 ->willReturn($timestamp);
236 $this->dateTimeMock->expects($this->once())
239 ->willReturn($expiresResult);
241 $this->model->setPrivateHeaders($ttl);
242 $this->assertEquals($pragma, $this->model->getHeader(
'Pragma')->getFieldValue());
243 $this->assertEquals($cacheControl, $this->model->getHeader(
'Cache-Control')->getFieldValue());
244 $this->assertEquals($expiresResult, $this->model->getHeader(
'Expires')->getFieldValue());
252 $this->expectException(
'InvalidArgumentException');
253 $this->expectExceptionMessage(
'Time to live is a mandatory parameter for set private headers');
254 $this->model->setPrivateHeaders(
null);
262 $timestamp = 1000000;
263 $pragma =
'no-cache';
264 $cacheControl =
'max-age=0, must-revalidate, no-cache, no-store';
265 $expiresResult =
'Thu, 01 Jan 1970 00:00:00 GMT';
267 $this->dateTimeMock->expects($this->once())
268 ->method(
'strToTime')
270 ->willReturn($timestamp);
271 $this->dateTimeMock->expects($this->once())
274 ->willReturn($expiresResult);
276 $this->model->setNoCacheHeaders();
277 $this->assertEquals($pragma, $this->model->getHeader(
'Pragma')->getFieldValue());
278 $this->assertEquals($cacheControl, $this->model->getHeader(
'Cache-Control')->getFieldValue());
279 $this->assertEquals($expiresResult, $this->model->getHeader(
'Expires')->getFieldValue());
287 $this->model->setHeader(
'Content-Type',
'text/javascript');
288 $this->model->representJson(
'json_string');
289 $this->assertEquals(
'application/json', $this->model->getHeader(
'Content-Type')->getFieldValue());
290 $this->assertEquals(
'json_string', $this->model->getBody(
'default'));
301 $objectManagerClass = new \ReflectionClass(\
Magento\Framework\
App\ObjectManager::class);
302 $instanceProperty = $objectManagerClass->getProperty(
'_instance');
303 $instanceProperty->setAccessible(
true);
304 $instanceProperty->setValue(
null);
306 $this->model->__wakeup();
307 $this->assertNull($this->cookieMetadataFactoryMock);
308 $this->assertNull($this->cookieManagerMock);
318 $objectManagerMock = $this->createMock(\
Magento\Framework\
App\ObjectManager::class);
319 $objectManagerMock->expects($this->once())
321 ->with(\
Magento\Framework\Stdlib\CookieManagerInterface::class)
322 ->will($this->returnValue($this->cookieManagerMock));
323 $objectManagerMock->expects($this->at(1))
325 ->with(\
Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
326 ->will($this->returnValue($this->cookieMetadataFactoryMock));
329 $this->model->__wakeup();
335 $this->model->setXFrameOptions(
$value);
testWakeUpWithException()
static setInstance(\Magento\Framework\ObjectManagerInterface $objectManager)
const EXPIRATION_TIMESTAMP_FORMAT
testSendVaryEmptyDataDeleteCookie()
testSetPrivateHeadersWithoutTtl()
$cookieMetadataFactoryMock
testSetPublicHeadersWithoutTtl()