-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathResponseTest.php
More file actions
executable file
·174 lines (125 loc) · 4.66 KB
/
ResponseTest.php
File metadata and controls
executable file
·174 lines (125 loc) · 4.66 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Utopia\Http;
use PHPUnit\Framework\TestCase;
use Utopia\Http\Adapter\FPM\Response;
class FPMResponseTest extends TestCase
{
protected ?Response $response;
public function setUp(): void
{
$this->response = new Response();
}
public function tearDown(): void
{
$this->response = null;
}
public function testCanSetContentType()
{
$contentType = $this->response->setContentType(Response::CONTENT_TYPE_HTML, Response::CHARSET_UTF8);
// Assertions
$this->assertInstanceOf('Utopia\Http\Response', $contentType);
}
public function testCanSetStatus()
{
$status = $this->response->setStatusCode(Response::STATUS_CODE_OK);
// Assertions
$this->assertInstanceOf('Utopia\Http\Response', $status);
try {
$this->response->setStatusCode(0); // Unknown status code
} catch (\Exception $e) {
$this->assertInstanceOf('\Exception', $e);
return;
}
$this->fail('Expected exception');
}
public function testCanGetStatus()
{
$status = $this->response->setStatusCode(Response::STATUS_CODE_OK);
// Assertions
$this->assertInstanceOf('Utopia\Http\Response', $status);
$this->assertEquals(Response::STATUS_CODE_OK, $this->response->getStatusCode());
}
public function testCanAddHeader()
{
$result = $this->response->addHeader('key', 'value');
$this->assertEquals($this->response, $result);
}
public function testCanAddCookie()
{
$result = $this->response->addCookie('name', 'value');
$this->assertEquals($this->response, $result);
//test cookie case insensitive
$result = $this->response->addCookie('cookieName', 'cookieValue');
$result->getCookies()['cookiename']['name'] = 'cookiename';
$result->getCookies()['cookiename']['value'] = 'cookieValue';
}
public function testCanSend()
{
ob_start(); //Start of build
@$this->response
->addHeader('key', 'value')
->addCookie('name', 'value')
->send('body'); //FIXME we have a problem with header printing
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('body', $html);
}
public function testCanSendRedirect()
{
ob_start(); //Start of build
@$this->response->redirect('http://www.example.com');
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('', $html);
ob_start(); //Start of build
@$this->response->redirect('http://www.example.com', 300);
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('', $html);
}
public function testCanSendText()
{
ob_start(); //Start of build
@$this->response->text('HELLO WORLD');
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('HELLO WORLD', $html);
$this->assertEquals('text/plain; charset=UTF-8', $this->response->getContentType());
}
public function testCanSendHtml()
{
ob_start(); //Start of build
@$this->response->html('<html></html>');
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('<html></html>', $html);
$this->assertEquals('text/html; charset=UTF-8', $this->response->getContentType());
}
public function testCanSendJson()
{
ob_start(); //Start of build
@$this->response->json(['key' => 'value']);
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('{"key":"value"}', $html);
$this->assertEquals('application/json; charset=UTF-8', $this->response->getContentType());
}
public function testCanSendJsonp()
{
ob_start(); //Start of build
@$this->response->jsonp('test', ['key' => 'value']);
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('parent.test({"key":"value"});', $html);
$this->assertEquals('text/javascript; charset=UTF-8', $this->response->getContentType());
}
public function testCanSendIframe()
{
ob_start(); //Start of build
@$this->response->iframe('test', ['key' => 'value']);
$html = ob_get_contents();
ob_end_clean(); //End of build
$this->assertEquals('<script type="text/javascript">window.parent.test({"key":"value"});</script>', $html);
$this->assertEquals('text/html; charset=UTF-8', $this->response->getContentType());
}
}