File: MockApp.php

package info (click to toggle)
php-fruitcake-php-cors 1.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 244 kB
  • sloc: php: 745; makefile: 10
file content (59 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

/*
 * This file is part of fruitcake/php-cors and was originally part of asm89/stack-cors
 *
 * (c) Alexander <iam.asm89@gmail.com>
 * (c) Barryvdh <barryvdh@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Fruitcake\Cors\Tests;

use Fruitcake\Cors\CorsService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @phpstan-import-type CorsInputOptions from CorsService
 */
class MockApp
{
    /** @var string[] */
    private $responseHeaders;

    /**
     * @var CorsService
     */
    private $cors;

    /**
     * @param string[] $responseHeaders
     * @param CorsInputOptions $options
     */
    public function __construct(array $responseHeaders, array $options = [])
    {
        $this->responseHeaders = $responseHeaders;
        $this->cors = new CorsService($options);
    }

    public function handle(Request $request): Response
    {
        if ($this->cors->isPreflightRequest($request)) {
            $response = $this->cors->handlePreflightRequest($request);
            return $this->cors->varyHeader($response, 'Access-Control-Request-Method');
        }

        $response = new Response();

        $response->headers->add($this->responseHeaders);

        if ($request->getMethod() === 'OPTIONS') {
            $this->cors->varyHeader($response, 'Access-Control-Request-Method');
        }

        return $this->cors->addActualRequestHeaders($response, $request);
    }
}