CorsMiddlewareTest.php 1.97 KB
Newer Older
Tifenn Guillas's avatar
Tifenn Guillas committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

/*
 * This file is part of Anis Auth.
 *
 * (c) Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);

namespace App\Tests\Middleware;

use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

final class CorsMiddlewareTest extends TestCase
{
    public function testCorsHeadersForOptionsMethod()
    {
        $request = new ServerRequest('OPTIONS', '/');

        $requestHandler = $this->getMockBuilder(RequestHandler::class)
            ->disableOriginalConstructor()
            ->setMethods(['handle'])
            ->getMock();

        $requestHandler->method('handle')
            ->with($this->identicalTo($request))
            ->will($this->returnValue(new Response()));

        $corsMiddleware = new \App\Middleware\CorsMiddleware();
        $response = $corsMiddleware->process($request, $requestHandler);
        $this->assertSame((string) $response->getHeaderLine('Access-Control-Allow-Origin'), '*');
François Agneray's avatar
François Agneray committed
38
39
40
41
        $this->assertSame(
            'Content-Type, Authorization',
            (string) $response->getHeaderLine('Access-Control-Allow-Headers')
        );
Tifenn Guillas's avatar
Tifenn Guillas committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    }
    
    public function testCorsHeadersForGetMethod()
    {
        $request = new ServerRequest('GET', '/');

        $requestHandler = $this->getMockBuilder(RequestHandler::class)
            ->disableOriginalConstructor()
            ->setMethods(['handle'])
            ->getMock();

        $requestHandler->method('handle')
            ->with($this->identicalTo($request))
            ->will($this->returnValue(new Response()));

        $corsMiddleware = new \App\Middleware\CorsMiddleware();
        $response = $corsMiddleware->process($request, $requestHandler);
        $this->assertSame((string) $response->getHeaderLine('Access-Control-Allow-Origin'), '*');
    }
}