Skip to content

Commit

Permalink
Set protocol version for response in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Dec 28, 2016
1 parent 233a9ae commit c4e4223
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Auth/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ protected function forbidden(ServerRequestInterface $request, ResponseInterface
{
$unauthorized = $this->auth->user() === null;

$forbiddenResponse = $response->withStatus($unauthorized ? 401 : 403);
$forbiddenResponse = $response
->withProtocolVersion($request->getProtocolVersion())
->withStatus($unauthorized ? 401 : 403);
$forbiddenResponse->getBody()->write('Access denied');

return $forbiddenResponse;
Expand Down
4 changes: 4 additions & 0 deletions tests/Auth/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function testInvokeUnauthorized()

$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')->with('auth')->willReturn('user');
$request->expects($this->once())->method('getProtocolVersion')->willReturn('1.1');

$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())->method('write')->with('Access denied');
Expand All @@ -161,6 +162,7 @@ public function testInvokeUnauthorized()
$forbiddenResponse->expects($this->once())->method('getBody')->willReturn($stream);

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('withProtocolVersion')->with('1.1')->willReturnSelf();
$response->expects($this->once())->method('withStatus')->with(401)->willReturn($forbiddenResponse);

$next = $this->createCallbackMock($this->never());
Expand All @@ -179,6 +181,7 @@ public function testInvokeForbidden()

$request = $this->createMock(ServerRequestInterface::class);
$request->expects($this->once())->method('getAttribute')->with('auth')->willReturn('user');
$request->expects($this->once())->method('getProtocolVersion')->willReturn('1.1');

$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())->method('write')->with('Access denied');
Expand All @@ -187,6 +190,7 @@ public function testInvokeForbidden()
$forbiddenResponse->expects($this->once())->method('getBody')->willReturn($stream);

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('withProtocolVersion')->with('1.1')->willReturnSelf();
$response->expects($this->once())->method('withStatus')->with(403)->willReturn($forbiddenResponse);

$next = $this->createCallbackMock($this->never());
Expand Down

0 comments on commit c4e4223

Please sign in to comment.