- added missing http_request() function stub
[m6w6/ext-http] / tests / ut_HttpMessage.phpt
1 --TEST--
2 PHPUnit HttpMessage
3 --SKIPIF--
4 <?php
5 include 'skip.inc';
6 checkcls('HttpMessage');
7 skipif(!@include 'PHPUnit2/Framework/TestCase.php', 'need PHPUnit2');
8 ?>
9 --FILE--
10 <?php
11 echo "-TEST\n";
12
13 error_reporting(E_ALL);
14 ini_set('html_errors', 0);
15
16 require_once 'PHPUnit2/Framework/TestSuite.php';
17 require_once 'PHPUnit2/Framework/TestCase.php';
18 require_once 'PHPUnit2/TextUI/ResultPrinter.php';
19
20 class HttpMessageTest extends PHPUnit2_Framework_TestCase
21 {
22 function setUp()
23 {
24 $this->emptyMessage = new HttpMessage;
25 $this->responseMessage = new HttpMessage("HTTP/1.1 302 Found\r\nLocation: /foo\r\nHTTP/1.1 200 Ok\r\nServer: Funky/1.0\r\n\r\nHi there!");
26 $this->requestMessage = new HttpMessage("GET /foo HTTP/1.1\r\nHost: example.com\r\nContent-type: text/plain\r\nContent-length: 10\r\n\r\nHi there!\n");
27 }
28
29 function test___construct()
30 {
31 $this->assertTrue(new HttpMessage instanceof HttpMessage, "new HttpMessage instanceof HttpMessage");
32 }
33
34 function test_getBody()
35 {
36 $this->assertEquals('', $this->emptyMessage->getBody());
37 $this->assertEquals('Hi there!', $this->responseMessage->getBody());
38 $this->assertEquals("Hi there!\n", $this->requestMessage->getBody());
39 }
40
41 function test_setBody()
42 {
43 $this->emptyMessage->setBody('New Body 1');
44 $this->responseMessage->setBody('New Body 2');
45 $this->requestMessage->setBody('New Body 3');
46 $this->assertEquals('New Body 2', $this->responseMessage->getBody());
47 $this->assertEquals('New Body 1', $this->emptyMessage->getBody());
48 $this->assertEquals('New Body 3', $this->requestMessage->getBody());
49 }
50
51 function test_getHeaders()
52 {
53 $this->assertEquals(array(), $this->emptyMessage->getHeaders());
54 $this->assertEquals(array('Server' => 'Funky/1.0'), $this->responseMessage->getHeaders());
55 $this->assertEquals(array('Host' => 'example.com', 'Content-Type' => 'text/plain', 'Content-Length' => '10'), $this->requestMessage->getHeaders());
56 }
57
58 function test_setHeaders()
59 {
60 $this->emptyMessage->setHeaders(array('Foo' => 'Bar'));
61 $this->responseMessage->setHeaders(array());
62 $this->requestMessage->setHeaders(array('Host' => 'www.example.com'));
63 $this->assertEquals(array('Foo' => 'Bar'), $this->emptyMessage->getHeaders());
64 $this->assertEquals(array(), $this->responseMessage->getHeaders());
65 $this->assertEquals(array('Host' => 'www.example.com'), $this->requestMessage->getHeaders());
66 }
67
68 function test_addHeaders()
69 {
70 $this->emptyMessage->addHeaders(array('Foo' => 'Bar'));
71 $this->responseMessage->addHeaders(array('Date' => 'today'));
72 $this->requestMessage->addHeaders(array('Host' => 'www.example.com'));
73 $this->assertEquals(array('Foo' => 'Bar'), $this->emptyMessage->getHeaders());
74 $this->assertEquals(array('Server' => 'Funky/1.0', 'Date' => 'today'), $this->responseMessage->getHeaders());
75 $this->assertEquals(array('Host' => 'www.example.com', 'Content-Type' => 'text/plain', 'Content-Length' => '10'), $this->requestMessage->getHeaders());
76 $this->emptyMessage->addHeaders(array('Foo' => 'Baz'), true);
77 $this->assertEquals(array('Foo' => array('Bar', 'Baz')), $this->emptyMessage->getHeaders());
78 }
79
80 function test_getType()
81 {
82 $this->assertEquals(HTTP_MSG_NONE, $this->emptyMessage->getType());
83 $this->assertEquals(HTTP_MSG_RESPONSE, $this->responseMessage->getType());
84 $this->assertEquals(HTTP_MSG_REQUEST, $this->requestMessage->getType());
85 }
86
87 function test_setType()
88 {
89 $this->emptyMessage->setType(HTTP_MSG_RESPONSE);
90 $this->responseMessage->setType(HTTP_MSG_REQUEST);
91 $this->requestMessage->setType(HTTP_MSG_NONE);
92 $this->assertEquals(HTTP_MSG_RESPONSE, $this->emptyMessage->getType());
93 $this->assertEquals(HTTP_MSG_REQUEST, $this->responseMessage->getType());
94 $this->assertEquals(HTTP_MSG_NONE, $this->requestMessage->getType());
95 }
96
97 function test_getResponseCode()
98 {
99 $this->assertFalse($this->emptyMessage->getResponseCode());
100 $this->assertEquals(200, $this->responseMessage->getResponseCode());
101 $this->assertFalse($this->requestMessage->getResponseCode());
102 }
103
104 function test_setResponseCode()
105 {
106 $this->assertFalse($this->emptyMessage->setResponseCode(301));
107 $this->assertTrue($this->responseMessage->setResponseCode(301));
108 $this->assertFalse($this->requestMessage->setResponseCode(301));
109 $this->assertFalse($this->emptyMessage->getResponseCode());
110 $this->assertEquals(301, $this->responseMessage->getResponseCode());
111 $this->assertFalse($this->requestMessage->getResponseCode());
112 }
113
114 function test_getRequestMethod()
115 {
116 $this->assertFalse($this->emptyMessage->getRequestMethod());
117 $this->assertFalse($this->responseMessage->getRequestMethod());
118 $this->assertEquals('GET', $this->requestMessage->getRequestMethod());
119 }
120
121 function test_setRequestMethod()
122 {
123 $this->assertFalse($this->emptyMessage->setRequestMethod('POST'));
124 $this->assertFalse($this->responseMessage->setRequestMethod('POST'));
125 $this->assertTrue($this->requestMessage->setRequestMethod('POST'));
126 $this->assertFalse($this->emptyMessage->getRequestMethod());
127 $this->assertFalse($this->responseMessage->getRequestMethod());
128 $this->assertEquals('POST', $this->requestMessage->getRequestMethod());
129 }
130
131 function test_getRequestUrl()
132 {
133 $this->assertFalse($this->emptyMessage->getRequestUrl());
134 $this->assertFalse($this->responseMessage->getRequestUrl());
135 $this->assertEquals('/foo', $this->requestMessage->getRequestUrl());
136 }
137
138 function test_setRequestUrl()
139 {
140 $this->assertFalse($this->emptyMessage->setRequestUrl('/bla'));
141 $this->assertFalse($this->responseMessage->setRequestUrl('/bla'));
142 $this->assertTrue($this->requestMessage->setRequestUrl('/bla'));
143 $this->assertFalse($this->emptyMessage->getRequestUrl());
144 $this->assertFalse($this->responseMessage->getRequestUrl());
145 $this->assertEquals('/bla', $this->requestMessage->getRequestUrl());
146 }
147
148 function test_getHttpVersion()
149 {
150 $this->assertEquals('0.0', $this->emptyMessage->getHttpVersion());
151 $this->assertEquals('1.1', $this->responseMessage->getHttpVersion());
152 $this->assertEquals('1.1', $this->requestMessage->getHttpVersion());
153 }
154
155 function test_setHttpVersion()
156 {
157 $this->assertTrue($this->emptyMessage->setHttpVersion(1.0));
158 $this->assertTrue($this->responseMessage->setHttpVersion(1.0));
159 $this->assertTrue($this->requestMessage->setHttpVersion(1.0));
160 $this->assertEquals('1.0', $this->emptyMessage->getHttpVersion());
161 $this->assertEquals('1.0', $this->responseMessage->getHttpVersion());
162 $this->assertEquals('1.0', $this->requestMessage->getHttpVersion());
163 }
164
165 function test_getParentMessage()
166 {
167 $this->assertTrue($this->responseMessage->getParentMessage() instanceOf HttpMessage);
168 try {
169 $this->requestMessage->getParentMessage();
170 $this->assertTrue(false, "\$this->requestMessage->getParentMessage() did not throw an exception");
171 } catch (HttpRuntimeException $ex) {
172 }
173 }
174
175 function test_send()
176 {
177 }
178
179 function test_toString()
180 {
181 $this->assertEquals('', $this->emptyMessage->toString());
182 $this->assertEquals("HTTP/1.1 200 Ok\r\nServer: Funky/1.0\r\n\r\nHi there!\r\n", $this->responseMessage->toString());
183 $this->assertEquals("GET /foo HTTP/1.1\r\nHost: example.com\r\nContent-Type: text/plain\r\nContent-Length: 10\r\n\r\nHi there!\n\r\n", $this->requestMessage->toString());
184 }
185
186 function test_count()
187 {
188 if (5.1 <= (float)PHP_VERSION) {
189 $this->assertEquals(1, count($this->emptyMessage));
190 $this->assertEquals(2, count($this->responseMessage));
191 $this->assertEquals(1, count($this->requestMessage));
192 } else {
193 $this->assertEquals(1, $this->emptyMessage->count());
194 $this->assertEquals(2, $this->responseMessage->count());
195 $this->assertEquals(1, $this->requestMessage->count());
196 }
197 }
198
199 function test_serialize()
200 {
201 }
202
203 function test_unserialize()
204 {
205 }
206
207 function test___toString()
208 {
209 ob_start();
210 echo $this->responseMessage;
211 $this->assertEquals("HTTP/1.1 200 Ok\r\nServer: Funky/1.0\r\n\r\nHi there!\r\n", ob_get_clean());
212 }
213
214 function test_fromString()
215 {
216 $msg = HttpMessage::fromString("HTTP/1.1 200 Ok\r\nServer: Funky/1.0\r\n\r\nHi there!");
217 $this->assertTrue($msg instanceOf HttpMessage);
218 $this->assertEquals(HTTP_MSG_RESPONSE, $msg->getType());
219 $this->assertEquals("Hi there!", $msg->getBody());
220 }
221 }
222
223 $s = new PHPUnit2_Framework_TestSuite('HttpMessageTest');
224 $p = new PHPUnit2_TextUI_ResultPrinter();
225 $p->printResult($s->run(), 0);
226
227 echo "Done\n";
228 ?>
229 --EXPECTF--
230 %sTEST
231
232 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
233
234 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
235
236 Notice: HttpMessage::setResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
237
238 Notice: HttpMessage::setResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
239
240 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
241
242 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
243
244 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
245
246 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
247
248 Notice: HttpMessage::setRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
249
250 Notice: HttpMessage::setRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
251
252 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
253
254 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
255
256 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
257
258 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
259
260 Notice: HttpMessage::setRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
261
262 Notice: HttpMessage::setRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
263
264 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
265
266 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
267
268
269 Time: 0
270
271 OK (24 tests)
272 Done