Skip to content

Commit cc2a105

Browse files
committed
Support subdir in the OCS v2 endpoint
We should check against the ending substring since people could run their nextcloud in a subfolder. * Added test
1 parent 0e816a6 commit cc2a105

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/private/AppFramework/Middleware/OCSMiddleware.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ 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+
60+
$response = new OCSResponse($format, $code, $exception->getMessage());
61+
62+
if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) {
63+
$response->setStatus($code);
64+
}
65+
return $response;
6066
}
6167

6268
throw $exception;

tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public function dataAfterException() {
8989
* @param int $code
9090
*/
9191
public function testAfterException($controller, $exception, $forward, $message = '', $code = 0) {
92+
$this->request
93+
->method('getScriptName')
94+
->willReturn('/mysubfolder/ocs/v1.php');
9295
$OCSMiddleware = new OCSMiddleware($this->request);
9396

9497
try {
@@ -105,4 +108,34 @@ public function testAfterException($controller, $exception, $forward, $message =
105108
}
106109
}
107110

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

0 commit comments

Comments
 (0)