Changeset 1604
- Timestamp:
- 05/27/08 10:05:56 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php
r1495 r1604 7 7 * @subpackage peer_http 8 8 */ 9 stubClassLoader::load('net::stubbles::peer::stubHeaderList', 9 stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalAccessException', 10 'net::stubbles::peer::stubHeaderList', 10 11 'net::stubbles::peer::stubConnectionException', 11 12 'net::stubbles::peer::stubSocket' … … 118 119 /** 119 120 * read the response from socket and parse it 121 * 122 * @return stubHTTPResponse 120 123 */ 121 124 public function read() 125 { 126 return $this->readHeader()->readBody(); 127 } 128 129 /** 130 * reads response headers 131 * 132 * @return stubHTTPResponse 133 */ 134 public function readHeader() 122 135 { 123 136 $this->parseHead($this->socket->readLine()); … … 130 143 131 144 $this->headers = stubHeaderList::fromString($header); 145 return $this; 146 } 147 148 /** 149 * reads the response body 150 * 151 * @return stubHTTPResponse 152 * @throws stubIllegalAccessException 153 */ 154 public function readBody() 155 { 156 if (null === $this->headers) { 157 throw new stubIllegalAccessException('Need to read response headers first.'); 158 } 159 132 160 if ($this->headers->get('Transfer-Encoding') === 'chunked') { 133 161 $this->body = $this->readChunked(); … … 135 163 $this->body = $this->readDefault($this->headers->get('Content-Length', 4096)); 136 164 } 165 166 return $this; 137 167 } 138 168 trunk/src/test/php/net/stubbles/peer/http/stubHTTPResponseTestCase.php
r1495 r1604 124 124 $this->assertNull($httpResponse->getHeader()); 125 125 $this->assertEquals('', $httpResponse->getBody()); 126 $ httpResponse->read();126 $this->assertSame($httpResponse, $httpResponse->read()); 127 127 $this->assertType('stubHeaderList', $httpResponse->getHeader()); 128 128 $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); … … 167 167 $this->assertNull($httpResponse->getHeader()); 168 168 $this->assertEquals('', $httpResponse->getBody()); 169 $ httpResponse->read();169 $this->assertSame($httpResponse, $httpResponse->read()); 170 170 $this->assertType('stubHeaderList', $httpResponse->getHeader()); 171 171 $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); … … 211 211 $this->assertNull($httpResponse->getHeader()); 212 212 $this->assertEquals('', $httpResponse->getBody()); 213 $ httpResponse->read();213 $this->assertSame($httpResponse, $httpResponse->read()); 214 214 $this->assertType('stubHeaderList', $httpResponse->getHeader()); 215 215 $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 216 216 $this->assertEquals('body', $httpResponse->getBody()); 217 } 218 219 /** 220 * reading the header only 221 * 222 * @test 223 */ 224 public function readHead() 225 { 226 $httpResponse = $this->getMock('stubHTTPResponse', 227 array('parseHead', 228 'readChunked', 229 'readDefault' 230 ), 231 array($this->mockSocket) 232 ); 233 $this->mockSocket->expects($this->once()) 234 ->method('readLine') 235 ->will($this->returnValue('HTTP/1.1 200 OK' . stubHTTPConnection::END_OF_LINE)); 236 $this->mockSocket->expects($this->exactly(4)) 237 ->method('eof') 238 ->will($this->returnValue(false)); 239 $this->mockSocket->expects($this->exactly(3)) 240 ->method('read') 241 ->will($this->onConsecutiveCalls('Host: example.com' . stubHTTPConnection::END_OF_LINE, 242 'Content-Length: 2048' . stubHTTPConnection::END_OF_LINE, 243 stubHTTPConnection::END_OF_LINE 244 ) 245 ); 246 $httpResponse->expects($this->once()) 247 ->method('parseHead') 248 ->with($this->equalTo('HTTP/1.1 200 OK' . stubHTTPConnection::END_OF_LINE)); 249 $this->assertNull($httpResponse->getHeader()); 250 $this->assertEquals('', $httpResponse->getBody()); 251 $this->assertSame($httpResponse, $httpResponse->readHeader()); 252 $this->assertType('stubHeaderList', $httpResponse->getHeader()); 253 $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 254 } 255 256 /** 257 * reading body without previous reading of headers throws exception 258 * 259 * @test 260 * @expectedException stubIllegalAccessException 261 */ 262 public function readBodyWithoutReadingHeadersThrowsException() 263 { 264 $this->assertSame($this->httpResponse, $this->httpResponse->readBody()); 217 265 } 218 266
