StreamResponseTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\IOutput;
  10. use OCP\AppFramework\Http\StreamResponse;
  11. class StreamResponseTest extends \Test\TestCase {
  12. /** @var IOutput */
  13. private $output;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->output = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')
  17. ->disableOriginalConstructor()
  18. ->getMock();
  19. }
  20. public function testOutputNotModified() {
  21. $path = __FILE__;
  22. $this->output->expects($this->once())
  23. ->method('getHttpResponseCode')
  24. ->willReturn(Http::STATUS_NOT_MODIFIED);
  25. $this->output->expects($this->never())
  26. ->method('setReadfile');
  27. $response = new StreamResponse($path);
  28. $response->callback($this->output);
  29. }
  30. public function testOutputOk() {
  31. $path = __FILE__;
  32. $this->output->expects($this->once())
  33. ->method('getHttpResponseCode')
  34. ->willReturn(Http::STATUS_OK);
  35. $this->output->expects($this->once())
  36. ->method('setReadfile')
  37. ->with($this->equalTo($path))
  38. ->willReturn(true);
  39. $response = new StreamResponse($path);
  40. $response->callback($this->output);
  41. }
  42. public function testOutputNotFound() {
  43. $path = __FILE__ . 'test';
  44. $this->output->expects($this->once())
  45. ->method('getHttpResponseCode')
  46. ->willReturn(Http::STATUS_OK);
  47. $this->output->expects($this->never())
  48. ->method('setReadfile');
  49. $this->output->expects($this->once())
  50. ->method('setHttpResponseCode')
  51. ->with($this->equalTo(Http::STATUS_NOT_FOUND));
  52. $response = new StreamResponse($path);
  53. $response->callback($this->output);
  54. }
  55. public function testOutputReadFileError() {
  56. $path = __FILE__;
  57. $this->output->expects($this->once())
  58. ->method('getHttpResponseCode')
  59. ->willReturn(Http::STATUS_OK);
  60. $this->output->expects($this->once())
  61. ->method('setReadfile')
  62. ->willReturn(false);
  63. $this->output->expects($this->once())
  64. ->method('setHttpResponseCode')
  65. ->with($this->equalTo(Http::STATUS_BAD_REQUEST));
  66. $response = new StreamResponse($path);
  67. $response->callback($this->output);
  68. }
  69. }