6dd5b707bbf665e75baddbb33d00bfbaf5d728fe
[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 $test->testSetRequestMethod("HEAD");
50 $this->assertEquals("HEAD", $test->testGetRequestMethod());
51 $this->assertEquals("HEAD", $test->getRequestMethod());
52 $test->testSetRequestUrl("/");
53 $this->assertEquals("/", $test->testGetRequestUrl());
54 $this->assertEquals("/", $test->getRequestUrl());
55 $this->assertEquals("HEAD / HTTP/1.1", $test->getInfo());
56 $test->testSetType(http\Message::TYPE_RESPONSE);
57 $test->setResponseStatus("Created");
58 $this->assertEquals("Created", $test->testGetResponseStatus());
59 $this->assertEquals("Created", $test->getResponseStatus());
60 $test->setResponseCode(201);
61 $this->assertEquals(201, $test->testGetResponseCode());
62 $this->assertEquals(201, $test->getResponseCode());
63 $test->testSetResponseStatus("Ok");
64 $this->assertEquals("Ok", $test->testGetResponseStatus());
65 $this->assertEquals("Ok", $test->getResponseStatus());
66 $test->testSetResponseCode(200);
67 $this->assertEquals(200, $test->testGetResponseCode());
68 $this->assertEquals(200, $test->getResponseCode());
69 $test->testSetHttpVersion("1.0");
70 $this->assertEquals("1.0", $test->testGetHttpVersion());
71 $this->assertEquals("1.0", $test->getHttpVersion());
72 $this->assertEquals("HTTP/1.0 200 OK", $test->getInfo());
73 $test->setHttpVersion("1.1");
74 $this->assertEquals("1.1", $test->testGetHttpVersion());
75 $this->assertEquals("1.1", $test->getHttpVersion());
76 $this->assertEquals("HTTP/1.1 200 OK", $test->getInfo());
77 $test->setInfo("HTTP/1.1 201 Created");
78 $this->assertEquals("Created", $test->testGetResponseStatus());
79 $this->assertEquals("Created", $test->getResponseStatus());
80 $this->assertEquals(201, $test->testGetResponseCode());
81 $this->assertEquals(201, $test->getResponseCode());
82 $this->assertEquals("1.1", $test->testGetHttpVersion());
83 $this->assertEquals("1.1", $test->getHttpVersion());
84 $test->testSetHeaders(array("Foo" => "bar"));
85 $this->assertEquals(array("Foo" => "bar"), $test->testGetHeaders());
86 $this->assertEquals(array("Foo" => "bar"), $test->getHeaders());
87 $this->assertEquals("bar", $test->getHeader("foo"));
88 $this->assertEquals(null, $test->getHeader("bar"));
89 $parent = new message;
90 $test->testSetParentMessage($parent);
91 $this->assertEquals($parent, $test->testGetParentMessage());
92 $this->assertEquals($parent, $test->getParentMessage());
93 }
94
95 function testAddBody() {
96 $m = new http\Message;
97 $body = new http\Message\Body;
98 $body->append("foo");
99 $m->addBody($body);
100 $body = new http\Message\Body;
101 $body->append("bar");
102 $m->addBody($body);
103 $this->assertEquals("foobar", (string) $m->getBody());
104 }
105
106 function testAddHeaders() {
107 $m = new http\Message;
108 $m->addHeaders(array("foo"=>"bar","bar"=>"foo"));
109 $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), $m->getHeaders());
110 $m->addHeaders(array("key"=>"val","more"=>"Stuff"));
111 $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo","Key"=>"val","More"=>"Stuff"), $m->getHeaders());
112 }
113
114 function testArrayHeaders() {
115 $m = new http\Message("GET / HTTP/1.1");
116 $m->addHeader("Accept", "text/html");
117 $m->addHeader("Accept", "text/xml;q=0");
118 $m->addHeader("Accept", "text/plain;q=0.5");
119 $this->assertEquals(
120 "GET / HTTP/1.1\r\n".
121 "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n",
122 $m->toString()
123 );
124 }
125
126 function testHeaderValueTypes() {
127 $m = new http\Message("HTTP/1.1 200 Ok");
128 $m->addHeader("Bool", true);
129 $m->addHeader("Int", 123);
130 $m->addHeader("Float", 1.23);
131 $m->addHeader("Array", array(1,2,3));
132 $m->addHeader("Object", new strval("test"));
133 $m->addHeader("Set-Cookie",
134 array(
135 array(
136 "cookies" => array("foo" => "bar"),
137 "expires" => date_create("2012-12-31 23:59:59")->format(
138 DateTime::COOKIE
139 ),
140 "path" => "/somewhere"
141 )
142 )
143 );
144 $m->addHeader("Set-Cookie", "val=0");
145
146 $this->assertEquals(
147 "HTTP/1.1 200 Ok\r\n".
148 "Bool: true\r\n".
149 "Int: 123\r\n".
150 "Float: 1.23\r\n".
151 "Array: 1, 2, 3\r\n".
152 "Object: test\r\n".
153 "Set-Cookie: foo=bar; path=/somewhere; expires=Mon, 31 Dec 2012 22:59:59 GMT; \r\n".
154 "Set-Cookie: val=0\r\n",
155 $m->toString()
156 );
157 }
158
159 function testDetach() {
160 $m = new http\Message(
161 "HTTP/1.1 200 Ok\r\n".
162 "HTTP/1.1 201 Created\n".
163 "HTTP/1.1 302 Found\r\n"
164 );
165
166 $this->assertCount(3, $m);
167 $d = $m->detach();
168 $this->assertCount(3, $m);
169 $this->assertCount(1, $d);
170
171 $this->assertEquals("HTTP/1.1 302 Found\r\n\r\n", $d->toString(true));
172 }
173
174 function testPrepend() {
175 for ($i=0; $i<9; ++$i) {
176 $a[] = new http\Message("HTTP/1.1 ".($i+200));
177 }
178
179 foreach ($a as $m) {
180 if (isset($p)) $m->prepend($p);
181 $p = $m;
182 }
183
184 $this->assertEquals(
185 "HTTP/1.1 200\r\n\r\n".
186 "HTTP/1.1 201\r\n\r\n".
187 "HTTP/1.1 202\r\n\r\n".
188 "HTTP/1.1 203\r\n\r\n".
189 "HTTP/1.1 204\r\n\r\n".
190 "HTTP/1.1 205\r\n\r\n".
191 "HTTP/1.1 206\r\n\r\n".
192 "HTTP/1.1 207\r\n\r\n".
193 "HTTP/1.1 208\r\n\r\n",
194 $m->toString(true)
195 );
196 }
197
198 function testPrependError() {
199 $m = new http\Message("HTTP/1.1 200\r\nHTTP/1.1 201");
200 try {
201 $m->prepend($m->getParentMessage());
202 $this->assertFalse("this code should not be reached");
203 } catch (http\Exception $e) {
204 $this->assertEquals("Cannot prepend a message located within the same message chain", $e->getMessage());
205 }
206 }
207
208 function testToCallback() {
209 $m = new http\Message("HTTP/1.1 200 Ok");
210 $m->addHeader("Content-Type", "text/plain");
211 $m->getBody()->append("this\nis\nthe\ntext");
212
213 $d = new http\Encoding\Stream\Deflate;
214 $s = "";
215 $m->toCallback(function ($m, $data) use ($d) {
216 $s.=$d->update($data);
217 });
218 $s.=$d->finish();
219 $this->assertEquals($m->toString(), http\Encoding\Stream\Inflate::decode($s));
220 }
221
222 function testToStream() {
223 $m = new http\Message("HTTP/1.1 200 Ok");
224 $m->addHeader("Content-Type", "text/plain");
225 $m->getBody()->append("this\nis\nthe\ntext");
226
227 $f = tmpfile();
228 $m->toStream($f);
229 rewind($f);
230 $this->assertEquals((string) $m, stream_get_contents($f));
231 fclose($f);
232 }
233
234 function testBoundary() {
235 $p = new http\Message;
236 $p->addHeader("Content-Type", "text/plain");
237 $p->getBody()->append("data");
238
239 $m = new http\Message("HTTP/1.1 200");
240 $m->getBody()->addPart($p);
241 $this->assertStringMatchesFormat(
242 "HTTP/1.1 200\r\n".
243 "Content-Length: 97\r\n".
244 "Content-Type: multipart/form-data; boundary=\"%x.%x\"\r\n".
245 "\r\n".
246 "--%x.%x\r\n".
247 "Content-Type: text/plain\r\n".
248 "Content-Length: 4\r\n".
249 "\r\n".
250 "data\r\n".
251 "--%x.%x--\r\n".
252 "",
253 str_replace("\r", "", $m->toString()) // phpunit replaces \r\n with \n
254 );
255 }
256 }
257