-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathResponseTest.php
More file actions
113 lines (92 loc) · 3.25 KB
/
ResponseTest.php
File metadata and controls
113 lines (92 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace think\tests;
use PHPUnit\Framework\TestCase;
use Mockery as m;
use think\Cookie;
use think\response\Html;
use think\response\Json;
class ResponseTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testHtmlResponseCreation()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test content');
$this->assertInstanceOf(Html::class, $response);
$this->assertEquals('test content', $response->getData());
}
public function testJsonResponseCreation()
{
$cookie = m::mock(Cookie::class);
$data = ['key' => 'value'];
$response = new Json($cookie, $data);
$this->assertInstanceOf(Json::class, $response);
$this->assertEquals($data, $response->getData());
}
public function testResponseCode()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test', 200);
$this->assertEquals(200, $response->getCode());
$response->code(404);
$this->assertEquals(404, $response->getCode());
}
public function testResponseHeaders()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test');
$response->header(['Content-Type' => 'text/html']);
$headers = $response->getHeader();
$this->assertEquals('text/html', $headers['Content-Type']);
$response->header(['X-Custom' => 'value']);
$headers = $response->getHeader();
$this->assertEquals('value', $headers['X-Custom']);
}
public function testResponseData()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'initial');
$this->assertEquals('initial', $response->getData());
$response->data('updated');
$this->assertEquals('updated', $response->getData());
}
public function testResponseStatusMethods()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, '', 200);
$this->assertEquals(200, $response->getCode());
$response->code(404);
$this->assertEquals(404, $response->getCode());
$response->code(500);
$this->assertEquals(500, $response->getCode());
}
public function testContentTypeMethod()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test');
$response->contentType('application/json', 'utf-8');
$headers = $response->getHeader();
$this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
}
public function testLastModified()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test');
$time = '2025-01-01 10:00:00';
$response->lastModified($time);
$headers = $response->getHeader();
$this->assertArrayHasKey('Last-Modified', $headers);
}
public function testETag()
{
$cookie = m::mock(Cookie::class);
$response = new Html($cookie, 'test');
$etag = 'test-etag';
$response->eTag($etag);
$headers = $response->getHeader();
$this->assertEquals('test-etag', $headers['ETag']);
}
}