Просмотр исходного кода

Merged r25148 from trunk; resolves ZF-8721

git-svn-id: http://framework.zend.com/svn/framework/standard/branches/release-1.12@25152 44c647ce-9c0f-0410-b52a-842ac1e357ba
cogo 13 лет назад
Родитель
Сommit
912c754ce3

+ 12 - 7
library/Zend/Service/ReCaptcha/Response.php

@@ -142,13 +142,18 @@ class Zend_Service_ReCaptcha_Response
     {
         $body = $response->getBody();
 
-        $parts = explode("\n", $body, 2);
+        // Default status and error code
+        $status = 'false';
+        $errorCode = '';
 
-        if (count($parts) !== 2) {
-            $status = 'false';
-            $errorCode = '';
-        } else {
-            list($status, $errorCode) = $parts;
+        $parts = explode("\n", $body);
+
+        if ($parts[0] === 'true') {
+            $status = 'true';
+        }
+
+        if (!empty($parts[1])) {
+            $errorCode = $parts[1];
         }
 
         $this->setStatus($status);
@@ -156,4 +161,4 @@ class Zend_Service_ReCaptcha_Response
 
         return $this;
     }
-}
+}

+ 22 - 0
tests/Zend/Service/ReCaptcha/ResponseTest.php

@@ -66,6 +66,16 @@ class Zend_Service_ReCaptcha_ResponseTest extends PHPUnit_Framework_TestCase
         $this->assertSame(false, $this->_response->isValid());
     }
 
+    public function testSetFromHttpResponseWhenResponseContentIsMissing() {
+        $responseBody = 'true';
+        $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
+
+        $this->_response->setFromHttpResponse($httpResponse);
+
+        $this->assertTrue($this->_response->getStatus());
+        $this->assertSame('', $this->_response->getErrorCode());
+    }
+
     public function testSetFromHttpResponse() {
         $status = 'false';
         $errorCode = 'foobar';
@@ -78,6 +88,18 @@ class Zend_Service_ReCaptcha_ResponseTest extends PHPUnit_Framework_TestCase
         $this->assertSame($errorCode, $this->_response->getErrorCode());
     }
 
+    public function testSetFromHttpResponseWhenResponseHasSeveralLinesOfContent() {
+        $status = 'false';
+        $errorCode = 'foobar';
+        $responseBody = $status . "\n" . $errorCode . "\nSome data\nEven more data";
+        $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
+
+        $this->_response->setFromHttpResponse($httpResponse);
+
+        $this->assertSame(false, $this->_response->getStatus());
+        $this->assertSame($errorCode, $this->_response->getErrorCode());
+    }
+
     public function testConstructor() {
         $status = 'true';
         $errorCode = 'ok';