Skip to content

Commit c4e4223

Browse files
committed
Set protocol version for response in middleware
1 parent 233a9ae commit c4e4223

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Auth/Middleware.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ protected function forbidden(ServerRequestInterface $request, ResponseInterface
7676
{
7777
$unauthorized = $this->auth->user() === null;
7878

79-
$forbiddenResponse = $response->withStatus($unauthorized ? 401 : 403);
79+
$forbiddenResponse = $response
80+
->withProtocolVersion($request->getProtocolVersion())
81+
->withStatus($unauthorized ? 401 : 403);
8082
$forbiddenResponse->getBody()->write('Access denied');
8183

8284
return $forbiddenResponse;

tests/Auth/MiddlewareTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function testInvokeUnauthorized()
153153

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

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

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

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

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

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

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

192196
$next = $this->createCallbackMock($this->never());

0 commit comments

Comments
 (0)