1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Entities\JsonResponse; |
8
|
|
|
use Gacela\Router\Entities\Request; |
9
|
|
|
use Gacela\Router\Entities\Response; |
10
|
|
|
use Gacela\Router\Router; |
11
|
|
|
use Gacela\Router\Routes; |
12
|
|
|
use GacelaTest\Feature\HeaderTestCase; |
13
|
|
|
|
14
|
|
|
final class RouterResponseTest extends HeaderTestCase |
15
|
|
|
{ |
16
|
|
|
public function test_string_response(): void |
17
|
|
|
{ |
18
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
19
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
20
|
|
|
|
21
|
|
|
Router::configure(static function (Routes $routes): void { |
22
|
|
|
$routes->get('uri', static fn () => new Response('body')); |
|
|
|
|
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
$this->expectOutputString('body'); |
26
|
|
|
|
27
|
|
|
self::assertSame([], $this->headers()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_json_response(): void |
31
|
|
|
{ |
32
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
33
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
34
|
|
|
|
35
|
|
|
Router::configure(static function (Routes $routes): void { |
36
|
|
|
$routes->get('uri', static fn () => new JsonResponse([ |
|
|
|
|
37
|
|
|
'key' => 'value', |
38
|
|
|
])); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$this->expectOutputString('{"key":"value"}'); |
42
|
|
|
|
43
|
|
|
self::assertSame([ |
44
|
|
|
[ |
45
|
|
|
'header' => 'Content-Type: application/json', |
46
|
|
|
'replace' => true, |
47
|
|
|
'response_code' => 0, |
48
|
|
|
], |
49
|
|
|
], $this->headers()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_response_headers(): void |
53
|
|
|
{ |
54
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
55
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
56
|
|
|
|
57
|
|
|
Router::configure(static function (Routes $routes): void { |
58
|
|
|
$routes->get('uri', static fn () => new Response('{"key":"value"}', [ |
|
|
|
|
59
|
|
|
'Access-Control-Allow-Origin: *', |
60
|
|
|
'Content-Type: application/json', |
61
|
|
|
])); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->expectOutputString('{"key":"value"}'); |
65
|
|
|
|
66
|
|
|
self::assertSame([ |
67
|
|
|
[ |
68
|
|
|
'header' => 'Access-Control-Allow-Origin: *', |
69
|
|
|
'replace' => true, |
70
|
|
|
'response_code' => 0, |
71
|
|
|
], |
72
|
|
|
[ |
73
|
|
|
'header' => 'Content-Type: application/json', |
74
|
|
|
'replace' => true, |
75
|
|
|
'response_code' => 0, |
76
|
|
|
], |
77
|
|
|
], $this->headers()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function test_json_response_headers(): void |
81
|
|
|
{ |
82
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
83
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
84
|
|
|
|
85
|
|
|
Router::configure(static function (Routes $routes): void { |
86
|
|
|
$routes->get('uri', static fn () => new JsonResponse(['key' => 'value'], [ |
|
|
|
|
87
|
|
|
'Access-Control-Allow-Origin: *', |
88
|
|
|
'Content-Type: application/json', |
89
|
|
|
])); |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$this->expectOutputString('{"key":"value"}'); |
93
|
|
|
|
94
|
|
|
self::assertSame([ |
95
|
|
|
[ |
96
|
|
|
'header' => 'Access-Control-Allow-Origin: *', |
97
|
|
|
'replace' => true, |
98
|
|
|
'response_code' => 0, |
99
|
|
|
], |
100
|
|
|
[ |
101
|
|
|
'header' => 'Content-Type: application/json', |
102
|
|
|
'replace' => true, |
103
|
|
|
'response_code' => 0, |
104
|
|
|
], |
105
|
|
|
], $this->headers()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|