OAuthUnitCommonHttpHttpClientsTest

The Gravity Forms Salesforce Add-On OAuth Unit Common Http HttpClientsTest class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/tests/Unit/Common/Http/HttpClientsTest.php  
  1. class HttpClientsTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @var object|\OAuth\Common\Http\Client\ClientInterface[] 
  4. */ 
  5. protected $clients; 
  6.  
  7. public function setUp() 
  8. $streamClient = new Client\StreamClient(); 
  9. $streamClient->setTimeout(3); 
  10.  
  11. $curlClient = new Client\CurlClient(); 
  12. $curlClient->setTimeout(3); 
  13.  
  14. $this->clients[] = $streamClient; 
  15. $this->clients[] = $curlClient; 
  16.  
  17. public function tearDown() 
  18. foreach ($this->clients as $client) { 
  19. unset($client); 
  20.  
  21. /** 
  22. * Test that extra headers are passed properly 
  23. */ 
  24. public function testHeaders() 
  25. $testUri = new Uri('http://httpbin.org/get'); 
  26.  
  27. $me = $this; 
  28. $headerCb = function ($response) use ($me) { 
  29. $data = json_decode($response, true); 
  30. $me->assertEquals('extraheadertest', $data['headers']['Testingheader']); 
  31. }; 
  32.  
  33. $this->__doTestRetrieveResponse($testUri, array(), array('Testingheader' => 'extraheadertest'), 'GET', $headerCb); 
  34.  
  35. /** 
  36. * Tests that we get an exception for a >= 400 status code 
  37. */ 
  38. public function testException() 
  39. // sending a post here should get us a 405 which should trigger an exception 
  40. $testUri = new Uri('http://httpbin.org/delete'); 
  41. foreach ($this->clients as $client) { 
  42. $this->setExpectedException('OAuth\Common\Http\Exception\TokenResponseException'); 
  43. $client->retrieveResponse($testUri, array('blah' => 'blih')); 
  44.  
  45. /** 
  46. * Tests the DELETE method 
  47. */ 
  48. public function testDelete() 
  49. $testUri = new Uri('http://httpbin.org/delete'); 
  50.  
  51. $me = $this; 
  52. $deleteTestCb = function ($response) use ($me) { 
  53. $data = json_decode($response, true); 
  54. $me->assertEquals('', $data['data']); 
  55. }; 
  56.  
  57. $this->__doTestRetrieveResponse($testUri, array(), array(), 'DELETE', $deleteTestCb); 
  58.  
  59. /** 
  60. * Tests the PUT method 
  61. */ 
  62. public function testPut() 
  63. $testUri = new Uri('http://httpbin.org/put'); 
  64.  
  65. $me = $this; 
  66. $putTestCb = function ($response) use ($me) { 
  67. // verify the put response 
  68. $data = json_decode($response, true); 
  69. $me->assertEquals(json_encode(array('testKey' => 'testValue')), $data['data']); 
  70. }; 
  71.  
  72. $this->__doTestRetrieveResponse($testUri, json_encode(array('testKey' => 'testValue')), array('Content-Type' => 'application/json'), 'PUT', $putTestCb); 
  73.  
  74. /** 
  75. * Tests the POST method 
  76. */ 
  77. public function testPost() 
  78. // http test server 
  79. $testUri = new Uri('http://httpbin.org/post'); 
  80.  
  81. $me = $this; 
  82. $postTestCb = function ($response) use ($me) { 
  83. // verify the post response 
  84. $data = json_decode($response, true); 
  85. // note that we check this because the retrieveResponse wrapper function automatically adds a content-type 
  86. // if there isn't one and it 
  87. $me->assertEquals('testValue', $data['form']['testKey']); 
  88. }; 
  89.  
  90. $this->__doTestRetrieveResponse($testUri, array('testKey' => 'testValue'), array(), 'POST', $postTestCb); 
  91.  
  92. /** 
  93. * Expect exception when we try to send a GET request with a body 
  94. */ 
  95. public function testInvalidGet() 
  96. $testUri = new Uri('http://site.net'); 
  97.  
  98. foreach ($this->clients as $client) { 
  99. $this->setExpectedException('InvalidArgumentException'); 
  100. $client->retrieveResponse($testUri, array('blah' => 'blih'), array(), 'GET'); 
  101.  
  102. /** 
  103. * Tests the GET method 
  104. */ 
  105. public function testGet() 
  106. // test uri 
  107. $testUri = new Uri('http://httpbin.org/get?testKey=testValue'); 
  108.  
  109. $me = $this; 
  110. $getTestCb = function ($response) use ($me) { 
  111. $data = json_decode($response, true); 
  112. $me->assertEquals('testValue', $data['args']['testKey']); 
  113. }; 
  114.  
  115. $this->__doTestRetrieveResponse($testUri, array(), array(), 'GET', $getTestCb); 
  116.  
  117. /** 
  118. * Test on all HTTP clients. 
  119. * @param UriInterface $uri 
  120. * @param array $param 
  121. * @param array $header 
  122. * @param string $method 
  123. * @param \Closure $responseCallback 
  124. */ 
  125. protected function __doTestRetrieveResponse(UriInterface $uri, $param, array $header, $method, $responseCallback) 
  126. foreach ($this->clients as $client) { 
  127. $response = $client->retrieveResponse($uri, $param, $header, $method); 
  128. $responseCallback($response, $client);