1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Unit; |
5
|
|
|
|
6
|
|
|
use Blade; |
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Longman\LaravelLodash\Middlewares\AllowCorsRequests; |
9
|
|
|
use Orchestra\Testbench\Http\Kernel; |
10
|
|
|
|
11
|
|
|
class MiddlewaresTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected function getEnvironmentSetUp($app) |
15
|
|
|
{ |
16
|
|
|
/** @var \Illuminate\Routing\Router $router */ |
17
|
|
|
$router = $app['router']; |
18
|
|
|
|
19
|
|
|
/** @var \Orchestra\Testbench\Http\Kernel $kernel */ |
20
|
|
|
$kernel = $app[Kernel::class]; |
21
|
|
|
$kernel->pushMiddleware(AllowCorsRequests::class); |
22
|
|
|
|
23
|
|
|
$router->get('url1', function () { |
24
|
|
|
return 'ok'; |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
$router->get('{all?}', function () { |
28
|
|
|
return 'all'; |
29
|
|
|
})->where('all', '.*'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @test */ |
33
|
|
|
public function it_should_return_error_on_invalid_origin() |
34
|
|
|
{ |
35
|
|
|
$this->markTestSkipped(); |
36
|
|
|
$response = $this->call('GET', 'url1', [], ['Origin' => 'safsagsafsadsadsa']); |
37
|
|
|
$response->dump(); |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
// Invalid domain |
41
|
|
|
$response = $this->json('OPTIONS', 'url1', [], ['Origin' => 'safsagsafsadsadsa']); |
42
|
|
|
$response->assertStatus(400); // Bad request |
43
|
|
|
|
44
|
|
|
// Without http:// |
45
|
|
|
$response = $this->json('OPTIONS', 'url1', [], ['Origin' => 'google.com']); |
46
|
|
|
$response->assertStatus(400); // Bad request |
47
|
|
|
|
48
|
|
|
// Valid domain, but not whitelisted |
49
|
|
|
$response = $this->json('OPTIONS', 'url1', [], ['Origin' => 'http://google.com']); |
50
|
|
|
$response->assertStatus(405); // Method not allowed |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @test */ |
54
|
|
|
public function it_should_return_success_on_request() |
55
|
|
|
{ |
56
|
|
|
$this->markTestSkipped(); |
57
|
|
|
$origins = config('lodash.cors.allow_origins'); |
58
|
|
|
if (empty($origins[0])) { |
59
|
|
|
$this->markTestSkipped('Allowed origins are not specified in config. Skip test.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$origin = substr($origins[0], 4) !== 'http' ? 'http://' . $origins[0] : $origins[0]; |
63
|
|
|
|
64
|
|
|
// Whitelisted origin |
65
|
|
|
$response = $this->json('OPTIONS', 'url1', [], ['Origin' => $origin]); |
66
|
|
|
$response->assertStatus(200); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|