HttpTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_XmlRpc_Request_HttpTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_Request_HttpTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/AllTests/StreamWrapper/PhpInput.php';
  28. require_once 'Zend/XmlRpc/Request/Http.php';
  29. /**
  30. * Test case for Zend_XmlRpc_Request_Http
  31. *
  32. * @category Zend
  33. * @package Zend_XmlRpc
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_XmlRpc
  38. */
  39. class Zend_XmlRpc_Request_HttpTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_Request_HttpTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Setup environment
  53. */
  54. public function setUp()
  55. {
  56. $this->xml =<<<EOX
  57. <?xml version="1.0" encoding="UTF-8"?>
  58. <methodCall>
  59. <methodName>test.userUpdate</methodName>
  60. <params>
  61. <param>
  62. <value><string>blahblahblah</string></value>
  63. </param>
  64. <param>
  65. <value><struct>
  66. <member>
  67. <name>salutation</name>
  68. <value><string>Felsenblöcke</string></value>
  69. </member>
  70. <member>
  71. <name>firstname</name>
  72. <value><string>Lépiné</string></value>
  73. </member>
  74. <member>
  75. <name>lastname</name>
  76. <value><string>Géranté</string></value>
  77. </member>
  78. <member>
  79. <name>company</name>
  80. <value><string>Zend Technologies, Inc.</string></value>
  81. </member>
  82. </struct></value>
  83. </param>
  84. </params>
  85. </methodCall>
  86. EOX;
  87. $this->request = new Zend_XmlRpc_Request_Http();
  88. $this->request->loadXml($this->xml);
  89. $this->server = $_SERVER;
  90. foreach ($_SERVER as $key => $value) {
  91. if ('HTTP_' == substr($key, 0, 5)) {
  92. unset($_SERVER[$key]);
  93. }
  94. }
  95. $_SERVER['HTTP_USER_AGENT'] = 'Zend_XmlRpc_Client';
  96. $_SERVER['HTTP_HOST'] = 'localhost';
  97. $_SERVER['HTTP_CONTENT_TYPE'] = 'text/xml';
  98. $_SERVER['HTTP_CONTENT_LENGTH'] = strlen($this->xml) + 1;
  99. Zend_AllTests_StreamWrapper_PhpInput::mockInput($this->xml);
  100. }
  101. /**
  102. * Teardown environment
  103. */
  104. public function tearDown()
  105. {
  106. $_SERVER = $this->server;
  107. unset($this->request);
  108. Zend_AllTests_StreamWrapper_PhpInput::restoreDefault();
  109. }
  110. public function testGetRawRequest()
  111. {
  112. $this->assertEquals($this->xml, $this->request->getRawRequest());
  113. }
  114. public function testGetHeaders()
  115. {
  116. $expected = array(
  117. 'User-Agent' => 'Zend_XmlRpc_Client',
  118. 'Host' => 'localhost',
  119. 'Content-Type' => 'text/xml',
  120. 'Content-Length' => 958
  121. );
  122. $this->assertEquals($expected, $this->request->getHeaders());
  123. }
  124. public function testGetFullRequest()
  125. {
  126. $expected =<<<EOT
  127. User-Agent: Zend_XmlRpc_Client
  128. Host: localhost
  129. Content-Type: text/xml
  130. Content-Length: 958
  131. EOT;
  132. $expected .= $this->xml;
  133. $this->assertEquals($expected, $this->request->getFullRequest());
  134. }
  135. public function testCanPassInMethodAndParams()
  136. {
  137. try {
  138. $request = new Zend_XmlRpc_Request_Http('foo', array('bar', 'baz'));
  139. } catch (Exception $e) {
  140. $this->fail('Should be able to pass in methods and params to request');
  141. }
  142. }
  143. public function testExtendingClassShouldBeAbleToReceiveMethodAndParams()
  144. {
  145. try {
  146. $request = new Zend_XmlRpc_Request_HttpTest_Extension('foo', array('bar', 'baz'));
  147. } catch (Exception $e) {
  148. $this->fail('Should be able to pass in methods and params to request');
  149. }
  150. $this->assertEquals('foo', $request->method);
  151. $this->assertEquals(array('bar', 'baz'), $request->params);
  152. }
  153. public function testHttpRequestReadsFromPhpInput()
  154. {
  155. $this->assertNull(Zend_AllTests_StreamWrapper_PhpInput::argumentsPassedTo('stream_open'));
  156. $request = new Zend_XmlRpc_Request_Http();
  157. list($path, $mode,) = Zend_AllTests_StreamWrapper_PhpInput::argumentsPassedTo('stream_open');
  158. $this->assertSame('php://input', $path);
  159. $this->assertSame('rb', $mode);
  160. $this->assertSame($this->xml, $request->getRawRequest());
  161. }
  162. public function testHttpRequestGeneratesFaultIfReadFromPhpInputFails()
  163. {
  164. Zend_AllTests_StreamWrapper_PhpInput::methodWillReturn('stream_open', false);
  165. $request = new Zend_XmlRpc_Request_Http();
  166. $this->assertTrue($request->isFault());
  167. $this->assertSame(630, $request->getFault()->getCode());
  168. }
  169. }
  170. class Zend_XmlRpc_Request_HttpTest_Extension extends Zend_XmlRpc_Request_Http
  171. {
  172. public function __construct($method = null, $params = null)
  173. {
  174. $this->method = $method;
  175. $this->params = (array) $params;
  176. }
  177. }
  178. // Call Zend_XmlRpc_Request_HttpTest::main() if this source file is executed directly.
  179. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_Request_HttpTest::main") {
  180. Zend_XmlRpc_Request_HttpTest::main();
  181. }