Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
19
use core\di;
20
use core\tests\router\route_testcase;
21
use GuzzleHttp\Psr7\ServerRequest;
22
use Psr\Http\Message\ResponseInterface;
23
 
24
/**
25
 * Tests for the CORS middleware.
26
 *
27
 * @package    core
28
 * @category   test
29
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers \core\router\middleware\cors_middleware
32
 */
33
final class cors_middleware_test extends route_testcase {
34
    /**
35
     * Standard CORS headers are added.
36
     */
37
    public function test_cors_headers(): void {
38
        $app = $this->get_simple_app();
39
        $app->add(di::get(cors_middleware::class));
40
        $app->addRoutingMiddleware();
41
 
42
        $app->map(['GET'], '/test', function ($request, $response) {
43
            return $response;
44
        });
45
 
46
        // Handle the request.
47
        $request = new ServerRequest('GET', '/test');
48
        $returns = $app->handle($request);
49
        $this->assertInstanceOf(ResponseInterface::class, $returns);
50
 
51
        // Assert the relevant CORS headers.
52
        $this->assertEquals('*', $returns->getHeaderLine('Access-Control-Allow-Origin'));
53
        $this->assertEquals('GET', $returns->getHeaderLine('Access-Control-Allow-Methods'));
54
 
55
        // Check the allowed headers.
56
        $allowedheaders = $returns->getHeaderLine('Access-Control-Allow-Headers');
57
        $this->assertStringContainsString('Content-Type', $allowedheaders);
58
        $this->assertStringContainsString('api_key', $allowedheaders);
59
        $this->assertStringContainsString('Authorization', $allowedheaders);
60
    }
61
 
62
    /**
63
     * CORS methods are added for multiple routes matching the same path.
64
     */
65
    public function test_cors_multiple_methods_headers(): void {
66
        $app = $this->get_simple_app();
67
        $app->add(di::get(cors_middleware::class));
68
        $app->addRoutingMiddleware();
69
 
70
        $app->map(['GET'], '/test', fn ($request, $response) => $response);
71
        $app->map(['POST'], '/test', fn ($request, $response) => $response);
72
        $app->map(['PUT', 'PATCH'], '/test', fn ($request, $response) => $response);
73
        $app->map(['DELETE'], '/test', fn ($request, $response) => $response);
74
 
75
        // Handle the request.
76
        $request = new ServerRequest('GET', '/test');
77
        $returns = $app->handle($request);
78
        $this->assertInstanceOf(ResponseInterface::class, $returns);
79
 
80
        // Assert the relevant CORS headers.
81
        $this->assertEquals('*', $returns->getHeaderLine('Access-Control-Allow-Origin'));
82
        $this->assertEquals('GET,POST,PUT,PATCH,DELETE', $returns->getHeaderLine('Access-Control-Allow-Methods'));
83
    }
84
}