|
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\Http; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Files\Files; |
|
16
|
|
|
use Spiral\Http\Config\HttpConfig; |
|
17
|
|
|
use Spiral\Http\Exception\ResponseException; |
|
18
|
|
|
use Spiral\Http\ResponseWrapper; |
|
19
|
|
|
use Spiral\Tests\Http\Diactoros\ResponseFactory; |
|
20
|
|
|
use Spiral\Tests\Http\Diactoros\StreamFactory; |
|
21
|
|
|
use Laminas\Diactoros\Stream; |
|
22
|
|
|
|
|
23
|
|
|
class ResponsesTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testRedirect(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$response = $this->getWrapper()->redirect('google.com'); |
|
28
|
|
|
$this->assertSame('google.com', $response->getHeaderLine('Location')); |
|
29
|
|
|
$this->assertSame(302, $response->getStatusCode()); |
|
30
|
|
|
|
|
31
|
|
|
$response = $this->getWrapper()->redirect('google.com', 301); |
|
32
|
|
|
$this->assertSame('google.com', $response->getHeaderLine('Location')); |
|
33
|
|
|
$this->assertSame(301, $response->getStatusCode()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testRedirectException(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->expectException(ResponseException::class); |
|
39
|
|
|
$this->getWrapper()->redirect(true); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testJson(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$response = $this->getWrapper()->json([ |
|
45
|
|
|
'status' => 300, |
|
46
|
|
|
'message' => 'hi' |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame('{"status":300,"message":"hi"}', (string)$response->getBody()); |
|
50
|
|
|
$this->assertSame(300, $response->getStatusCode()); |
|
51
|
|
|
$this->assertSame('application/json', $response->getHeaderLine('Content-Type')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testHtml(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$response = $this->getWrapper()->html('hello world'); |
|
57
|
|
|
$this->assertSame('hello world', (string)$response->getBody()); |
|
58
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
|
59
|
|
|
$ff = $response->getHeader('Content-Type'); |
|
|
|
|
|
|
60
|
|
|
$this->assertSame(['text/html; charset=utf-8'], $response->getHeader('Content-Type')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testAttachment(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$response = $this->getWrapper()->attachment(__FILE__); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
|
68
|
|
|
$this->assertStringEqualsFile(__FILE__, (string)$response->getBody()); |
|
69
|
|
|
$this->assertSame(filesize(__FILE__), $response->getBody()->getSize()); |
|
70
|
|
|
$this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testAttachmentResource(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$response = $this->getWrapper()->attachment(fopen(__FILE__, 'r'), 'file.php'); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
|
78
|
|
|
$this->assertStringEqualsFile(__FILE__, (string)$response->getBody()); |
|
79
|
|
|
$this->assertSame(filesize(__FILE__), $response->getBody()->getSize()); |
|
80
|
|
|
$this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testAttachmentStream(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$response = $this->getWrapper()->attachment(new Stream(fopen(__FILE__, 'r'), 'r'), 'file.php'); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
|
88
|
|
|
$this->assertStringEqualsFile(__FILE__, (string)$response->getBody()); |
|
89
|
|
|
$this->assertSame(filesize(__FILE__), $response->getBody()->getSize()); |
|
90
|
|
|
$this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testAttachmentStreamable(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$response = $this->getWrapper()->attachment( |
|
96
|
|
|
new Streamable(new Stream(fopen(__FILE__, 'r'), 'r')), |
|
|
|
|
|
|
97
|
|
|
'file.php' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
|
101
|
|
|
$this->assertStringEqualsFile(__FILE__, (string)$response->getBody()); |
|
102
|
|
|
$this->assertSame(filesize(__FILE__), $response->getBody()->getSize()); |
|
103
|
|
|
$this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testCreate(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$response = $this->getWrapper()->create(400); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testAttachmentStreamNoName(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->expectException(ResponseException::class); |
|
116
|
|
|
$this->getWrapper()->attachment(new Stream(fopen(__FILE__, 'rb'), 'r')); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testAttachmentException(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$this->expectException(ResponseException::class); |
|
122
|
|
|
$this->getWrapper()->attachment('invalid'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function getWrapper(): ResponseWrapper |
|
126
|
|
|
{ |
|
127
|
|
|
return new ResponseWrapper( |
|
128
|
|
|
new ResponseFactory(new HttpConfig(['headers' => []])), |
|
129
|
|
|
new StreamFactory(), |
|
130
|
|
|
new Files() |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|