1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router\middleware;
|
|
|
18 |
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
19 |
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for uri_normalisation_middleware.
|
|
|
23 |
*
|
|
|
24 |
* @package core
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core\router\middleware\uri_normalisation_middleware
|
|
|
29 |
*/
|
|
|
30 |
final class uri_normalisation_middleware_test extends \advanced_testcase {
|
|
|
31 |
/**
|
|
|
32 |
* Test the normalisation of URIs.
|
|
|
33 |
*
|
|
|
34 |
* @dataProvider data_provider
|
|
|
35 |
* @param string $input The input URI.
|
|
|
36 |
* @param string $expected The expected output URI.
|
|
|
37 |
*/
|
|
|
38 |
public function test_normalisation(
|
|
|
39 |
string $input,
|
|
|
40 |
string $expected,
|
|
|
41 |
): void {
|
|
|
42 |
$request = new ServerRequest('GET', $input);
|
|
|
43 |
$handler = new class () implements \Psr\Http\Server\RequestHandlerInterface {
|
|
|
44 |
#[\Override]
|
|
|
45 |
public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface {
|
|
|
46 |
return new \GuzzleHttp\Psr7\Response();
|
|
|
47 |
}
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
$handler = $this->getMockBuilder(RequestHandlerInterface::class)->getMock();
|
|
|
51 |
$handler->expects($this->once())
|
|
|
52 |
->method('handle')
|
|
|
53 |
->with(
|
|
|
54 |
$this->callback(function ($request) use ($expected) {
|
|
|
55 |
return $request->getUri()->getPath() === $expected;
|
|
|
56 |
}),
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
$middleware = \core\di::get(uri_normalisation_middleware::class);
|
|
|
60 |
$middleware->process($request, $handler);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Data provider for test_normalisation.
|
|
|
65 |
*/
|
|
|
66 |
public static function data_provider(): array {
|
|
|
67 |
return [
|
|
|
68 |
'Empty URI' => [
|
|
|
69 |
'',
|
|
|
70 |
'/',
|
|
|
71 |
],
|
|
|
72 |
'Duplicate slashes' => [
|
|
|
73 |
'/test//path',
|
|
|
74 |
'/test/path',
|
|
|
75 |
],
|
|
|
76 |
'Trailing slash' => [
|
|
|
77 |
'/test/path/',
|
|
|
78 |
'/test/path',
|
|
|
79 |
],
|
|
|
80 |
'Multiple duplicate slashes' => [
|
|
|
81 |
'/test///path//with//more//than//one',
|
|
|
82 |
'/test/path/with/more/than/one',
|
|
|
83 |
],
|
|
|
84 |
];
|
|
|
85 |
}
|
|
|
86 |
}
|