HttpTest.php 6.2 KB

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