Skip to content

Commit

Permalink
Merge pull request #4 from denismitr/v2.1
Browse files Browse the repository at this point in the history
adds JsonResponse support
  • Loading branch information
denismitr authored Feb 15, 2020
2 parents 21c0c76 + 73fb806 commit 2d55e9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/ETagMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Denismitr\ETag;

use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

Expand Down Expand Up @@ -48,11 +49,11 @@ public function handle($request, Closure $next)
}

/**
* @param Response $response
* @param JsonResponse|Response $response
* @param Request $request
* @return bool
*/
private function isNonEtagable(Response $response, Request $request): bool
private function isNonEtagable($response, Request $request): bool
{
if ($response->getStatusCode() !== 200 || ! $request->expectsJson()) {
return true;
Expand Down Expand Up @@ -92,10 +93,10 @@ private function isEtagMatch(string $etag, string $ifNonMatch): bool
}

/**
* @param $response
* @param JsonResponse|Response $response
* @return string
*/
private function extractEtag(Response $response): string
private function extractEtag($response): string
{
return '"' . md5($response->getContent()) . '"';
}
Expand Down
39 changes: 37 additions & 2 deletions tests/EtagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Denismitr\ETag\ETagMiddleware;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mockery as m;
Expand Down Expand Up @@ -49,6 +50,21 @@ public function it_returns_response_with_etag_header()
$this->assertInstanceOf('Mockery_0_Illuminate_Http_Response', $response);
}

/** @test */
public function it_returns_json_response_with_etag_header()
{
$response = $this->getJsonResponseMockWithEtag($this->content, $this->etag);

$request = m::mock('Illuminate\Http\Request');
$request->shouldReceive('method')->once()->andReturn('GET');
$request->shouldReceive('expectsJson')->once()->andReturn(true);
$request->shouldReceive('header')->with('If-Match')->once()->andReturn(null);
$request->shouldReceive('header')->with('If-None-Match')->once()->andReturn(null);

$response = $this->runMiddleware($request, $response);
$this->assertInstanceOf('Mockery_2_Illuminate_Http_JsonResponse', $response);
}

/** @test */
public function it_returns_304_unmodified_when_the_correct_etag_is_passed()
{
Expand Down Expand Up @@ -188,10 +204,10 @@ public function it_returns_the_response_untouched_if_response_does_not_want_json
* Run the middleware
*
* @param Request $request
* @param Response $response
* @param Response|JsonResponse $response
* @return Response
*/
protected function runMiddleware(Request $request, Response $response)
protected function runMiddleware(Request $request, $response)
{
$middleware = new ETagMiddleware;

Expand Down Expand Up @@ -230,6 +246,25 @@ protected function getResponseMockWithEtag(string $content, string $etag)
return $response;
}

protected function getJsonResponseMockWithEtag(string $content, string $etag)
{
$response = m::mock('Illuminate\Http\JsonResponse')
->shouldReceive('getStatusCode')
->once()
->andReturn(200)
->shouldReceive('getContent')
->once()
->andReturn($content)
->getMock();

$response
->shouldReceive('header')
->once()
->with('ETag', $etag);

return $response;
}

protected function getResponseWithErrorCode(int $code = 401, int $times = 1)
{
$response = m::mock('Illuminate\Http\Response');
Expand Down

0 comments on commit 2d55e9c

Please sign in to comment.