1
0

NotModifiedMiddlewareTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Middleware;
  8. use OC\AppFramework\Middleware\NotModifiedMiddleware;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\IRequest;
  12. class NotModifiedMiddlewareTest extends \Test\TestCase {
  13. /** @var IRequest */
  14. private $request;
  15. /** @var Controller */
  16. private $controller;
  17. /** @var NotModifiedMiddleware */
  18. private $middleWare;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->request = $this->createMock(IRequest::class);
  22. $this->middleWare = new NotModifiedMiddleware(
  23. $this->request
  24. );
  25. $this->controller = $this->createMock(Controller::class);
  26. }
  27. public function dataModified(): array {
  28. $now = new \DateTime();
  29. return [
  30. [null, '', null, '', false],
  31. ['etag', 'etag', null, '', false],
  32. ['etag', '"wrongetag"', null, '', false],
  33. ['etag', '', null, '', false],
  34. [null, '"etag"', null, '', false],
  35. ['etag', '"etag"', null, '', true],
  36. [null, '', $now, $now->format(\DateTimeInterface::RFC2822), true],
  37. [null, '', $now, $now->format(\DateTimeInterface::ATOM), false],
  38. [null, '', null, $now->format(\DateTimeInterface::RFC2822), false],
  39. [null, '', $now, '', false],
  40. ['etag', '"etag"', $now, $now->format(\DateTimeInterface::ATOM), true],
  41. ['etag', '"etag"', $now, $now->format(\DateTimeInterface::RFC2822), true],
  42. ];
  43. }
  44. /**
  45. * @dataProvider dataModified
  46. */
  47. public function testMiddleware(?string $etag, string $etagHeader, ?\DateTime $lastModified, string $lastModifiedHeader, bool $notModifiedSet) {
  48. $this->request->method('getHeader')
  49. ->willReturnCallback(function (string $name) use ($etagHeader, $lastModifiedHeader) {
  50. if ($name === 'IF_NONE_MATCH') {
  51. return $etagHeader;
  52. }
  53. if ($name === 'IF_MODIFIED_SINCE') {
  54. return $lastModifiedHeader;
  55. }
  56. return '';
  57. });
  58. $response = new Http\Response();
  59. if ($etag !== null) {
  60. $response->setETag($etag);
  61. }
  62. if ($lastModified !== null) {
  63. $response->setLastModified($lastModified);
  64. }
  65. $response->setStatus(Http::STATUS_OK);
  66. $result = $this->middleWare->afterController($this->controller, 'myfunction', $response);
  67. if ($notModifiedSet) {
  68. $this->assertSame(Http::STATUS_NOT_MODIFIED, $result->getStatus());
  69. } else {
  70. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  71. }
  72. }
  73. }