AllowCorsRequestsTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A it_should_return_correct_headers_on_options() 0 34 1
A it_should_ignore_when_origin_missing() 0 5 1
A it_should_return_error_on_invalid_origin() 0 22 2
A it_should_return_success_on_valid_origin() 0 14 1
A it_should_return_success_on_project_url() 0 8 1
A getEnvironmentSetUp() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Unit\Middleware;
6
7
use Longman\LaravelLodash\Middlewares\AllowCorsRequests;
8
use Tests\Unit\TestCase;
9
10
use function config;
11
use function implode;
12
13
class AllowCorsRequestsTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
14
{
15
    /** @test */
16
    public function it_should_return_correct_headers_on_options()
17
    {
18
        $origin = 'domain.com';
19
        config()->set('lodash.cors.allow_origins', [$origin]);
20
        $headers = [
21
            'Origin',
22
            'X-Requested-With',
23
            'Content-Type',
24
            'Accept',
25
            'Authorization',
26
            'CustomHeader1234',
27
        ];
28
        config()->set('lodash.cors.allow_headers', $headers);
29
        $methods = [
30
            'HEAD',
31
            'GET',
32
            'POST',
33
            'OPTIONS',
34
            'PUT',
35
            'PATCH',
36
            'DELETE',
37
            'CustomMethod1234',
38
        ];
39
        config()->set('lodash.cors.allow_methods', $methods);
40
41
        $response = $this->call('OPTIONS', 'url1', [], [], [], ['HTTP_Origin' => 'https://' . $origin]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'https://' . $origin) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
        $response->assertSeeText('Allowed');
43
        $response->assertHeader('Access-Control-Allow-Origin', 'https://' . $origin);
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
        $response->assertHeader('Access-Control-Allow-Credentials', 'true');
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
45
        $response->assertHeader('Access-Control-Allow-Methods', implode(',', $methods));
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
        $response->assertHeader('Access-Control-Allow-Headers', implode(',', $headers));
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
        $response->assertHeader('Access-Control-Max-Age', '1728000');
0 ignored issues
show
Bug introduced by
The method assertHeader() does not exist on Illuminate\Http\Response. Did you maybe mean header()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
    }
50
51
    /** @test */
52
    public function it_should_ignore_when_origin_missing()
53
    {
54
        $response = $this->call('GET', 'url1');
55
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
    }
57
58
    /** @test */
59
    public function it_should_return_error_on_invalid_origin()
60
    {
61
        config()->set('lodash.cors.allow_origins', ['domain.com']);
62
63
        foreach (['GET', 'OPTIONS'] as $method) {
64
            // Invalid domain
65
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'safsagsafsadsadsa']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'safsagsafsadsadsa') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
            $response->assertStatus(400); // Bad request
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
68
            // Without http://
69
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'google.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'google.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            $response->assertStatus(400); // Bad request
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71
72
            // Valid domain, but not whitelisted
73
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://google.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://google.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            $response->assertStatus(405); // Method not allowed
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75
76
            // Valid similar domain, but not whitelisted
77
            $response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://mydomain.com']);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://mydomain.com') is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
            $response->assertStatus(405); // Method not allowed
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
79
        }
80
    }
81
82
    /** @test */
83
    public function it_should_return_success_on_valid_origin()
84
    {
85
        config()->set('lodash.cors.allow_origins', ['domain.com']);
86
87
        $origins = config('lodash.cors.allow_origins');
88
89
        // Whitelisted origin
90
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://' . $origins[0]]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => 'http://' . $origins[0]) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
92
93
        // Whitelisted subdomain origin
94
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://sub.' . $origins[0]]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => '...://sub.' . $origins[0]) is of type array<string,string,{"HTTP_Origin":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
96
    }
97
98
    /** @test */
99
    public function it_should_return_success_on_project_url()
100
    {
101
        config()->set('lodash.cors.allow_origins', ['domain.com']);
102
103
        // Whitelisted origin
104
        $response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => config('app.url', 'http://localhost')]);
0 ignored issues
show
Documentation introduced by
array('HTTP_Origin' => \...', 'http://localhost')) is of type array<string,*,{"HTTP_Origin":"*"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
        $response->assertStatus(200);
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Illuminate\Http\Response. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
    }
107
108
    protected function getEnvironmentSetUp($app)
109
    {
110
        /** @var \Illuminate\Routing\Router $router */
111
        $router = $app['router'];
112
113
        $router->get('url1', static function () {
114
            return 'ok';
115
        })->middleware(AllowCorsRequests::class);
116
117
        $router->options('url1', static function () {
118
            return 'ok';
119
        })->middleware(AllowCorsRequests::class);
120
    }
121
}
122