HttpTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Http\RequestCore;
  4. use OSS\Http\ResponseCore;
  5. use OSS\Http\RequestCore_Exception;
  6. use Symfony\Component\Config\Definition\Exception\Exception;
  7. class HttpTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testResponseCore()
  10. {
  11. $res = new ResponseCore(null, "", 500);
  12. $this->assertFalse($res->isOK());
  13. $this->assertTrue($res->isOK(500));
  14. }
  15. public function testGet()
  16. {
  17. $httpCore = new RequestCore("http://www.baidu.com");
  18. $httpResponse = $httpCore->send_request();
  19. $this->assertNotNull($httpResponse);
  20. }
  21. public function testSetProxyAndTimeout()
  22. {
  23. $httpCore = new RequestCore("http://www.baidu.com");
  24. $httpCore->set_proxy("1.0.2.1:8888");
  25. $httpCore->connect_timeout = 1;
  26. try {
  27. $httpResponse = $httpCore->send_request();
  28. $this->assertTrue(false);
  29. } catch (RequestCore_Exception $e) {
  30. }
  31. }
  32. public function testGetParseTrue()
  33. {
  34. $httpCore = new RequestCore("http://www.baidu.com");
  35. $httpCore->curlopts = array(CURLOPT_HEADER => true);
  36. $url = $httpCore->send_request(true);
  37. foreach ($httpCore->get_response_header() as $key => $value) {
  38. $this->assertEquals($httpCore->get_response_header($key), $value);
  39. }
  40. $this->assertNotNull($url);
  41. }
  42. public function testParseResponse()
  43. {
  44. $httpCore = new RequestCore("http://www.baidu.com");
  45. $response = $httpCore->send_request();
  46. $parsed = $httpCore->process_response(null, $response);
  47. $this->assertNotNull($parsed);
  48. }
  49. public function testExceptionGet()
  50. {
  51. $httpCore = null;
  52. $exception = false;
  53. try {
  54. $httpCore = new RequestCore("http://www.notexistsitexx.com");
  55. $httpCore->set_body("");
  56. $httpCore->set_method("GET");
  57. $httpCore->connect_timeout = 10;
  58. $httpCore->timeout = 10;
  59. $res = $httpCore->send_request();
  60. } catch (RequestCore_Exception $e) {
  61. $exception = true;
  62. }
  63. $this->assertTrue($exception);
  64. }
  65. }