StreamResponseTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Http;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\IOutput;
  25. use OCP\AppFramework\Http\StreamResponse;
  26. class StreamResponseTest extends \Test\TestCase {
  27. /** @var IOutput */
  28. private $output;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->output = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. }
  35. public function testOutputNotModified() {
  36. $path = __FILE__;
  37. $this->output->expects($this->once())
  38. ->method('getHttpResponseCode')
  39. ->willReturn(Http::STATUS_NOT_MODIFIED);
  40. $this->output->expects($this->never())
  41. ->method('setReadfile');
  42. $response = new StreamResponse($path);
  43. $response->callback($this->output);
  44. }
  45. public function testOutputOk() {
  46. $path = __FILE__;
  47. $this->output->expects($this->once())
  48. ->method('getHttpResponseCode')
  49. ->willReturn(Http::STATUS_OK);
  50. $this->output->expects($this->once())
  51. ->method('setReadfile')
  52. ->with($this->equalTo($path))
  53. ->willReturn(true);
  54. $response = new StreamResponse($path);
  55. $response->callback($this->output);
  56. }
  57. public function testOutputNotFound() {
  58. $path = __FILE__ . 'test';
  59. $this->output->expects($this->once())
  60. ->method('getHttpResponseCode')
  61. ->willReturn(Http::STATUS_OK);
  62. $this->output->expects($this->never())
  63. ->method('setReadfile');
  64. $this->output->expects($this->once())
  65. ->method('setHttpResponseCode')
  66. ->with($this->equalTo(Http::STATUS_NOT_FOUND));
  67. $response = new StreamResponse($path);
  68. $response->callback($this->output);
  69. }
  70. public function testOutputReadFileError() {
  71. $path = __FILE__;
  72. $this->output->expects($this->once())
  73. ->method('getHttpResponseCode')
  74. ->willReturn(Http::STATUS_OK);
  75. $this->output->expects($this->once())
  76. ->method('setReadfile')
  77. ->willReturn(false);
  78. $this->output->expects($this->once())
  79. ->method('setHttpResponseCode')
  80. ->with($this->equalTo(Http::STATUS_BAD_REQUEST));
  81. $response = new StreamResponse($path);
  82. $response->callback($this->output);
  83. }
  84. }