File: CorsServiceTest.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 (289 lines) | stat: -rw-r--r-- 10,776 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?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 Fruitcake\Cors\Exceptions\InvalidOptionException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

/**
 * @phpstan-type CorsNormalizedOptions array{
 *  'allowedOrigins': string[],
 *  'allowedOriginsPatterns': string[],
 *  'supportsCredentials': bool,
 *  'allowedHeaders': string[],
 *  'allowedMethods': string[],
 *  'exposedHeaders': string[],
 *  'maxAge': int|bool|null,
 *  'allowAllOrigins': bool,
 *  'allowAllHeaders': bool,
 *  'allowAllMethods': bool,
 * }
 */
class CorsServiceTest extends TestCase
{
    #[Test]
    public function itCanHaveOptions(): void
    {
        $options = [
            'allowedOrigins' => ['localhost'],
            'allowedOriginsPatterns' => ['/something/'],
            'allowedHeaders' => ['x-custom'],
            'allowedMethods' => ['PUT'],
            'maxAge' => 684,
            'supportsCredentials' => true,
            'exposedHeaders' => ['x-custom-2'],
        ];

        $service = new CorsService($options);

        $this->assertInstanceOf(CorsService::class, $service);

        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']);
        $this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']);
        $this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']);
        $this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']);
        $this->assertEquals($options['maxAge'], $normalized['maxAge']);
        $this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']);
        $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
    }

    #[Test]
    public function itCanSetOptions(): void
    {
        $service = new CorsService();
        $normalized = $this->getOptionsFromService($service);
        $this->assertEquals([], $normalized['allowedOrigins']);

        $this->assertInstanceOf(CorsService::class, $service);

        $options = [
            'allowedOrigins' => ['localhost'],
            'allowedOriginsPatterns' => ['/something/'],
            'allowedHeaders' => ['x-custom'],
            'allowedMethods' => ['PUT'],
            'maxAge' => 684,
            'supportsCredentials' => true,
            'exposedHeaders' => ['x-custom-2'],
        ];

        $service->setOptions($options);

        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']);
        $this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']);
        $this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']);
        $this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']);
        $this->assertEquals($options['maxAge'], $normalized['maxAge']);
        $this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']);
        $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
    }

    #[Test]
    public function itCanOverwriteSetOptions(): void
    {
        $service = new CorsService(['allowedOrigins' => ['example.com']]);
        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals(['example.com'], $normalized['allowedOrigins']);

        $this->assertInstanceOf(CorsService::class, $service);

        $options = [
            'allowedOrigins' => ['localhost'],
            'allowedOriginsPatterns' => ['/something/'],
            'allowedHeaders' => ['x-custom'],
            'allowedMethods' => ['PUT'],
            'maxAge' => 684,
            'supportsCredentials' => true,
            'exposedHeaders' => ['x-custom-2'],
        ];

        $service->setOptions($options);

        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']);
        $this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']);
        $this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']);
        $this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']);
        $this->assertEquals($options['maxAge'], $normalized['maxAge']);
        $this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']);
        $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
    }

    #[Test]
    public function itCanHaveNoOptions(): void
    {
        $service = new CorsService();
        $this->assertInstanceOf(CorsService::class, $service);

        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals([], $normalized['allowedOrigins']);
        $this->assertEquals([], $normalized['allowedOriginsPatterns']);
        $this->assertEquals([], $normalized['allowedHeaders']);
        $this->assertEquals([], $normalized['allowedMethods']);
        $this->assertEquals([], $normalized['exposedHeaders']);
        $this->assertEquals(0, $normalized['maxAge']);
        $this->assertEquals(false, $normalized['supportsCredentials']);
    }

    #[Test]
    public function itCanHaveEmptyOptions(): void
    {
        $service = new CorsService([]);
        $this->assertInstanceOf(CorsService::class, $service);

        $normalized = $this->getOptionsFromService($service);

        $this->assertEquals([], $normalized['allowedOrigins']);
        $this->assertEquals([], $normalized['allowedOriginsPatterns']);
        $this->assertEquals([], $normalized['allowedHeaders']);
        $this->assertEquals([], $normalized['allowedMethods']);
        $this->assertEquals([], $normalized['exposedHeaders']);
        $this->assertEquals(0, $normalized['maxAge']);
        $this->assertEquals(false, $normalized['supportsCredentials']);
    }

    #[Test]
    public function itNormalizesFalseExposedHeaders(): void
    {
        $service = new CorsService(['exposedHeaders' => false]);
        $this->assertEquals([], $this->getOptionsFromService($service)['exposedHeaders']);
    }

    #[Test]
    public function itAllowsNullMaxAge(): void
    {
        $service = new CorsService(['maxAge' => null]);
        $this->assertNull($this->getOptionsFromService($service)['maxAge']);
    }

    #[Test]
    public function itAllowsZeroMaxAge(): void
    {
        $service = new CorsService(['maxAge' => 0]);
        $this->assertEquals(0, $this->getOptionsFromService($service)['maxAge']);
    }

    #[Test]
    public function itThrowsExceptionOnInvalidExposedHeaders(): void
    {
        $this->expectException(\TypeError::class);

        /** @phpstan-ignore-next-line */
        $service = new CorsService(['exposedHeaders' => true]);
    }

    #[Test]
    public function itThrowsExceptionOnInvalidOriginsArray(): void
    {
        $this->expectException(\TypeError::class);

        /** @phpstan-ignore-next-line */
        $service = new CorsService(['allowedOrigins' => 'string']);
    }

    #[Test]
    public function itNormalizesWildcardOrigins(): void
    {
        $service = new CorsService(['allowedOrigins' => ['*']]);
        $this->assertInstanceOf(CorsService::class, $service);

        $this->assertTrue($this->getOptionsFromService($service)['allowAllOrigins']);
    }

    #[Test]
    public function itNormalizesWildcardHeaders(): void
    {
        $service = new CorsService(['allowedHeaders' => ['*']]);
        $this->assertInstanceOf(CorsService::class, $service);

        $this->assertTrue($this->getOptionsFromService($service)['allowAllHeaders']);
    }

    #[Test]
    public function itNormalizesWildcardMethods(): void
    {
        $service = new CorsService(['allowedMethods' => ['*']]);
        $this->assertInstanceOf(CorsService::class, $service);

        $this->assertTrue($this->getOptionsFromService($service)['allowAllMethods']);
    }

    #[Test]
    public function itConvertsWildcardOriginPatterns(): void
    {
        $service = new CorsService(['allowedOrigins' => ['*.mydomain.com']]);
        $this->assertInstanceOf(CorsService::class, $service);

        $patterns = $this->getOptionsFromService($service)['allowedOriginsPatterns'];
        $this->assertEquals(['#^.*\.mydomain\.com\z#u'], $patterns);
    }

    #[Test]
    public function itNormalizesUnderscoreOptions(): void
    {
        $options = [
            'allowed_origins' => ['localhost'],
            'allowed_origins_patterns' => ['/something/'],
            'allowed_headers' => ['x-custom'],
            'allowed_methods' => ['PUT'],
            'max_age' => 684,
            'supports_credentials' => true,
            'exposed_headers' => ['x-custom-2'],
        ];

        $service = new CorsService($options);
        $this->assertInstanceOf(CorsService::class, $service);

        $this->assertEquals($options['allowed_origins'], $this->getOptionsFromService($service)['allowedOrigins']);
        $this->assertEquals(
            $options['allowed_origins_patterns'],
            $this->getOptionsFromService($service)['allowedOriginsPatterns']
        );
        $this->assertEquals($options['allowed_headers'], $this->getOptionsFromService($service)['allowedHeaders']);
        $this->assertEquals($options['allowed_methods'], $this->getOptionsFromService($service)['allowedMethods']);
        $this->assertEquals($options['exposed_headers'], $this->getOptionsFromService($service)['exposedHeaders']);
        $this->assertEquals($options['max_age'], $this->getOptionsFromService($service)['maxAge']);
        $this->assertEquals(
            $options['supports_credentials'],
            $this->getOptionsFromService($service)['supportsCredentials']
        );
    }

    /**
     * @param CorsService $service
     * @return CorsNormalizedOptions
     */
    private function getOptionsFromService(CorsService $service): array
    {
        $reflected = new \ReflectionClass($service);

        $properties = $reflected->getProperties(\ReflectionProperty::IS_PRIVATE);

        $options = [];
        foreach ($properties as $property) {
            $property->setAccessible(true);
            $options[$property->getName()] = $property->getValue($service);
        }

        /** @var CorsNormalizedOptions $options */
        return $options;
    }
}