1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Unit\Middleware; |
5
|
|
|
|
6
|
|
|
use Longman\LaravelLodash\Middlewares\AllowCorsRequests; |
7
|
|
|
use Tests\Unit\TestCase; |
8
|
|
|
|
9
|
|
|
use function config; |
10
|
|
|
use function implode; |
11
|
|
|
|
12
|
|
|
class AllowCorsRequestsTest extends TestCase |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
protected function getEnvironmentSetUp($app) |
15
|
|
|
{ |
16
|
|
|
/** @var \Illuminate\Routing\Router $router */ |
17
|
|
|
$router = $app['router']; |
18
|
|
|
|
19
|
|
|
$router->get('url1', static function () { |
20
|
|
|
return 'ok'; |
21
|
|
|
})->middleware(AllowCorsRequests::class); |
22
|
|
|
|
23
|
|
|
$router->options('url1', static function () { |
24
|
|
|
return 'ok'; |
25
|
|
|
})->middleware(AllowCorsRequests::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @test */ |
29
|
|
|
public function it_should_return_correct_headers_on_options() |
30
|
|
|
{ |
31
|
|
|
$origin = 'domain.com'; |
32
|
|
|
config()->set('lodash.cors.allow_origins', [$origin]); |
33
|
|
|
$headers = [ |
34
|
|
|
'Origin', |
35
|
|
|
'X-Requested-With', |
36
|
|
|
'Content-Type', |
37
|
|
|
'Accept', |
38
|
|
|
'Authorization', |
39
|
|
|
'CustomHeader1234', |
40
|
|
|
]; |
41
|
|
|
config()->set('lodash.cors.allow_headers', $headers); |
42
|
|
|
$methods = [ |
43
|
|
|
'HEAD', |
44
|
|
|
'GET', |
45
|
|
|
'POST', |
46
|
|
|
'OPTIONS', |
47
|
|
|
'PUT', |
48
|
|
|
'PATCH', |
49
|
|
|
'DELETE', |
50
|
|
|
'CustomMethod1234', |
51
|
|
|
]; |
52
|
|
|
config()->set('lodash.cors.allow_methods', $methods); |
53
|
|
|
|
54
|
|
|
$response = $this->call('OPTIONS', 'url1', [], [], [], ['HTTP_Origin' => 'https://' . $origin]); |
|
|
|
|
55
|
|
|
$response->assertSeeText('Allowed'); |
56
|
|
|
$response->assertHeader('Access-Control-Allow-Origin', 'https://' . $origin); |
|
|
|
|
57
|
|
|
$response->assertHeader('Access-Control-Allow-Credentials', 'true'); |
|
|
|
|
58
|
|
|
$response->assertHeader('Access-Control-Allow-Methods', implode(',', $methods)); |
|
|
|
|
59
|
|
|
$response->assertHeader('Access-Control-Allow-Headers', implode(',', $headers)); |
|
|
|
|
60
|
|
|
$response->assertHeader('Access-Control-Max-Age', '1728000'); |
|
|
|
|
61
|
|
|
$response->assertStatus(200); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @test */ |
65
|
|
|
public function it_should_ignore_when_origin_missing() |
66
|
|
|
{ |
67
|
|
|
$response = $this->call('GET', 'url1'); |
68
|
|
|
$response->assertStatus(200); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @test */ |
72
|
|
|
public function it_should_return_error_on_invalid_origin() |
73
|
|
|
{ |
74
|
|
|
config()->set('lodash.cors.allow_origins', ['domain.com']); |
75
|
|
|
|
76
|
|
|
foreach (['GET', 'OPTIONS'] as $method) { |
77
|
|
|
// Invalid domain |
78
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'safsagsafsadsadsa']); |
|
|
|
|
79
|
|
|
$response->assertStatus(400); // Bad request |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Without http:// |
82
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'google.com']); |
|
|
|
|
83
|
|
|
$response->assertStatus(400); // Bad request |
|
|
|
|
84
|
|
|
|
85
|
|
|
// Valid domain, but not whitelisted |
86
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://google.com']); |
|
|
|
|
87
|
|
|
$response->assertStatus(405); // Method not allowed |
|
|
|
|
88
|
|
|
|
89
|
|
|
// Valid similar domain, but not whitelisted |
90
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://mydomain.com']); |
|
|
|
|
91
|
|
|
$response->assertStatus(405); // Method not allowed |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @test */ |
96
|
|
|
public function it_should_return_success_on_valid_origin() |
97
|
|
|
{ |
98
|
|
|
config()->set('lodash.cors.allow_origins', ['domain.com']); |
99
|
|
|
|
100
|
|
|
$origins = config('lodash.cors.allow_origins'); |
101
|
|
|
|
102
|
|
|
// Whitelisted origin |
103
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://' . $origins[0]]); |
|
|
|
|
104
|
|
|
$response->assertStatus(200); |
|
|
|
|
105
|
|
|
|
106
|
|
|
// Whitelisted subdomain origin |
107
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://sub.' . $origins[0]]); |
|
|
|
|
108
|
|
|
$response->assertStatus(200); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @test */ |
112
|
|
|
public function it_should_return_success_on_project_url() |
113
|
|
|
{ |
114
|
|
|
config()->set('lodash.cors.allow_origins', ['domain.com']); |
115
|
|
|
|
116
|
|
|
// Whitelisted origin |
117
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => config('app.url', 'http://localhost')]); |
|
|
|
|
118
|
|
|
$response->assertStatus(200); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|