Skip to content

Commit

Permalink
Don't stop reading when only a '0' has been read.
Browse files Browse the repository at this point in the history
Make tests simpler by using onConsecutiveCalls() instead of trying to
maintain mock method indexes.

Refs #7121
  • Loading branch information
markstory committed Jul 28, 2015
1 parent 97be4f8 commit 58ea40e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Network/Http/HttpSocket.php
Expand Up @@ -378,7 +378,7 @@ public function request($request = array()) {

$response = null;
$inHeader = true;
while ($data = $this->read()) {
while (($data = $this->read()) !== false) {
if ($this->_contentResource) {
if ($inHeader) {
$response .= $data;
Expand Down
75 changes: 59 additions & 16 deletions lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Expand Up @@ -307,6 +307,10 @@ public function testConfigUri() {
* @return void
*/
public function testRequest() {
$this->Socket->expects($this->any())
->method('read')
->will($this->returnValue(false));

$this->Socket->reset();

$response = $this->Socket->request(true);
Expand Down Expand Up @@ -589,6 +593,10 @@ public function testRequest() {
* @return void
*/
public function testGetWithSchemeAndPort() {
$this->Socket->expects($this->any())
->method('read')
->will($this->returnValue(false));

$this->Socket->reset();
$request = array(
'uri' => array(
Expand All @@ -609,6 +617,10 @@ public function testGetWithSchemeAndPort() {
* @return void
*/
public function testRequestWithStringQuery() {
$this->Socket->expects($this->any())
->method('read')
->will($this->returnValue(false));

$this->Socket->reset();
$request = array(
'uri' => array(
Expand Down Expand Up @@ -642,14 +654,18 @@ public function testRequestNotAllowedUri() {
*/
public function testRequest2() {
$this->Socket->reset();

$request = array('uri' => 'htpp://www.cakephp.org/');
$number = mt_rand(0, 9999999);
$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>Hello, your lucky number is " . $number . "</h1>";
$this->Socket->expects($this->at(0))->method('write')
->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
$this->Socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse, false));

$response = (string)$this->Socket->request($request);
$this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
}
Expand All @@ -662,7 +678,11 @@ public function testRequest2() {
public function testRequest3() {
$request = array('uri' => 'htpp://www.cakephp.org/');
$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a cookie test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse, false));

$this->Socket->connected = true;
$this->Socket->request($request);
$result = $this->Socket->response['cookies'];
Expand Down Expand Up @@ -711,9 +731,10 @@ public function testRequestWithConstructor() {
*/
public function testRequestWithResource() {
$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse, false, $serverResponse, false));
$this->Socket->connected = true;

$f = fopen(TMP . 'download.txt', 'w');
Expand Down Expand Up @@ -744,8 +765,10 @@ public function testRequestWithCrossCookie() {
$this->Socket->config['request']['cookies'] = array();

$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";

$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));

$expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
$this->Socket->request('http://www.cakephp.org/');
$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
Expand Down Expand Up @@ -781,8 +804,9 @@ public function testRequestWithCrossCookie() {
public function testRequestCustomResponse() {
$this->Socket->connected = true;
$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse, false));

$this->Socket->responseClass = 'CustomResponse';
$response = $this->Socket->request('http://www.cakephp.org/');
Expand Down Expand Up @@ -817,6 +841,8 @@ public function testRequestWithRedirectUrlEncoded() {
$this->Socket->expects($this->at(4))
->method('read')
->will($this->returnValue($serverResponse2));
$this->Socket->expects($this->any())
->method('read')->will($this->returnValue(false));

$response = $this->Socket->request($request);
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
Expand All @@ -834,8 +860,10 @@ public function testRequestWithRedirectAsTrue() {
);
$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));

$response = $this->Socket->request($request);
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
Expand All @@ -853,8 +881,10 @@ public function testRequestWithRedirectAsInt() {
);
$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));

$this->Socket->request($request);
$this->assertEquals(1, $this->Socket->request['redirect']);
Expand All @@ -872,8 +902,10 @@ public function testRequestWithRedirectAsIntReachingZero() {
);
$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/oneruri\r\n\r\n";
$serverResponse2 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));

$response = $this->Socket->request($request);
$this->assertEquals(0, $this->Socket->request['redirect']);
Expand Down Expand Up @@ -1113,6 +1145,9 @@ public function testHead() {
* @return void
*/
public function testAuth() {
$this->Socket->expects($this->any())
->method('read')->will($this->returnValue(false));

$this->Socket->get('http://mark:secret@example.com/test');
$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);

Expand Down Expand Up @@ -1154,6 +1189,9 @@ public function testAuth() {
* @return void
*/
public function testConsecutiveGetResetsAuthCredentials() {
$this->Socket->expects($this->any())
->method('read')->will($this->returnValue(false));

$this->Socket->get('http://mark:secret@example.com/test');
$this->assertEquals('mark', $this->Socket->request['uri']['user']);
$this->assertEquals('secret', $this->Socket->request['uri']['pass']);
Expand Down Expand Up @@ -1813,6 +1851,9 @@ public function testPartialReset() {
* @return void
*/
public function testConfigContext() {
$this->Socket->expects($this->any())
->method('read')->will($this->returnValue(false));

$this->Socket->reset();
$this->Socket->request('http://example.com');
$this->assertTrue($this->Socket->config['context']['ssl']['verify_peer']);
Expand Down Expand Up @@ -1869,8 +1910,10 @@ public function statusProvider() {
public function testResponseStatusParsing($status, $code, $msg = '') {
$this->Socket->connected = true;
$serverResponse = $status . "\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));

$this->Socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls($serverResponse, false));

$response = $this->Socket->request('http://www.cakephp.org/');
$this->assertInstanceOf('HttpSocketResponse', $response);
Expand Down

0 comments on commit 58ea40e

Please sign in to comment.