NotModifiedMiddlewareTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\Middleware;
  25. use OC\AppFramework\Middleware\NotModifiedMiddleware;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\IRequest;
  29. class NotModifiedMiddlewareTest extends \Test\TestCase {
  30. /** @var IRequest */
  31. private $request;
  32. /** @var Controller */
  33. private $controller;
  34. /** @var NotModifiedMiddleware */
  35. private $middleWare;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->request = $this->createMock(IRequest::class);
  39. $this->middleWare = new NotModifiedMiddleware(
  40. $this->request
  41. );
  42. $this->controller = $this->createMock(Controller::class);
  43. }
  44. public function dataModified(): array {
  45. $now = new \DateTime();
  46. return [
  47. [null, '', null, '', false],
  48. ['etag', 'etag', null, '', false],
  49. ['etag', '"wrongetag"', null, '', false],
  50. ['etag', '', null, '', false],
  51. [null, '"etag"', null, '', false],
  52. ['etag', '"etag"', null, '', true],
  53. [null, '', $now, $now->format(\DateTimeInterface::RFC2822), true],
  54. [null, '', $now, $now->format(\DateTimeInterface::ATOM), false],
  55. [null, '', null, $now->format(\DateTimeInterface::RFC2822), false],
  56. [null, '', $now, '', false],
  57. ['etag', '"etag"', $now, $now->format(\DateTimeInterface::ATOM), true],
  58. ['etag', '"etag"', $now, $now->format(\DateTimeInterface::RFC2822), true],
  59. ];
  60. }
  61. /**
  62. * @dataProvider dataModified
  63. */
  64. public function testMiddleware(?string $etag, string $etagHeader, ?\DateTime $lastModified, string $lastModifiedHeader, bool $notModifiedSet) {
  65. $this->request->method('getHeader')
  66. ->willReturnCallback(function (string $name) use ($etagHeader, $lastModifiedHeader) {
  67. if ($name === 'IF_NONE_MATCH') {
  68. return $etagHeader;
  69. }
  70. if ($name === 'IF_MODIFIED_SINCE') {
  71. return $lastModifiedHeader;
  72. }
  73. return '';
  74. });
  75. $response = new Http\Response();
  76. if ($etag !== null) {
  77. $response->setETag($etag);
  78. }
  79. if ($lastModified !== null) {
  80. $response->setLastModified($lastModified);
  81. }
  82. $response->setStatus(Http::STATUS_OK);
  83. $result = $this->middleWare->afterController($this->controller, 'myfunction', $response);
  84. if ($notModifiedSet) {
  85. $this->assertSame(Http::STATUS_NOT_MODIFIED, $result->getStatus());
  86. } else {
  87. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  88. }
  89. }
  90. }