2015-01-22 14:26:46 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-01-22 14:26:46 -05:00
|
|
|
*/
|
|
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
namespace Test\AppFramework\Http;
|
2015-01-22 14:26:46 -05:00
|
|
|
|
|
|
|
|
use OCP\AppFramework\Http;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http\IOutput;
|
|
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
2015-01-22 14:26:46 -05:00
|
|
|
|
|
|
|
|
class StreamResponseTest extends \Test\TestCase {
|
|
|
|
|
/** @var IOutput */
|
|
|
|
|
private $output;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
parent::setUp();
|
2016-07-10 08:17:26 -04:00
|
|
|
$this->output = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2015-01-22 14:26:46 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOutputNotModified(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
$path = __FILE__;
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('getHttpResponseCode')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(Http::STATUS_NOT_MODIFIED);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->output->expects($this->never())
|
|
|
|
|
->method('setReadfile');
|
|
|
|
|
$response = new StreamResponse($path);
|
|
|
|
|
|
|
|
|
|
$response->callback($this->output);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOutputOk(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
$path = __FILE__;
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('getHttpResponseCode')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(Http::STATUS_OK);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('setReadfile')
|
|
|
|
|
->with($this->equalTo($path))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(true);
|
2015-01-22 14:26:46 -05:00
|
|
|
$response = new StreamResponse($path);
|
|
|
|
|
|
|
|
|
|
$response->callback($this->output);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOutputNotFound(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
$path = __FILE__ . 'test';
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('getHttpResponseCode')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(Http::STATUS_OK);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->output->expects($this->never())
|
|
|
|
|
->method('setReadfile');
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('setHttpResponseCode')
|
|
|
|
|
->with($this->equalTo(Http::STATUS_NOT_FOUND));
|
|
|
|
|
$response = new StreamResponse($path);
|
|
|
|
|
|
|
|
|
|
$response->callback($this->output);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOutputReadFileError(): void {
|
2015-01-22 14:26:46 -05:00
|
|
|
$path = __FILE__;
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('getHttpResponseCode')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(Http::STATUS_OK);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('setReadfile')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(false);
|
2015-01-22 14:26:46 -05:00
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('setHttpResponseCode')
|
|
|
|
|
->with($this->equalTo(Http::STATUS_BAD_REQUEST));
|
|
|
|
|
$response = new StreamResponse($path);
|
|
|
|
|
|
|
|
|
|
$response->callback($this->output);
|
|
|
|
|
}
|
|
|
|
|
}
|