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 |
|
|
|
|
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]); |
|
|
|
|
42
|
|
|
$response->assertSeeText('Allowed'); |
43
|
|
|
$response->assertHeader('Access-Control-Allow-Origin', 'https://' . $origin); |
|
|
|
|
44
|
|
|
$response->assertHeader('Access-Control-Allow-Credentials', 'true'); |
|
|
|
|
45
|
|
|
$response->assertHeader('Access-Control-Allow-Methods', implode(',', $methods)); |
|
|
|
|
46
|
|
|
$response->assertHeader('Access-Control-Allow-Headers', implode(',', $headers)); |
|
|
|
|
47
|
|
|
$response->assertHeader('Access-Control-Max-Age', '1728000'); |
|
|
|
|
48
|
|
|
$response->assertStatus(200); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
|
public function it_should_ignore_when_origin_missing() |
53
|
|
|
{ |
54
|
|
|
$response = $this->call('GET', 'url1'); |
55
|
|
|
$response->assertStatus(200); |
|
|
|
|
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']); |
|
|
|
|
66
|
|
|
$response->assertStatus(400); // Bad request |
|
|
|
|
67
|
|
|
|
68
|
|
|
// Without http:// |
69
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'google.com']); |
|
|
|
|
70
|
|
|
$response->assertStatus(400); // Bad request |
|
|
|
|
71
|
|
|
|
72
|
|
|
// Valid domain, but not whitelisted |
73
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://google.com']); |
|
|
|
|
74
|
|
|
$response->assertStatus(405); // Method not allowed |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Valid similar domain, but not whitelisted |
77
|
|
|
$response = $this->call($method, 'url1', [], [], [], ['HTTP_Origin' => 'http://mydomain.com']); |
|
|
|
|
78
|
|
|
$response->assertStatus(405); // Method not allowed |
|
|
|
|
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]]); |
|
|
|
|
91
|
|
|
$response->assertStatus(200); |
|
|
|
|
92
|
|
|
|
93
|
|
|
// Whitelisted subdomain origin |
94
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['HTTP_Origin' => 'http://sub.' . $origins[0]]); |
|
|
|
|
95
|
|
|
$response->assertStatus(200); |
|
|
|
|
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')]); |
|
|
|
|
105
|
|
|
$response->assertStatus(200); |
|
|
|
|
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
|
|
|
|