FileDisplayResponseTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Http;
  7. use OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http\FileDisplayResponse;
  9. use OCP\Files\File;
  10. class FileDisplayResponseTest extends \Test\TestCase {
  11. /** @var File|\PHPUnit\Framework\MockObject\MockObject */
  12. private $file;
  13. /** @var FileDisplayResponse */
  14. private $response;
  15. protected function setUp(): void {
  16. $this->file = $this->getMockBuilder('OCP\Files\File')
  17. ->getMock();
  18. $this->file->expects($this->once())
  19. ->method('getETag')
  20. ->willReturn('myETag');
  21. $this->file->expects($this->once())
  22. ->method('getName')
  23. ->willReturn('myFileName');
  24. $this->file->expects($this->once())
  25. ->method('getMTime')
  26. ->willReturn(1464825600);
  27. $this->response = new FileDisplayResponse($this->file);
  28. }
  29. public function testHeader(): void {
  30. $headers = $this->response->getHeaders();
  31. $this->assertArrayHasKey('Content-Disposition', $headers);
  32. $this->assertSame('inline; filename="myFileName"', $headers['Content-Disposition']);
  33. }
  34. public function testETag(): void {
  35. $this->assertSame('myETag', $this->response->getETag());
  36. }
  37. public function testLastModified(): void {
  38. $lastModified = $this->response->getLastModified();
  39. $this->assertNotNull($lastModified);
  40. $this->assertSame(1464825600, $lastModified->getTimestamp());
  41. }
  42. public function test304(): void {
  43. $output = $this->getMockBuilder('OCP\AppFramework\Http\IOutput')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $output->expects($this->any())
  47. ->method('getHttpResponseCode')
  48. ->willReturn(Http::STATUS_NOT_MODIFIED);
  49. $output->expects($this->never())
  50. ->method('setOutput');
  51. $this->file->expects($this->never())
  52. ->method('getContent');
  53. $this->response->callback($output);
  54. }
  55. public function testNon304(): void {
  56. $output = $this->getMockBuilder('OCP\AppFramework\Http\IOutput')
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $output->expects($this->any())
  60. ->method('getHttpResponseCode')
  61. ->willReturn(Http::STATUS_OK);
  62. $output->expects($this->once())
  63. ->method('setOutput')
  64. ->with($this->equalTo('my data'));
  65. $output->expects($this->once())
  66. ->method('setHeader')
  67. ->with($this->equalTo('Content-Length: 42'));
  68. $this->file->expects($this->once())
  69. ->method('getContent')
  70. ->willReturn('my data');
  71. $this->file->expects($this->any())
  72. ->method('getSize')
  73. ->willReturn(42);
  74. $this->response->callback($output);
  75. }
  76. }