Completed
Push — master ( 4c5bed...613263 )
by Matthew Weier
06:22
created

testAppendVaryHeaderInNormalRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace ZfrCorsTest\Service;
20
21
use PHPUnit_Framework_TestCase as TestCase;
22
use Zend\Http\Response as HttpResponse;
23
use Zend\Http\Request as HttpRequest;
24
use Zend\Mvc\MvcEvent;
25
use ZfrCors\Options\CorsOptions;
26
use ZfrCors\Service\CorsService;
27
28
/**
29
 * Integration tests for {@see \ZfrCors\Service\CorsService}
30
 *
31
 * @author Florent Blaison <[email protected]>
32
 *
33
 * @covers \ZfrCors\Service\CorsService
34
 * @group Coverage
35
 */
36
class CorsServiceTest extends TestCase
37
{
38
    /**
39
     * @var CorsService
40
     */
41
    protected $corsService;
42
43
    /**
44
     * @var HttpResponse
45
     */
46
    protected $response;
47
48
    /**
49
     * @var HttpRequest
50
     */
51
    protected $request;
52
53
    /**
54
     * @var MvcEvent
55
     */
56
    protected $event;
57
58
    /**
59
     * @var CorsOptions
60
     */
61
    protected $corsOptions;
62
63
    /**
64
     * Set up
65
     */
66
    public function setUp()
67
    {
68
        parent::setUp();
69
70
        $this->corsOptions = new CorsOptions(
71
            [
72
                'allowed_origins'     => ['http://example.com'],
73
                'allowed_methods'     => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
74
                'allowed_headers'     => ['Content-Type', 'Accept'],
75
                'exposed_headers'     => ['Location'],
76
                'max_age'             => 10,
77
                'allowed_credentials' => true,
78
            ]
79
        );
80
81
        $this->corsService = new CorsService($this->corsOptions);
82
    }
83
84
    public function testCanDetectCorsRequest()
85
    {
86
        $request = new HttpRequest();
87
88
        $this->assertFalse($this->corsService->isCorsRequest($request));
89
90
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
91
        $this->assertEquals(true, $this->corsService->isCorsRequest($request));
92
    }
93
94
    public function testIsNotCorsRequestIfNotACrossRequest()
95
    {
96
        $request = new HttpRequest();
97
        $request->setUri('http://example.com');
98
99
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
100
        $this->assertEquals(false, $this->corsService->isCorsRequest($request));
101
    }
102
103
    public function testCanDetectPreflightRequest()
104
    {
105
        $request = new HttpRequest();
106
107
        $this->assertFalse($this->corsService->isPreflightRequest($request));
108
109
        $request->setMethod('OPTIONS');
110
        $this->assertFalse($this->corsService->isPreflightRequest($request));
111
112
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
113
        $this->assertFalse($this->corsService->isPreflightRequest($request));
114
115
        $request->getHeaders()->addHeaderLine('Access-Control-Request-Method', 'POST');
116
        $this->assertTrue($this->corsService->isPreflightRequest($request));
117
    }
118
119
    public function testProperlyCreatePreflightResponse()
120
    {
121
        $request  = new HttpRequest();
122
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
123
124
        $response = $this->corsService->createPreflightCorsResponse($request);
125
126
        $headers = $response->getHeaders();
127
128
        $this->assertEquals(200, $response->getStatusCode());
129
        $this->assertEquals('', $response->getContent());
130
        $this->assertEquals('http://example.com', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
        $this->assertEquals(
132
            'GET, POST, PUT, DELETE, OPTIONS',
133
            $headers->get('Access-Control-Allow-Methods')->getFieldValue()
134
        );
135
        $this->assertEquals('Content-Type, Accept', $headers->get('Access-Control-Allow-Headers')->getFieldValue());
136
        $this->assertEquals(10, $headers->get('Access-Control-Max-Age')->getFieldValue());
137
        $this->assertEquals(0, $headers->get('Content-Length')->getFieldValue());
138
139
        $this->assertEquals('true', $headers->get('Access-Control-Allow-Credentials')->getFieldValue());
140
    }
141
142
    public function testDoesNotAddAllowCredentialsHeadersIfAsked()
143
    {
144
        $request  = new HttpRequest();
145
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
146
        $this->corsOptions->setAllowedCredentials(false);
147
148
        $response = $this->corsService->createPreflightCorsResponse($request);
149
150
        $headers = $response->getHeaders();
151
        $this->assertFalse($headers->has('Access-Control-Allow-Credentials'));
152
    }
153
154
    public function testCanReturnWildCardAllowOrigin()
155
    {
156
        $request  = new HttpRequest();
157
        $request->getHeaders()->addHeaderLine('Origin', 'http://funny-origin.com');
158
        $this->corsOptions->setAllowedOrigins(['*']);
159
160
        $response = $this->corsService->createPreflightCorsResponse($request);
161
162
        $headers = $response->getHeaders();
163
        $this->assertEquals('http://funny-origin.com', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
164
    }
165
166
    public function testCanReturnWildCardSubDomainAllowOrigin()
167
    {
168
        $request  = new HttpRequest();
169
        $request->getHeaders()->addHeaderLine('Origin', 'http://subdomain.example.com');
170
        $this->corsOptions->setAllowedOrigins(['*.example.com']);
171
172
        $response = $this->corsService->createPreflightCorsResponse($request);
173
174
        $headers = $response->getHeaders();
175
        $headerValue = $headers->get('Access-Control-Allow-Origin')->getFieldValue();
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
176
        $this->assertEquals('http://subdomain.example.com', $headerValue);
177
    }
178
179
    public function testCanReturnWildCardSubDomainWithSchemeAllowOrigin()
180
    {
181
        $request  = new HttpRequest();
182
        $request->getHeaders()->addHeaderLine('Origin', 'https://subdomain.example.com');
183
        $this->corsOptions->setAllowedOrigins(['https://*.example.com']);
184
185
        $response = $this->corsService->createPreflightCorsResponse($request);
186
187
        $headers = $response->getHeaders();
188
        $headerValue = $headers->get('Access-Control-Allow-Origin')->getFieldValue();
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
189
        $this->assertEquals('https://subdomain.example.com', $headerValue);
190
    }
191
192
    public function testReturnNullForMissMatchedWildcardSubDomainOrigin()
193
    {
194
        $request  = new HttpRequest();
195
        $request->getHeaders()->addHeaderLine('Origin', 'http://subdomain.example.org');
196
        $this->corsOptions->setAllowedOrigins(['*.example.com']);
197
198
        $response = $this->corsService->createPreflightCorsResponse($request);
199
200
        $headers = $response->getHeaders();
201
        $this->assertEquals('null', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
202
    }
203
204
    public function testReturnNullForRootDomainOnWildcardSubDomainOrigin()
205
    {
206
        $request  = new HttpRequest();
207
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
208
        $this->corsOptions->setAllowedOrigins(['*.example.com']);
209
210
        $response = $this->corsService->createPreflightCorsResponse($request);
211
212
        $headers = $response->getHeaders();
213
        $this->assertEquals('null', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
214
    }
215
216
    public function testReturnNullForDifferentSchemeOnWildcardSubDomainOrigin()
217
    {
218
        $request  = new HttpRequest();
219
        $request->getHeaders()->addHeaderLine('Origin', 'https://example.com');
220
        $this->corsOptions->setAllowedOrigins(['http://*.example.com']);
221
222
        $response = $this->corsService->createPreflightCorsResponse($request);
223
224
        $headers = $response->getHeaders();
225
        $this->assertEquals('null', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
226
    }
227
228
    public function testReturnNullForUnknownOrigin()
229
    {
230
        $request  = new HttpRequest();
231
        $request->getHeaders()->addHeaderLine('Origin', 'http://unauthorized-origin.com');
232
233
        $response = $this->corsService->createPreflightCorsResponse($request);
234
235
        $headers = $response->getHeaders();
236
        $this->assertEquals('null', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
237
    }
238
239
    public function testEnsureVaryHeaderForNoOrigin()
240
    {
241
        $response = new HttpResponse();
242
243
        $this->corsService->ensureVaryHeader($response);
244
245
        $headers = $response->getHeaders();
246
247
        $this->assertEquals(false, $headers->get('Origin'));
248
        $this->assertNotEquals(false, $headers->get('Vary'));
249
    }
250
251
    public function testCanPopulateNormalCorsRequest()
252
    {
253
        $request  = new HttpRequest();
254
        $response = new HttpResponse();
255
256
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
257
258
        $this->corsService->populateCorsResponse($request, $response);
259
260
        $headers = $response->getHeaders();
261
262
        $this->assertEquals('http://example.com', $headers->get('Access-Control-Allow-Origin')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
263
        $this->assertEquals('Location', $headers->get('Access-Control-Expose-Headers')->getFieldValue());
264
    }
265
266
    public function testRefuseNormalCorsRequestIfUnauthorized()
267
    {
268
        $request  = new HttpRequest();
269
        $response = new HttpResponse();
270
271
        $request->getHeaders()->addHeaderLine('Origin', 'http://unauthorized.com');
272
273
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
274
            'ZfrCors\Exception\DisallowedOriginException',
275
            'The origin "http://unauthorized.com" is not authorized'
276
        );
277
278
        $this->corsService->populateCorsResponse($request, $response);
279
    }
280
281
    public function testAddVaryHeaderInNormalRequest()
282
    {
283
        $request  = new HttpRequest();
284
        $response = new HttpResponse();
285
286
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
287
288
        $this->corsService->populateCorsResponse($request, $response);
289
290
        $headers = $response->getHeaders();
291
        $this->assertTrue($headers->has('Vary'));
292
    }
293
294
    public function testAppendVaryHeaderInNormalRequest()
295
    {
296
        $request  = new HttpRequest();
297
        $response = new HttpResponse();
298
299
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
300
        $response->getHeaders()->addHeaderLine('Vary', 'Foo');
301
302
        $this->corsService->populateCorsResponse($request, $response);
303
304
        $headers = $response->getHeaders();
305
        $this->assertTrue($headers->has('Vary'));
306
        $this->assertEquals('Foo, Origin', $headers->get('Vary')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
307
    }
308
309
    public function testPopulatesAllowCredentialsNormalCorsRequest()
310
    {
311
        $request  = new HttpRequest();
312
        $response = new HttpResponse();
313
314
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
315
316
        $this->corsService->populateCorsResponse($request, $response);
317
318
        $headers = $response->getHeaders();
319
320
        $this->assertEquals('true', $headers->get('Access-Control-Allow-Credentials')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
321
    }
322
323
    public function testCanDetectCorsRequestFromSameHostButDifferentPort()
324
    {
325
        $request = new HttpRequest();
326
        $request->setUri('http://example.com');
327
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com:9000');
328
        $this->assertTrue($this->corsService->isCorsRequest($request));
329
    }
330
331
    public function testCanDetectCorsRequestFromSameHostButDifferentScheme()
332
    {
333
        $request = new HttpRequest();
334
        $request->setUri('https://example.com');
335
        $request->getHeaders()->addHeaderLine('Origin', 'http://example.com');
336
        $this->assertTrue($this->corsService->isCorsRequest($request));
337
    }
338
339
    /**
340
     * @see https://github.com/zf-fr/zfr-cors/issues/44
341
     * @expectedException \ZfrCors\Exception\InvalidOriginException
342
     */
343
    public function testDoesNotCrashApplicationOnInvalidOriginValue()
344
    {
345
        $request = new HttpRequest();
346
        $request->setUri('https://example.com');
347
        $request->getHeaders()->addHeaderLine('Origin', 'file:');
348
        $this->corsService->isCorsRequest($request);
349
    }
350
}
351