|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +use OCA\UserOIDC\Service\RequestClassificationService; |
| 11 | +use OCP\IRequest; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class RequestClassificationServiceTest extends TestCase { |
| 16 | + #[DataProvider('topLevelHtmlNavigationProvider')] |
| 17 | + public function testIsTopLevelHtmlNavigation(string $method, array $headers, bool $expected): void { |
| 18 | + $request = $this->createMock(IRequest::class); |
| 19 | + $request->method('getMethod') |
| 20 | + ->willReturn($method); |
| 21 | + $request->method('getHeader') |
| 22 | + ->willReturnCallback(static function (string $name) use ($headers): string { |
| 23 | + return $headers[$name] ?? ''; |
| 24 | + }); |
| 25 | + |
| 26 | + self::assertSame($expected, RequestClassificationService::isTopLevelHtmlNavigation($request)); |
| 27 | + } |
| 28 | + |
| 29 | + public static function topLevelHtmlNavigationProvider(): array { |
| 30 | + return [ |
| 31 | + 'top level navigation with html accept' => [ |
| 32 | + 'GET', |
| 33 | + [ |
| 34 | + 'Accept' => 'text/html,application/xhtml+xml', |
| 35 | + 'Sec-Fetch-Mode' => 'navigate', |
| 36 | + 'Sec-Fetch-Dest' => 'document', |
| 37 | + ], |
| 38 | + true, |
| 39 | + ], |
| 40 | + 'html accept without fetch metadata' => [ |
| 41 | + 'GET', |
| 42 | + [ |
| 43 | + 'Accept' => 'text/html', |
| 44 | + ], |
| 45 | + true, |
| 46 | + ], |
| 47 | + 'xhr request' => [ |
| 48 | + 'GET', |
| 49 | + [ |
| 50 | + 'Accept' => 'text/html', |
| 51 | + 'X-Requested-With' => 'XMLHttpRequest', |
| 52 | + ], |
| 53 | + false, |
| 54 | + ], |
| 55 | + 'json request' => [ |
| 56 | + 'GET', |
| 57 | + [ |
| 58 | + 'Accept' => 'application/json', |
| 59 | + ], |
| 60 | + false, |
| 61 | + ], |
| 62 | + 'non navigate fetch mode' => [ |
| 63 | + 'GET', |
| 64 | + [ |
| 65 | + 'Accept' => 'text/html', |
| 66 | + 'Sec-Fetch-Mode' => 'cors', |
| 67 | + ], |
| 68 | + false, |
| 69 | + ], |
| 70 | + 'non document destination' => [ |
| 71 | + 'GET', |
| 72 | + [ |
| 73 | + 'Accept' => 'text/html', |
| 74 | + 'Sec-Fetch-Dest' => 'empty', |
| 75 | + ], |
| 76 | + false, |
| 77 | + ], |
| 78 | + 'non get request' => [ |
| 79 | + 'POST', |
| 80 | + [ |
| 81 | + 'Accept' => 'text/html', |
| 82 | + ], |
| 83 | + false, |
| 84 | + ], |
| 85 | + ]; |
| 86 | + } |
| 87 | +} |
0 commit comments