1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\Slim\Middleware; |
4
|
|
|
|
5
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Slim\Http\Headers; |
8
|
|
|
use Slim\Http\Request; |
9
|
|
|
use Slim\Http\Response; |
10
|
|
|
use Slim\Http\Stream; |
11
|
|
|
use Slim\Http\Uri; |
12
|
|
|
use Slim\Route; |
13
|
|
|
use SubjectivePHP\Slim\Middleware\OptionsMiddleware; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @coversDefaultClass \SubjectivePHP\Slim\Middleware\OptionsMiddleware |
17
|
|
|
* @covers ::__construct |
18
|
|
|
*/ |
19
|
|
|
final class OptionsMiddlewareTest extends TestCase implements RequestMethodInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
const ALLOW_HEADERS = ['Authorization, Content-Type']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const ALLOW_ORIGIN = '*'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var OptionsMiddleware |
33
|
|
|
*/ |
34
|
|
|
private $middleware; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var callable |
38
|
|
|
*/ |
39
|
|
|
private $next; |
40
|
|
|
|
41
|
|
|
public function setUp() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->middleware = new OptionsMiddleware(self::ALLOW_ORIGIN, self::ALLOW_HEADERS); |
44
|
|
|
$this->next = function (Request $request, Response $response) : Response { |
45
|
|
|
return $response; |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @test |
51
|
|
|
* @covers ::__invoke |
52
|
|
|
*/ |
53
|
|
|
public function invokeMethodNotOptions() |
54
|
|
|
{ |
55
|
|
|
$request = $this->getRequest(self::METHOD_GET); |
56
|
|
|
$response = new Response(); |
57
|
|
|
$this->assertSame($response, $this->middleware->__invoke($request, $response, $this->next)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
* @covers ::__invoke |
63
|
|
|
* @expectedException \Slim\Exception\NotFoundException |
64
|
|
|
*/ |
65
|
|
|
public function invokeMethodIsOptionsButNoRoute() |
66
|
|
|
{ |
67
|
|
|
$request = $this->getRequest(); |
68
|
|
|
$this->middleware->__invoke($request, new Response(), $this->next); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @covers ::__invoke |
74
|
|
|
*/ |
75
|
|
|
public function invoke() |
76
|
|
|
{ |
77
|
|
|
$methods = [self::METHOD_GET, self::METHOD_POST]; |
78
|
|
|
$route = new Route($methods, '/foo', $this->next); |
79
|
|
|
$request = $this->getRequest(self::METHOD_OPTIONS, $route); |
80
|
|
|
$response = $this->middleware->__invoke($request, new Response(), $this->next); |
81
|
|
|
$this->assertSame( |
82
|
|
|
[ |
83
|
|
|
'Access-Control-Allow-Origin' => [ |
84
|
|
|
self::ALLOW_ORIGIN, |
85
|
|
|
], |
86
|
|
|
'Access-Control-Allow-Headers' => [ |
87
|
|
|
implode(',', self::ALLOW_HEADERS), |
88
|
|
|
], |
89
|
|
|
'Access-Control-Allow-Methods' => [ |
90
|
|
|
implode(',', [self::METHOD_GET, self::METHOD_POST, self::METHOD_OPTIONS]), |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
$response->getHeaders() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getRequest(string $method = self::METHOD_OPTIONS, Route $route = null) : Request |
98
|
|
|
{ |
99
|
|
|
$uri = Uri::createFromString('http://localhost'); |
100
|
|
|
$headers = new Headers(); |
101
|
|
|
$resource = fopen('php://memory', 'r'); |
102
|
|
|
$body = new Stream($resource); |
103
|
|
|
$request = new Request($method, $uri, $headers, [], [], $body); |
104
|
|
|
return $request->withAttribute('route', $route); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|