1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Framework\Http; |
13
|
|
|
|
14
|
|
|
use Spiral\Http\Diactoros\StreamFactory; |
15
|
|
|
use Spiral\Tests\Framework\HttpTest; |
16
|
|
|
|
17
|
|
|
class ControllerTest extends HttpTest |
18
|
|
|
{ |
19
|
|
|
public function testIndexAction(): void |
20
|
|
|
{ |
21
|
|
|
$this->assertSame('Hello, Dave.', (string)$this->get('/index')->getBody()); |
22
|
|
|
$this->assertSame('Hello, Antony.', (string)$this->get('/index/Antony')->getBody()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testRouteJson(): void |
26
|
|
|
{ |
27
|
|
|
$this->assertSame('{"action":"route","name":"Dave"}', (string)$this->get('/route')->getBody()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test404(): void |
31
|
|
|
{ |
32
|
|
|
$this->assertSame('404', (string)$this->get('/undefined')->getStatusCode()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testPayloadAction(): void |
36
|
|
|
{ |
37
|
|
|
$factory = new StreamFactory(); |
38
|
|
|
|
39
|
|
|
$response = $this->http->handle($this->request('/payload', 'POST', [], [ |
40
|
|
|
'Content-Type' => 'application/json;charset=UTF-8;' |
41
|
|
|
], [])->withBody($factory->createStream('{"a":"b"}'))); |
42
|
|
|
|
43
|
|
|
$this->assertSame('{"a":"b"}', (string)$response->getBody()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testPayloadWithCustomJsonHeader(): void |
47
|
|
|
{ |
48
|
|
|
$factory = new StreamFactory(); |
49
|
|
|
|
50
|
|
|
$response = $this->http->handle($this->request('/payload', 'POST', [], [ |
51
|
|
|
'Content-Type' => 'application/vnd.api+json;charset=UTF-8;' |
52
|
|
|
], [])->withBody($factory->createStream('{"a":"b"}'))); |
53
|
|
|
|
54
|
|
|
$this->assertSame('{"a":"b"}', (string)$response->getBody()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testPayloadActionBad(): void |
58
|
|
|
{ |
59
|
|
|
$factory = new StreamFactory(); |
60
|
|
|
|
61
|
|
|
$response = $this->http->handle($this->request('/payload', 'POST', [], [ |
62
|
|
|
'Content-Type' => 'application/json;charset=UTF-8;' |
63
|
|
|
], [])->withBody($factory->createStream('{"a":"b"'))); |
64
|
|
|
|
65
|
|
|
$this->assertSame(400, $response->getStatusCode()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test500(): void |
69
|
|
|
{ |
70
|
|
|
$this->assertSame('500', (string)$this->get('/error')->getStatusCode()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|