1
0

FileDisplayResponseTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Http;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\FileDisplayResponse;
  26. use OCP\Files\File;
  27. class FileDisplayResponseTest extends \Test\TestCase {
  28. /** @var File|\PHPUnit_Framework_MockObject_MockObject */
  29. private $file;
  30. /** @var FileDisplayResponse */
  31. private $response;
  32. public function setup() {
  33. $this->file = $this->getMockBuilder('OCP\Files\File')
  34. ->getMock();
  35. $this->file->expects($this->once())
  36. ->method('getETag')
  37. ->willReturn('myETag');
  38. $this->file->expects($this->once())
  39. ->method('getName')
  40. ->willReturn('myFileName');
  41. $this->file->expects($this->once())
  42. ->method('getMTime')
  43. ->willReturn(1464825600);
  44. $this->response = new FileDisplayResponse($this->file);
  45. }
  46. public function testHeader() {
  47. $headers = $this->response->getHeaders();
  48. $this->assertArrayHasKey('Content-Disposition', $headers);
  49. $this->assertSame('inline; filename="myFileName"', $headers['Content-Disposition']);
  50. }
  51. public function testETag() {
  52. $this->assertSame('myETag', $this->response->getETag());
  53. }
  54. public function testLastModified() {
  55. $lastModified = $this->response->getLastModified();
  56. $this->assertNotNull($lastModified);
  57. $this->assertSame(1464825600, $lastModified->getTimestamp());
  58. }
  59. public function test304() {
  60. $output = $this->getMockBuilder('OCP\AppFramework\Http\IOutput')
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $output->expects($this->any())
  64. ->method('getHttpResponseCode')
  65. ->willReturn(Http::STATUS_NOT_MODIFIED);
  66. $output->expects($this->never())
  67. ->method('setOutput');
  68. $this->file->expects($this->never())
  69. ->method('getContent');
  70. $this->response->callback($output);
  71. }
  72. public function testNon304() {
  73. $output = $this->getMockBuilder('OCP\AppFramework\Http\IOutput')
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $output->expects($this->any())
  77. ->method('getHttpResponseCode')
  78. ->willReturn(Http::STATUS_OK);
  79. $output->expects($this->once())
  80. ->method('setOutput')
  81. ->with($this->equalTo('my data'));
  82. $output->expects($this->once())
  83. ->method('setHeader')
  84. ->with($this->equalTo('Content-Length: 42'));
  85. $this->file->expects($this->once())
  86. ->method('getContent')
  87. ->willReturn('my data');
  88. $this->file->expects($this->any())
  89. ->method('getSize')
  90. ->willReturn(42);
  91. $this->response->callback($output);
  92. }
  93. }