Skip to content

Commit

Permalink
Fix for issue #21299. Change HEAD action mapping to GET action interf…
Browse files Browse the repository at this point in the history
…ace and add HEAD request handling
  • Loading branch information
Oleksandr Iegorov committed Apr 15, 2019
1 parent be77120 commit fecce53
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 53 deletions.
117 changes: 77 additions & 40 deletions lib/internal/Magento/Framework/App/Test/Unit/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@ private function setUpLaunch()
{
$frontName = 'frontName';
$areaCode = 'areaCode';
$this->requestMock->expects($this->once())->method('getFrontName')->will($this->returnValue($frontName));
$this->requestMock->expects($this->once())
->method('getFrontName')
->willReturn($frontName);
$this->areaListMock->expects($this->once())
->method('getCodeByFrontName')
->with($frontName)->will($this->returnValue($areaCode));
->with($frontName)
->willReturn($areaCode);
$this->configLoaderMock->expects($this->once())
->method('load')->with($areaCode)->will($this->returnValue([]));
->method('load')
->with($areaCode)
->willReturn([]);
$this->objectManagerMock->expects($this->once())->method('configure')->with([]);
$this->objectManagerMock->expects($this->once())
->method('get')
Expand All @@ -149,13 +154,15 @@ private function setUpLaunch()
$this->frontControllerMock->expects($this->once())
->method('dispatch')
->with($this->requestMock)
->will($this->returnValue($this->responseMock));
->willReturn($this->responseMock);
}

public function testLaunchSuccess()
{
$this->setUpLaunch();
$this->requestMock->expects($this->once())->method('isHead')->will($this->returnValue(false));
$this->requestMock->expects($this->once())
->method('isHead')
->willReturn(false);
$this->eventManagerMock->expects($this->once())
->method('dispatch')
->with(
Expand All @@ -172,14 +179,16 @@ public function testLaunchSuccess()
public function testLaunchException()
{
$this->setUpLaunch();
$this->frontControllerMock->expects($this->once())->method('dispatch')->with($this->requestMock)->will(
$this->returnCallback(
function () {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Message');
}
)
);
$this->frontControllerMock->expects($this->once())
->method('dispatch')
->with($this->requestMock)->will(
$this->returnCallback(
function () {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Message');
}
)
);
$this->http->launch();
}

Expand All @@ -192,20 +201,22 @@ function () {
public function testLaunchHeadRequest($body, $expectedLength)
{
$this->setUpLaunch();
$this->requestMock->expects($this->once())->method('isHead')->will($this->returnValue(true));
$this->requestMock->expects($this->once())
->method('isHead')
->willReturn(true);
$this->responseMock->expects($this->once())
->method('getHttpResponseCode')
->will($this->returnValue(200));
->willReturn(200);
$this->responseMock->expects($this->once())
->method('getContent')
->will($this->returnValue($body));
->willReturn($body);
$this->responseMock->expects($this->once())
->method('clearBody')
->will($this->returnValue($this->responseMock));
->willReturn($this->responseMock);
$this->responseMock->expects($this->once())
->method('setHeader')
->with('Content-Length', $expectedLength)
->will($this->returnValue($this->responseMock));
->willReturn($this->responseMock);
$this->eventManagerMock->expects($this->once())
->method('dispatch')
->with(
Expand All @@ -219,7 +230,7 @@ public function testLaunchHeadRequest($body, $expectedLength)
* Different test content for responseMock with their expected lengths in bytes.
* @return array
*/
public function dataProviderForTestLaunchHeadRequest()
public function dataProviderForTestLaunchHeadRequest(): array
{
return [
[
Expand All @@ -244,20 +255,29 @@ public function dataProviderForTestLaunchHeadRequest()
public function testHandleDeveloperModeNotInstalled()
{
$dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
$dir->expects($this->once())->method('getAbsolutePath')->willReturn(__DIR__);
$dir->expects($this->once())
->method('getAbsolutePath')
->willReturn(__DIR__);
$this->filesystemMock->expects($this->once())
->method('getDirectoryRead')
->with(DirectoryList::ROOT)
->willReturn($dir);
$this->responseMock->expects($this->once())->method('setRedirect')->with('/_files/');
$this->responseMock->expects($this->once())->method('sendHeaders');
$this->responseMock->expects($this->once())
->method('setRedirect')
->with('/_files/');
$this->responseMock->expects($this->once())
->method('sendHeaders');
$bootstrap = $this->getBootstrapNotInstalled();
$bootstrap->expects($this->once())->method('getParams')->willReturn([
'SCRIPT_NAME' => '/index.php',
'DOCUMENT_ROOT' => __DIR__,
'SCRIPT_FILENAME' => __DIR__ . '/index.php',
SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files',
]);
$bootstrap->expects($this->once())
->method('getParams')
->willReturn(
[
'SCRIPT_NAME' => '/index.php',
'DOCUMENT_ROOT' => __DIR__,
'SCRIPT_FILENAME' => __DIR__ . '/index.php',
SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files',
]
);
$this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test Message')));
}

Expand All @@ -266,24 +286,37 @@ public function testHandleDeveloperMode()
$this->filesystemMock->expects($this->once())
->method('getDirectoryRead')
->will($this->throwException(new \Exception('strange error')));
$this->responseMock->expects($this->once())->method('setHttpResponseCode')->with(500);
$this->responseMock->expects($this->once())->method('setHeader')->with('Content-Type', 'text/plain');
$this->responseMock->expects($this->once())
->method('setHttpResponseCode')
->with(500);
$this->responseMock->expects($this->once())
->method('setHeader')
->with('Content-Type', 'text/plain');
$constraint = new \PHPUnit\Framework\Constraint\StringStartsWith('1 exception(s):');
$this->responseMock->expects($this->once())->method('setBody')->with($constraint);
$this->responseMock->expects($this->once())->method('sendResponse');
$this->responseMock->expects($this->once())
->method('setBody')
->with($constraint);
$this->responseMock->expects($this->once())
->method('sendResponse');
$bootstrap = $this->getBootstrapNotInstalled();
$bootstrap->expects($this->once())->method('getParams')->willReturn(
['DOCUMENT_ROOT' => 'something', 'SCRIPT_FILENAME' => 'something/else']
);
$bootstrap->expects($this->once())
->method('getParams')
->willReturn(
['DOCUMENT_ROOT' => 'something', 'SCRIPT_FILENAME' => 'something/else']
);
$this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test')));
}

public function testCatchExceptionSessionException()
{
$this->responseMock->expects($this->once())->method('setRedirect');
$this->responseMock->expects($this->once())->method('sendHeaders');
$this->responseMock->expects($this->once())
->method('setRedirect');
$this->responseMock->expects($this->once())
->method('sendHeaders');
$bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(false);
$bootstrap->expects($this->once())
->method('isDeveloperMode')
->willReturn(false);
$this->assertTrue($this->http->catchException(
$bootstrap,
new \Magento\Framework\Exception\SessionException(new \Magento\Framework\Phrase('Test'))
Expand All @@ -298,8 +331,12 @@ public function testCatchExceptionSessionException()
private function getBootstrapNotInstalled()
{
$bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
$bootstrap->expects($this->once())->method('getErrorCode')->willReturn(Bootstrap::ERR_IS_INSTALLED);
$bootstrap->expects($this->once())
->method('isDeveloperMode')
->willReturn(true);
$bootstrap->expects($this->once())
->method('getErrorCode')
->willReturn(Bootstrap::ERR_IS_INSTALLED);
return $bootstrap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@

namespace Magento\Framework\File\Test\Unit\Transfer\Adapter;

use \Magento\Framework\File\Transfer\Adapter\Http;
use Magento\Framework\File\Transfer\Adapter\Http;
use Magento\Framework\File\Mime;
use Magento\Framework\HTTP\PhpEnvironment\Response;
use Magento\Framework\App\Request\Http as RequestHttp;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Tests http transfer adapter.
*/
class HttpTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
* @var RequestHttp|MockObject
*/
private $request;

/**
* @var \Magento\Framework\HTTP\PhpEnvironment\Response|\PHPUnit_Framework_MockObject_MockObject
* @var Response|MockObject
*/
private $response;

/**
* @var Http|\PHPUnit_Framework_MockObject_MockObject
* @var Http|MockObject
*/
private $object;

/**
* @var \Magento\Framework\File\Mime|\PHPUnit_Framework_MockObject_MockObject
* @var Mime|MockObject
*/
private $mime;

Expand All @@ -39,12 +43,12 @@ class HttpTest extends \PHPUnit\Framework\TestCase
protected function setUp()
{
$this->response = $this->createPartialMock(
\Magento\Framework\HTTP\PhpEnvironment\Response::class,
Response::class,
['setHeader', 'sendHeaders', 'setHeaders']
);
$this->mime = $this->createMock(\Magento\Framework\File\Mime::class);
$this->mime = $this->createMock(Mime::class);
$this->request = $this->createPartialMock(
\Magento\Framework\App\Request\Http::class,
RequestHttp::class,
['isHead']
);
$this->object = new Http($this->response, $this->mime, $this->request);
Expand Down Expand Up @@ -98,10 +102,10 @@ public function testSendWithOptions(): void
$this->mime->expects($this->once())
->method('getMimeType')
->with($file)
->will($this->returnValue($contentType));
->willReturn($contentType);
$this->request->expects($this->once())
->method('isHead')
->will($this->returnValue(false));
->willReturn(false);
$this->expectOutputString(file_get_contents($file));

$this->object->send(['filepath' => $file, 'headers' => $headers]);
Expand Down Expand Up @@ -145,10 +149,10 @@ public function testSendHeadRequest(): void
$this->mime->expects($this->once())
->method('getMimeType')
->with($file)
->will($this->returnValue($contentType));
->willReturn($contentType);
$this->request->expects($this->once())
->method('isHead')
->will($this->returnValue(true));
->willReturn(true);

$this->object->send($file);
$this->assertEquals(false, $this->hasOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function getFilePath($options): string
* @param array $options
* @param string $filepath
*/
protected function prepareResponse($options, string $filepath): void
private function prepareResponse($options, string $filepath): void
{
$mimeType = $this->mime->getMimeType($filepath);
if (is_array($options) && isset($options['headers']) && $options['headers'] instanceof \Zend\Http\Headers) {
Expand Down

0 comments on commit fecce53

Please sign in to comment.