選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

102 行
2.7 KiB

  1. <?php
  2. /***************************************************************************
  3. *
  4. * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  5. *
  6. **************************************************************************/
  7. /**
  8. *
  9. * @file test_HttpRequest.php
  10. * @encoding UTF-8
  11. *
  12. * @date 2014年12月27日
  13. *
  14. */
  15. if(!defined('PUSH_SDK_HOME')){
  16. define('PUSH_SDK_HOME', dirname(dirname(__FILE__)));
  17. }
  18. include_once PUSH_SDK_HOME.'/lib/HttpRequest.php';
  19. class HttpRequestTest extends PHPUnit_Framework_TestCase{
  20. private $payload = array (
  21. 'a' => 100,
  22. 'b' => 200,
  23. 'c' => '!@#$%^&*()',
  24. '中文' => '中文结果',
  25. );
  26. public function testCurl(){
  27. $this->AssertTrue(is_callable('curl_version'), "php extension [cUrl] is not exists!!");
  28. return true;
  29. }
  30. /**
  31. * @depends testCurl
  32. */
  33. public function testServerReady(){
  34. exec('curl --head http://127.0.0.1:9890', $out);
  35. $failedMsg = "
  36. Error: the Test server not runing!!
  37. please make sure the test server runing at the 127.0.0.1:9890, \n
  38. to start the test server by commend 'php -S 0.0.0.0:9890' in the test directory. \n\n";
  39. $this -> assertTrue(count($out) > 0, $failedMsg);
  40. return true;
  41. }
  42. /**
  43. * @depends testServerReady
  44. * @return
  45. */
  46. public function testCreateHttpRequest(){
  47. $log = new PushSimpleLog('stdout',0);
  48. $http = new HttpRequest("http://127.0.0.1:9890/", null, $log);
  49. return $http;
  50. }
  51. /**
  52. * @param array $res
  53. */
  54. private function valiedResponse($res){
  55. extract($res);
  56. $this->assertEquals(200, intval($status));
  57. $rsObj = json_decode($content, true);
  58. $this -> assertNotEmpty($rsObj);
  59. foreach($this->payload as $key => $value){
  60. $this -> assertEquals($rsObj [$key], $value);
  61. }
  62. }
  63. /**
  64. * @depends testCreateHttpRequest
  65. * @param HttpRequest $http
  66. */
  67. public function testGet($http){
  68. $rs = $http -> get('httpServer.php', $this -> payload);
  69. $this->valiedResponse($rs);
  70. }
  71. /**
  72. * @depends testCreateHttpRequest
  73. * @param HttpRequest $http
  74. */
  75. public function testPost($http){
  76. $rs = $http -> post('httpServer.php', $this -> payload);
  77. $this->valiedResponse($rs);
  78. }
  79. /**
  80. * @depends testCreateHttpRequest
  81. * @param HttpRequest $http
  82. */
  83. public function testResolve($http){
  84. $this->assertEquals("http://127.0.0.1:9890/", $http -> resolve("http://127.0.0.1:9890/"));
  85. $this->assertEquals("http://127.0.0.1:9890/httpServer.php", $http -> resolve("httpServer.php"));
  86. }
  87. }