-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
5e7cb32
commit bd34e15
Showing
3 changed files
with
163 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |