Browse Source

Merge branch 'http-response-code' into master.

Merge changes from #3016 into master.

Closes #3016
mark_story 12 years ago
parent
commit
d559e429e6

+ 1 - 1
lib/Cake/Network/Http/HttpSocketResponse.php

@@ -159,7 +159,7 @@ class HttpSocketResponse implements ArrayAccess {
 		$this->raw = $message;
 		$this->body = (string)substr($message, strlen($match[0]));
 
-		if (preg_match("/(.+) ([0-9]{3}) (.+)\r\n/DU", $statusLine, $match)) {
+		if (preg_match("/(.+) ([0-9]{3})\s*([^ ]*)\r\n/DU", $statusLine, $match)) {
 			$this->httpVersion = $match[1];
 			$this->code = $match[2];
 			$this->reasonPhrase = $match[3];

+ 37 - 0
lib/Cake/Test/Case/Network/Http/HttpSocketTest.php

@@ -1754,4 +1754,41 @@ class HttpSocketTest extends CakeTestCase {
 			$this->assertContains('Failed to enable crypto', $message);
 		}
 	}
+
+/**
+ * Data provider for status codes.
+ *
+ * @return array
+ */
+	public function statusProvider() {
+		return array(
+			array('HTTP/1.1 200 '),
+			array('HTTP/1.1 200    '),
+			array('HTTP/1.1 200'),
+			array('HTTP/1.1 200  OK', 'OK'),
+		);
+	}
+
+/**
+ * test response status parsing
+ *
+ * @dataProvider statusProvider
+ * @return void
+ */
+	public function testResponseStatusParsing($status, $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));
+
+		$response = $this->Socket->request('http://www.cakephp.org/');
+		$this->assertInstanceOf('HttpSocketResponse', $response);
+		$expected = array(
+			'http-version' => 'HTTP/1.1',
+			'code' => '200',
+			'reason-phrase' => $msg
+		);
+		$this->assertEquals($expected, $response['status']);
+	}
+
 }