Skip to content

Commit 58ea40e

Browse files
committed
Don't stop reading when only a '0' has been read.
Make tests simpler by using onConsecutiveCalls() instead of trying to maintain mock method indexes. Refs #7121
1 parent 97be4f8 commit 58ea40e

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

lib/Cake/Network/Http/HttpSocket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public function request($request = array()) {
378378

379379
$response = null;
380380
$inHeader = true;
381-
while ($data = $this->read()) {
381+
while (($data = $this->read()) !== false) {
382382
if ($this->_contentResource) {
383383
if ($inHeader) {
384384
$response .= $data;

lib/Cake/Test/Case/Network/Http/HttpSocketTest.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ public function testConfigUri() {
307307
* @return void
308308
*/
309309
public function testRequest() {
310+
$this->Socket->expects($this->any())
311+
->method('read')
312+
->will($this->returnValue(false));
313+
310314
$this->Socket->reset();
311315

312316
$response = $this->Socket->request(true);
@@ -589,6 +593,10 @@ public function testRequest() {
589593
* @return void
590594
*/
591595
public function testGetWithSchemeAndPort() {
596+
$this->Socket->expects($this->any())
597+
->method('read')
598+
->will($this->returnValue(false));
599+
592600
$this->Socket->reset();
593601
$request = array(
594602
'uri' => array(
@@ -609,6 +617,10 @@ public function testGetWithSchemeAndPort() {
609617
* @return void
610618
*/
611619
public function testRequestWithStringQuery() {
620+
$this->Socket->expects($this->any())
621+
->method('read')
622+
->will($this->returnValue(false));
623+
612624
$this->Socket->reset();
613625
$request = array(
614626
'uri' => array(
@@ -642,14 +654,18 @@ public function testRequestNotAllowedUri() {
642654
*/
643655
public function testRequest2() {
644656
$this->Socket->reset();
657+
645658
$request = array('uri' => 'htpp://www.cakephp.org/');
646659
$number = mt_rand(0, 9999999);
647660
$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
648661
$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>";
649662
$this->Socket->expects($this->at(0))->method('write')
650663
->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
651-
$this->Socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
652-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
664+
665+
$this->Socket->expects($this->any())
666+
->method('read')
667+
->will($this->onConsecutiveCalls($serverResponse, false));
668+
653669
$response = (string)$this->Socket->request($request);
654670
$this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
655671
}
@@ -662,7 +678,11 @@ public function testRequest2() {
662678
public function testRequest3() {
663679
$request = array('uri' => 'htpp://www.cakephp.org/');
664680
$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>";
665-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
681+
682+
$this->Socket->expects($this->any())
683+
->method('read')
684+
->will($this->onConsecutiveCalls($serverResponse, false));
685+
666686
$this->Socket->connected = true;
667687
$this->Socket->request($request);
668688
$result = $this->Socket->response['cookies'];
@@ -711,9 +731,10 @@ public function testRequestWithConstructor() {
711731
*/
712732
public function testRequestWithResource() {
713733
$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>";
714-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
715-
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
716-
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
734+
735+
$this->Socket->expects($this->any())
736+
->method('read')
737+
->will($this->onConsecutiveCalls($serverResponse, false, $serverResponse, false));
717738
$this->Socket->connected = true;
718739

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

746767
$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>";
768+
747769
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
748770
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
771+
749772
$expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
750773
$this->Socket->request('http://www.cakephp.org/');
751774
$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
@@ -781,8 +804,9 @@ public function testRequestWithCrossCookie() {
781804
public function testRequestCustomResponse() {
782805
$this->Socket->connected = true;
783806
$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>";
784-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
785-
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
807+
$this->Socket->expects($this->any())
808+
->method('read')
809+
->will($this->onConsecutiveCalls($serverResponse, false));
786810

787811
$this->Socket->responseClass = 'CustomResponse';
788812
$response = $this->Socket->request('http://www.cakephp.org/');
@@ -817,6 +841,8 @@ public function testRequestWithRedirectUrlEncoded() {
817841
$this->Socket->expects($this->at(4))
818842
->method('read')
819843
->will($this->returnValue($serverResponse2));
844+
$this->Socket->expects($this->any())
845+
->method('read')->will($this->returnValue(false));
820846

821847
$response = $this->Socket->request($request);
822848
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
@@ -834,8 +860,10 @@ public function testRequestWithRedirectAsTrue() {
834860
);
835861
$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";
836862
$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>";
837-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
838-
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
863+
864+
$this->Socket->expects($this->any())
865+
->method('read')
866+
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));
839867

840868
$response = $this->Socket->request($request);
841869
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
@@ -853,8 +881,10 @@ public function testRequestWithRedirectAsInt() {
853881
);
854882
$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";
855883
$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>";
856-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
857-
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
884+
885+
$this->Socket->expects($this->any())
886+
->method('read')
887+
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));
858888

859889
$this->Socket->request($request);
860890
$this->assertEquals(1, $this->Socket->request['redirect']);
@@ -872,8 +902,10 @@ public function testRequestWithRedirectAsIntReachingZero() {
872902
);
873903
$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";
874904
$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";
875-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
876-
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
905+
906+
$this->Socket->expects($this->any())
907+
->method('read')
908+
->will($this->onConsecutiveCalls($serverResponse1, false, $serverResponse2, false));
877909

878910
$response = $this->Socket->request($request);
879911
$this->assertEquals(0, $this->Socket->request['redirect']);
@@ -1113,6 +1145,9 @@ public function testHead() {
11131145
* @return void
11141146
*/
11151147
public function testAuth() {
1148+
$this->Socket->expects($this->any())
1149+
->method('read')->will($this->returnValue(false));
1150+
11161151
$this->Socket->get('http://mark:secret@example.com/test');
11171152
$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
11181153

@@ -1154,6 +1189,9 @@ public function testAuth() {
11541189
* @return void
11551190
*/
11561191
public function testConsecutiveGetResetsAuthCredentials() {
1192+
$this->Socket->expects($this->any())
1193+
->method('read')->will($this->returnValue(false));
1194+
11571195
$this->Socket->get('http://mark:secret@example.com/test');
11581196
$this->assertEquals('mark', $this->Socket->request['uri']['user']);
11591197
$this->assertEquals('secret', $this->Socket->request['uri']['pass']);
@@ -1813,6 +1851,9 @@ public function testPartialReset() {
18131851
* @return void
18141852
*/
18151853
public function testConfigContext() {
1854+
$this->Socket->expects($this->any())
1855+
->method('read')->will($this->returnValue(false));
1856+
18161857
$this->Socket->reset();
18171858
$this->Socket->request('http://example.com');
18181859
$this->assertTrue($this->Socket->config['context']['ssl']['verify_peer']);
@@ -1869,8 +1910,10 @@ public function statusProvider() {
18691910
public function testResponseStatusParsing($status, $code, $msg = '') {
18701911
$this->Socket->connected = true;
18711912
$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>";
1872-
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
1873-
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
1913+
1914+
$this->Socket->expects($this->any())
1915+
->method('read')
1916+
->will($this->onConsecutiveCalls($serverResponse, false));
18741917

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

0 commit comments

Comments
 (0)