Skip to content

Commit

Permalink
Issue #97 - Always exclude StreamedResponse (#101)
Browse files Browse the repository at this point in the history
Always exclude StreamedResponse - Closes #97  

- Always exclude StreamedResponse similar to BinaryFileResponse as it cannot be handled.

- Refactored CollapseWhitespaceTest by removing methods which belonged to ShouldNotProcessResponseTest

- Improved ShouldNotProcessResponseTest
  • Loading branch information
percymamedy authored and joaorobertopb committed Feb 12, 2019
1 parent 5e7cb32 commit bd34e15
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 41 deletions.
5 changes: 5 additions & 0 deletions src/Middleware/PageSpeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RenatoMarinho\LaravelPageSpeed\Middleware;

use Closure;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

abstract class PageSpeed
Expand Down Expand Up @@ -79,6 +80,10 @@ protected function shouldProcessPageSpeed($request, $response)
return false;
}

if ($response instanceof StreamedResponse) {
return false;
}

foreach ($patterns as $pattern) {
if ($request->is($pattern)) {
return false;
Expand Down
42 changes: 1 addition & 41 deletions tests/Middleware/CollapseWhitespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@

namespace RenatoMarinho\LaravelPageSpeed\Test\Middleware;

use Illuminate\Http\Request;
use Mockery as m;
use RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace;
use RenatoMarinho\LaravelPageSpeed\Test\TestCase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace;

class CollapseWhitespaceTest extends TestCase
{
public function tearDown()
{
parent::tearDown();
m::close();
}

protected function getMiddleware()
{
$this->middleware = new CollapseWhitespace();
Expand All @@ -31,34 +21,4 @@ public function testCollapseWhitespace()

$this->assertSame($compress, trim($partial[0]));
}

public function testSkipBinaryFileResponse()
{
$request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]);

$response = $this->middleware->handle($request, function ($request) {
return response()->download($request->file);
});

$this->assertInstanceOf(BinaryFileResponse::class, $response);
}

public function testExpectLogicExceptionInBinaryFileResponse()
{
$this->expectException('LogicException');

$mock = m::mock(CollapseWhitespace::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

$mock->shouldReceive('shouldProcessPageSpeed')
->once()
->andReturn(true);

$request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]);

$response = $mock->handle($request, function ($request) {
return response()->download($request->file);
});
}
}
157 changes: 157 additions & 0 deletions tests/Middleware/ShouldNotProcessResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace RenatoMarinho\LaravelPageSpeed\Test\Middleware;

use Mockery as m;
use Illuminate\Http\Request;
use RenatoMarinho\LaravelPageSpeed\Test\TestCase;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed;
use RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace;

class ShouldNotProcessResponseTest extends TestCase
{
/**
* PageSpeed middleware instance.
*
* @var \RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed
*/
protected $middleware;

/**
* Clean up the testing environment before the next test.
*
* @return void
*/
protected function tearDown()
{
parent::tearDown();
m::close();
}

/**
* Test that a BinaryFileResponse is ignored by any middleware.
*
* @return void
*/
public function testSkipBinaryFileResponse()
{
$request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]);

$response = $this->middleware->handle($request, $this->getNextBinaryFileResponse());

$this->assertInstanceOf(BinaryFileResponse::class, $response);
}

/**
* Test that a StreamedResponse is ignored by any middleware.
*
* @return void
*/
public function testSkipStreamedResponse()
{
$request = Request::create('/', 'GET');

$response = $this->middleware->handle($request, $this->getNextStreamedResponse());

$this->assertInstanceOf(StreamedResponse::class, $response);
}

/**
* Test a LogicException is throw when trying to process a
* BinaryFileResponse.
*
* @return void
*
* @expectedException \LogicException
*/
public function testExpectLogicExceptionInBinaryFileResponse()
{
$request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]);

$middleware = $this->mockMiddlewareWhichAllowsPageSpeedProcess();

$middleware->handle($request, $this->getNextBinaryFileResponse());
}

/**
* Test a LogicException is throw when trying to process a
* StreamedResponse.
*
* @return void
*
* @expectedException \LogicException
*/
public function testExpectLogicExceptionInStreamedResponse()
{
$request = Request::create('/', 'GET');

$middleware = $this->mockMiddlewareWhichAllowsPageSpeedProcess();

$middleware->handle($request, $this->getNextStreamedResponse());
}

/**
* Mock a BinaryFileResponse.
*
* @return \Closure
*/
protected function getNextBinaryFileResponse()
{
return function ($request) {
return response()->download($request->file);
};
}

/**
* Mock a StreamedResponse.
*
* @return \Closure
*/
protected function getNextStreamedResponse()
{
return function ($request) {
$response = new StreamedResponse(function () {
echo "I am Streamed";
});

$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
'attachment',
'foo.txt'
));

return $response;
};
}

/**
* Return an instance of the middleware which always
* allows processing of response.
*
* @return m\Mock|PageSpeed
*/
protected function mockMiddlewareWhichAllowsPageSpeedProcess()
{
$mock = m::mock(CollapseWhitespace::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

$mock->shouldReceive('shouldProcessPageSpeed')
->once()
->andReturn(true);

return $mock;
}

/**
* Middleware used during this test.
*
* @return void
*/
protected function getMiddleware()
{
$this->middleware = new CollapseWhitespace();
}
}

0 comments on commit bd34e15

Please sign in to comment.