b572f57273adfb0192dc3536ba65c88ae74737cd
[m6w6/ext-http] / phpunit / MessageTest.php
1 <?php
2
3 class strval {
4 private $str;
5 function __construct($str) {
6 $this->str = $str;
7 }
8 function __toString() {
9 return (string) $this->str;
10 }
11 }
12
13 class message extends http\Message {
14 function __call($m, $args) {
15 if (preg_match("/^test(get|set)(.*)\$/i", $m, $match)) {
16 list(, $getset, $prop) = $match;
17 $prop = strtolower($prop[0]).substr($prop,1);
18 switch(strtolower($getset)) {
19 case "get":
20 return $this->$prop;
21 case "set":
22 $this->$prop = current($args);
23 break;
24 }
25 }
26 }
27 }
28
29 class MessageTest extends PHPUnit_Framework_TestCase
30 {
31 function testProperties() {
32 $test = new message;
33 $this->assertEquals(0, $test->testGetType());
34 $this->assertEquals(null, $test->testGetBody());
35 $this->assertEquals("", $test->testGetRequestMethod());
36 $this->assertEquals("", $test->testGetRequestUrl());
37 $this->assertEquals("", $test->testGetResponseStatus());
38 $this->assertEquals(0, $test->testGetResponseCode());
39 $this->assertEquals("1.1", $test->testGetHttpVersion());
40 $this->assertEquals(array(), $test->testGetHeaders());
41 $this->assertEquals(null, $test->testGetParentMessage());
42 $test->testSetType(http\Message::TYPE_REQUEST);
43 $this->assertEquals(http\Message::TYPE_REQUEST, $test->testGetType());
44 $this->assertEquals(http\Message::TYPE_REQUEST, $test->getType());
45 $body = new http\Message\Body;
46 $test->testSetBody($body);
47 $this->assertEquals($body, $test->testGetBody());
48 $this->assertEquals($body, $test->getBody());
49 $file = fopen(__FILE__,"r");
50 $test->testSetBody($file);
51 $this->assertEquals($file, $test->testGetBody()->getResource());
52 $this->assertEquals($file, $test->getBody()->getResource());
53 $test->testSetBody("data");
54 $this->assertEquals("data", (string) $test->testGetBody());
55 $this->assertEquals("data", (string) $test->getBody());
56 $test->testSetRequestMethod("HEAD");
57 $this->assertEquals("HEAD", $test->testGetRequestMethod());
58 $this->assertEquals("HEAD", $test->getRequestMethod());
59 $test->testSetRequestUrl("/");
60 $this->assertEquals("/", $test->testGetRequestUrl());
61 $this->assertEquals("/", $test->getRequestUrl());
62 $this->assertEquals("HEAD / HTTP/1.1", $test->getInfo());
63 $test->testSetType(http\Message::TYPE_RESPONSE);
64 $test->setResponseStatus("Created");
65 $this->assertEquals("Created", $test->testGetResponseStatus());
66 $this->assertEquals("Created", $test->getResponseStatus());
67 $test->setResponseCode(201);
68 $this->assertEquals(201, $test->testGetResponseCode());
69 $this->assertEquals(201, $test->getResponseCode());
70 $test->testSetResponseStatus("Ok");
71 $this->assertEquals("Ok", $test->testGetResponseStatus());
72 $this->assertEquals("Ok", $test->getResponseStatus());
73 $test->testSetResponseCode(200);
74 $this->assertEquals(200, $test->testGetResponseCode());
75 $this->assertEquals(200, $test->getResponseCode());
76 $test->testSetHttpVersion("1.0");
77 $this->assertEquals("1.0", $test->testGetHttpVersion());
78 $this->assertEquals("1.0", $test->getHttpVersion());
79 $this->assertEquals("HTTP/1.0 200 OK", $test->getInfo());
80 $test->setHttpVersion("1.1");
81 $this->assertEquals("1.1", $test->testGetHttpVersion());
82 $this->assertEquals("1.1", $test->getHttpVersion());
83 $this->assertEquals("HTTP/1.1 200 OK", $test->getInfo());
84 $test->setInfo("HTTP/1.1 201 Created");
85 $this->assertEquals("Created", $test->testGetResponseStatus());
86 $this->assertEquals("Created", $test->getResponseStatus());
87 $this->assertEquals(201, $test->testGetResponseCode());
88 $this->assertEquals(201, $test->getResponseCode());
89 $this->assertEquals("1.1", $test->testGetHttpVersion());
90 $this->assertEquals("1.1", $test->getHttpVersion());
91 $test->testSetHeaders(array("Foo" => "bar"));
92 $this->assertEquals(array("Foo" => "bar"), $test->testGetHeaders());
93 $this->assertEquals(array("Foo" => "bar"), $test->getHeaders());
94 $this->assertEquals("bar", $test->getHeader("foo"));
95 $this->assertEquals(null, $test->getHeader("bar"));
96 $parent = new message;
97 $test->testSetParentMessage($parent);
98 $this->assertEquals($parent, $test->testGetParentMessage());
99 $this->assertEquals($parent, $test->getParentMessage());
100 }
101
102 function testAddBody() {
103 $m = new http\Message;
104 $body = new http\Message\Body;
105 $body->append("foo");
106 $m->addBody($body);
107 $body = new http\Message\Body;
108 $body->append("bar");
109 $m->addBody($body);
110 $this->assertEquals("foobar", (string) $m->getBody());
111 }
112
113 function testAddHeaders() {
114 $m = new http\Message;
115 $m->addHeaders(array("foo"=>"bar","bar"=>"foo"));
116 $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), $m->getHeaders());
117 $m->addHeaders(array("key"=>"val","more"=>"Stuff"));
118 $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo","Key"=>"val","More"=>"Stuff"), $m->getHeaders());
119 }
120
121 function testArrayHeaders() {
122 $m = new http\Message("GET / HTTP/1.1");
123 $m->addHeader("Accept", "text/html");
124 $m->addHeader("Accept", "text/xml;q=0");
125 $m->addHeader("Accept", "text/plain;q=0.5");
126 $this->assertEquals(
127 "GET / HTTP/1.1\r\n".
128 "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n",
129 $m->toString()
130 );
131 }
132
133 function testHeaderValueTypes() {
134 $m = new http\Message("HTTP/1.1 200 Ok");
135 $m->addHeader("Bool", true);
136 $m->addHeader("Int", 123);
137 $m->addHeader("Float", 1.23);
138 $m->addHeader("Array", array(1,2,3));
139 $m->addHeader("Object", new strval("test"));
140 $m->addHeader("Set-Cookie",
141 array(
142 array(
143 "cookies" => array("foo" => "bar"),
144 "expires" => date_create("2012-12-31 22:59:59 GMT")->format(
145 DateTime::COOKIE
146 ),
147 "path" => "/somewhere"
148 )
149 )
150 );
151 $m->addHeader("Set-Cookie", "val=0");
152
153 $this->assertEquals(
154 "HTTP/1.1 200 Ok\r\n".
155 "Bool: true\r\n".
156 "Int: 123\r\n".
157 "Float: 1.23\r\n".
158 "Array: 1, 2, 3\r\n".
159 "Object: test\r\n".
160 "Set-Cookie: foo=bar; path=/somewhere; expires=Mon, 31 Dec 2012 22:59:59 GMT; \r\n".
161 "Set-Cookie: val=0\r\n",
162 $m->toString()
163 );
164 }
165
166 function testDetach() {
167 $m = new http\Message(
168 "HTTP/1.1 200 Ok\r\n".
169 "HTTP/1.1 201 Created\n".
170 "HTTP/1.1 302 Found\r\n"
171 );
172
173 $this->assertCount(3, $m);
174 $d = $m->detach();
175 $this->assertCount(3, $m);
176 $this->assertCount(1, $d);
177
178 $this->assertEquals("HTTP/1.1 302 Found\r\n\r\n", $d->toString(true));
179 }
180
181 function testPrepend() {
182 for ($i=0; $i<9; ++$i) {
183 $a[] = new http\Message("HTTP/1.1 ".($i+200));
184 }
185
186 foreach ($a as $m) {
187 if (isset($p)) $m->prepend($p);
188 $p = $m;
189 }
190
191 $this->assertEquals(
192 "HTTP/1.1 200\r\n\r\n".
193 "HTTP/1.1 201\r\n\r\n".
194 "HTTP/1.1 202\r\n\r\n".
195 "HTTP/1.1 203\r\n\r\n".
196 "HTTP/1.1 204\r\n\r\n".
197 "HTTP/1.1 205\r\n\r\n".
198 "HTTP/1.1 206\r\n\r\n".
199 "HTTP/1.1 207\r\n\r\n".
200 "HTTP/1.1 208\r\n\r\n",
201 $m->toString(true)
202 );
203 }
204
205 function testEmptyUrlWarning() {
206 $m = new http\Message;
207 $this->setExpectedException("PHPUnit_Framework_Error_Notice");
208 $m->setRequestUrl("/foo");
209 $m->setType(http\Message::TYPE_REQUEST);
210 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
211 $m->setRequestUrl("");
212 }
213
214 function testEmptyParentMessage() {
215 $m = new http\Message;
216 try {
217 $m->getParentMessage();
218 $this->assertFalse("this code should not be reached");
219 } catch (http\Exception $e) {
220 $this->assertEquals("HttpMessage does not have a parent message", $e->getMessage());
221 }
222 }
223
224 function testPrependError() {
225 $m = new http\Message("HTTP/1.1 200\r\nHTTP/1.1 201");
226 try {
227 $m->prepend($m->getParentMessage());
228 $this->assertFalse("this code should not be reached");
229 } catch (http\Exception $e) {
230 $this->assertEquals("Cannot prepend a message located within the same message chain", $e->getMessage());
231 }
232 }
233
234 function testToCallback() {
235 $m = new http\Message("HTTP/1.1 200 Ok");
236 $m->addHeader("Content-Type", "text/plain");
237 $m->getBody()->append("this\nis\nthe\ntext");
238
239 $d = new http\Encoding\Stream\Deflate;
240 $s = "";
241 $m->toCallback(function ($m, $data) use ($d, &$s) {
242 $s.=$d->update($data);
243 });
244 $s.=$d->finish();
245 $this->assertEquals($m->toString(), http\Encoding\Stream\Inflate::decode($s));
246 }
247
248 function testToStream() {
249 $m = new http\Message("HTTP/1.1 200 Ok");
250 $m->addHeader("Content-Type", "text/plain");
251 $m->getBody()->append("this\nis\nthe\ntext");
252
253 $f = tmpfile();
254 $m->toStream($f);
255 rewind($f);
256 $this->assertEquals((string) $m, stream_get_contents($f));
257 fclose($f);
258 }
259
260 function testBoundary() {
261 $p = new http\Message;
262 $p->addHeader("Content-Type", "text/plain");
263 $p->getBody()->append("data");
264
265 $m = new http\Message("HTTP/1.1 200");
266 $m->getBody()->addPart($p);
267 $this->assertStringMatchesFormat(
268 "HTTP/1.1 200\r\n".
269 "Content-Length: %d\r\n".
270 "Content-Type: multipart/form-data; boundary=\"%x.%x\"\r\n".
271 "\r\n".
272 "--%x.%x\r\n".
273 "Content-Type: text/plain\r\n".
274 "Content-Length: 4\r\n".
275 "\r\n".
276 "data\r\n".
277 "--%x.%x--\r\n".
278 "",
279 str_replace("\r", "", $m->toString()) // phpunit replaces \r\n with \n
280 );
281 }
282 }
283