Parcourir la source

add HTTP streaming tests

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19218 44c647ce-9c0f-0410-b52a-842ac1e357ba
stas il y a 16 ans
Parent
commit
45ea298fa8

+ 83 - 0
tests/Zend/Http/Client/CommonHttpTests.php

@@ -847,7 +847,90 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $expected = $this->_getTestFileContents('ZF4238-zerolineresponse.txt');
         $this->assertEquals($expected, $got);
     }
+    
+    public function testStreamResponse()
+    {
+        if(!($this->client->getAdapter() instanceof Zend_Http_Client_Adapter_Stream)) {
+              $this->markTestSkipped('Current adapter does not support streaming');
+              return;   
+        }
+        $this->client->setUri($this->baseuri . 'staticFile.jpg');
+        $this->client->setStream();
+
+        $response = $this->client->request();
+        
+        $this->assertTrue($response instanceof Zend_Http_Response_Stream, 'Request did not return stream response!');
+        $this->assertTrue(is_resource($response->getStream()), 'Request does not contain stream!');
+        
+        $stream_name = $response->getStreamName();
+     
+        $stream_read = stream_get_contents($response->getStream());
+        $file_read = file_get_contents($stream_name);
+        
+        $expected = $this->_getTestFileContents('staticFile.jpg');
+
+        $this->assertEquals($expected, $stream_read, 'Downloaded stream does not seem to match!');
+        $this->assertEquals($expected, $file_read, 'Downloaded file does not seem to match!');
+    }
+    
+    public function testStreamResponseBody()
+    {
+        if(!($this->client->getAdapter() instanceof Zend_Http_Client_Adapter_Stream)) {
+              $this->markTestSkipped('Current adapter does not support streaming');
+              return;   
+        }
+        $this->client->setUri($this->baseuri . 'staticFile.jpg');
+        $this->client->setStream();
+
+        $response = $this->client->request();
+        
+        $this->assertTrue($response instanceof Zend_Http_Response_Stream, 'Request did not return stream response!');
+        $this->assertTrue(is_resource($response->getStream()), 'Request does not contain stream!');
+        
+        $body = $response->getBody();
+        
+        $expected = $this->_getTestFileContents('staticFile.jpg');
+        $this->assertEquals($expected, $body, 'Downloaded stream does not seem to match!');
+    }
+    
+    public function testStreamResponseNamed()
+    {
+        if(!($this->client->getAdapter() instanceof Zend_Http_Client_Adapter_Stream)) {
+              $this->markTestSkipped('Current adapter does not support streaming');
+              return;   
+        }
+        $this->client->setUri($this->baseuri . 'staticFile.jpg');
+        $outfile = tempnam(sys_get_temp_dir(), "outstream");
+        $this->client->setStream($outfile);
+
+        $response = $this->client->request();
+        
+        $this->assertTrue($response instanceof Zend_Http_Response_Stream, 'Request did not return stream response!');
+        $this->assertTrue(is_resource($response->getStream()), 'Request does not contain stream!');
+        
+        $this->assertEquals($outfile, $response->getStreamName());
+     
+        $stream_read = stream_get_contents($response->getStream());
+        $file_read = file_get_contents($outfile);
+        
+        $expected = $this->_getTestFileContents('staticFile.jpg');
 
+        $this->assertEquals($expected, $stream_read, 'Downloaded stream does not seem to match!');
+        $this->assertEquals($expected, $file_read, 'Downloaded file does not seem to match!');
+    }
+       
+    public function testStreamRequest()
+    {
+        if(!($this->client->getAdapter() instanceof Zend_Http_Client_Adapter_Stream)) {
+              $this->markTestSkipped('Current adapter does not support streaming');
+              return;   
+        }
+        $data = fopen(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg', "r"); 
+        $res = $this->client->setRawData($data, 'image/jpeg')->request('PUT');
+        $expected = $this->_getTestFileContents('staticFile.jpg');
+        $this->assertEquals($expected, $res->getBody(), 'Response body does not contain the expected data');
+    }
+    
     /**
      * Internal helpder function to get the contents of test files
      *

+ 2 - 1
tests/Zend/Http/Client/StaticTest.php

@@ -597,7 +597,8 @@ class Zend_Http_Client_StaticTest_Mock extends Zend_Http_Client
         'httpversion'     => self::HTTP_1,
         'keepalive'       => false,
         'storeresponse'   => true,
-        'strict'          => true
+        'strict'          => true,
+        'output_stream'	  => false,
     );
 }
 

+ 23 - 0
tests/Zend/Http/Client/_files/testStreamRequest.php

@@ -0,0 +1,23 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: testRawPostData.php 17573 2009-08-13 18:01:41Z alexander $
+ */
+
+readfile('php://input');