6 checkcls('HttpMessage');
7 skipif(!@include 'PHPUnit2/Framework/TestCase.php', 'need PHPUnit2');
13 error_reporting(E_ALL);
14 ini_set('html_errors', 0);
16 require_once 'PHPUnit2/Framework/TestSuite.php';
17 require_once 'PHPUnit2/Framework/TestCase.php';
18 require_once 'PHPUnit2/TextUI/ResultPrinter.php';
20 class HttpMessageTest extends PHPUnit2_Framework_TestCase
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");
29 function test___construct()
31 $this->assertTrue(new HttpMessage instanceof HttpMessage, "new HttpMessage instanceof HttpMessage");
34 function test_getBody()
36 $this->assertEquals('', $this->emptyMessage->getBody());
37 $this->assertEquals('Hi there!', $this->responseMessage->getBody());
38 $this->assertEquals("Hi there!\n", $this->requestMessage->getBody());
41 function test_setBody()
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());
51 function test_getHeaders()
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());
58 function test_setHeaders()
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());
68 function test_addHeaders()
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());
80 function test_getType()
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());
87 function test_setType()
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());
97 function test_getResponseCode()
99 $this->assertFalse($this->emptyMessage->getResponseCode());
100 $this->assertEquals(200, $this->responseMessage->getResponseCode());
101 $this->assertFalse($this->requestMessage->getResponseCode());
104 function test_setResponseCode()
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());
114 function test_getRequestMethod()
116 $this->assertFalse($this->emptyMessage->getRequestMethod());
117 $this->assertFalse($this->responseMessage->getRequestMethod());
118 $this->assertEquals('GET', $this->requestMessage->getRequestMethod());
121 function test_setRequestMethod()
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());
131 function test_getRequestUrl()
133 $this->assertFalse($this->emptyMessage->getRequestUrl());
134 $this->assertFalse($this->responseMessage->getRequestUrl());
135 $this->assertEquals('/foo', $this->requestMessage->getRequestUrl());
138 function test_setRequestUrl()
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());
148 function test_getHttpVersion()
150 $this->assertEquals('0.0', $this->emptyMessage->getHttpVersion());
151 $this->assertEquals('1.1', $this->responseMessage->getHttpVersion());
152 $this->assertEquals('1.1', $this->requestMessage->getHttpVersion());
155 function test_setHttpVersion()
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());
165 function test_getParentMessage()
167 $this->assertTrue($this->responseMessage->getParentMessage() instanceOf HttpMessage);
169 $this->requestMessage->getParentMessage();
170 $this->assertTrue(false, "\$this->requestMessage->getParentMessage() did not throw an exception");
171 } catch (HttpRuntimeException $ex) {
179 function test_toString()
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());
186 function test_count()
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));
193 $this->assertEquals(1, $this->emptyMessage->count());
194 $this->assertEquals(2, $this->responseMessage->count());
195 $this->assertEquals(1, $this->requestMessage->count());
199 function test_serialize()
203 function test_unserialize()
207 function test___toString()
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());
214 function test_fromString()
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());
223 $s = new PHPUnit2_Framework_TestSuite('HttpMessageTest');
224 $p = new PHPUnit2_TextUI_ResultPrinter();
225 $p->printResult($s->run(), 0);
232 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
234 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
236 Notice: HttpMessage::setResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
238 Notice: HttpMessage::setResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
240 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
242 Notice: HttpMessage::getResponseCode(): HttpMessage is not of type HTTP_MSG_RESPONSE in %sut_HttpMessage.php on line %d
244 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
246 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
248 Notice: HttpMessage::setRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
250 Notice: HttpMessage::setRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
252 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
254 Notice: HttpMessage::getRequestMethod(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
256 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
258 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
260 Notice: HttpMessage::setRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
262 Notice: HttpMessage::setRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
264 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d
266 Notice: HttpMessage::getRequestUrl(): HttpMessage is not of type HTTP_MSG_REQUEST in %sut_HttpMessage.php on line %d