Skip to content

Commit 9dafc51

Browse files
authored
Merge pull request #524 from nextcloud/ocs_appframework_status_code
Set proper status code in OCS AppFramework Middleware
2 parents af39501 + 72b06d2 commit 9dafc51

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/private/AppFramework/Middleware/OCSMiddleware.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ public function afterException($controller, $methodName, \Exception $exception)
5656
if ($code === 0) {
5757
$code = Http::STATUS_INTERNAL_SERVER_ERROR;
5858
}
59-
return new OCSResponse($format, $code, $exception->getMessage());
59+
$response = new OCSResponse($format, $code, $exception->getMessage());
60+
61+
if ($this->request->getScriptName() === '/ocs/v2.php') {
62+
$response->setStatus($code);
63+
}
64+
return $response;
6065
}
6166

6267
throw $exception;

tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ public function dataAfterException() {
8888
* @param string $message
8989
* @param int $code
9090
*/
91-
public function testAfterException($controller, $exception, $forward, $message = '', $code = 0) {
91+
public function testAfterExceptionOCSv1($controller, $exception, $forward, $message = '', $code = 0) {
92+
$this->request
93+
->method('getScriptName')
94+
->willReturn('/ocs/v1.php');
9295
$OCSMiddleware = new OCSMiddleware($this->request);
9396

9497
try {
@@ -99,6 +102,37 @@ public function testAfterException($controller, $exception, $forward, $message =
99102

100103
$this->assertSame($message, $this->invokePrivate($result, 'message'));
101104
$this->assertSame($code, $this->invokePrivate($result, 'statuscode'));
105+
$this->assertSame(200, $result->getStatus());
106+
} catch (\Exception $e) {
107+
$this->assertTrue($forward);
108+
$this->assertEquals($exception, $e);
109+
}
110+
}
111+
112+
/**
113+
* @dataProvider dataAfterException
114+
*
115+
* @param Controller $controller
116+
* @param \Exception $exception
117+
* @param bool $forward
118+
* @param string $message
119+
* @param int $code
120+
*/
121+
public function testAfterExceptionOCSv2($controller, $exception, $forward, $message = '', $code = 0) {
122+
$this->request
123+
->method('getScriptName')
124+
->willReturn('/ocs/v2.php');
125+
$OCSMiddleware = new OCSMiddleware($this->request);
126+
127+
try {
128+
$result = $OCSMiddleware->afterException($controller, 'method', $exception);
129+
$this->assertFalse($forward);
130+
131+
$this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result);
132+
133+
$this->assertSame($message, $this->invokePrivate($result, 'message'));
134+
$this->assertSame($code, $this->invokePrivate($result, 'statuscode'));
135+
$this->assertSame($code, $result->getStatus());
102136
} catch (\Exception $e) {
103137
$this->assertTrue($forward);
104138
$this->assertEquals($exception, $e);

0 commit comments

Comments
 (0)