5 function __construct($str) {
8 function __toString() {
9 return (string) $this->str
;
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)) {
22 $this->$prop = current($args);
29 class MessageTest
extends PHPUnit_Framework_TestCase
31 function testProperties() {
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());
102 function testAddBody() {
103 $m = new http\Message
;
104 $body = new http\Message\Body
;
105 $body->append("foo");
107 $body = new http\Message\Body
;
108 $body->append("bar");
110 $this->assertEquals("foobar", (string) $m->getBody());
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());
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");
127 "GET / HTTP/1.1\r\n".
128 "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n",
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",
143 "cookies" => array("foo" => "bar"),
144 "expires" => date_create("2012-12-31 23:59:59")->format(
147 "path" => "/somewhere"
151 $m->addHeader("Set-Cookie", "val=0");
154 "HTTP/1.1 200 Ok\r\n".
158 "Array: 1, 2, 3\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",
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"
173 $this->assertCount(3, $m);
175 $this->assertCount(3, $m);
176 $this->assertCount(1, $d);
178 $this->assertEquals("HTTP/1.1 302 Found\r\n\r\n", $d->toString(true));
181 function testPrepend() {
182 for ($i=0; $i<9; ++
$i) {
183 $a[] = new http\
Message("HTTP/1.1 ".($i+
200));
187 if (isset($p)) $m->prepend($p);
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",
205 function testEmptyUrlWarning() {
206 $m = new http\Message
;
207 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
208 $m->setRequestUrl("/foo");
209 $m->setType(http\Message
::TYPE_REQUEST
);
210 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
211 $m->setRequestUrl("");
214 function testEmptyParentMessage() {
215 $m = new http\Message
;
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());
224 function testPrependError() {
225 $m = new http\
Message("HTTP/1.1 200\r\nHTTP/1.1 201");
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());
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");
239 $d = new http\Encoding\Stream\Deflate
;
241 $m->toCallback(function ($m, $data) use ($d) {
242 $s.=$d->update($data);
245 $this->assertEquals($m->toString(), http\Encoding\Stream\Inflate
::decode($s));
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");
256 $this->assertEquals((string) $m, stream_get_contents($f));
260 function testBoundary() {
261 $p = new http\Message
;
262 $p->addHeader("Content-Type", "text/plain");
263 $p->getBody()->append("data");
265 $m = new http\
Message("HTTP/1.1 200");
266 $m->getBody()->addPart($p);
267 $this->assertStringMatchesFormat(
269 "Content-Length: %d\r\n".
270 "Content-Type: multipart/form-data; boundary=\"%x.%x\"\r\n".
273 "Content-Type: text/plain\r\n".
274 "Content-Length: 4\r\n".
279 str_replace("\r", "", $m->toString()) // phpunit replaces \r\n with \n