OAuthTestUnitCommonHttpClientCurlClientTest

The Gravity Forms Salesforce Add-On OAuthTest Unit Common Http Client CurlClientTest class.

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/Common/Http/Client/CurlClientTest.php  
  1. class CurlClientTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. */ 
  4. public function testConstructCorrectInstance() 
  5. $client = new CurlClient(); 
  6.  
  7. $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\AbstractClient', $client); 
  8.  
  9. /** 
  10. * @covers OAuth\Common\Http\Client\CurlClient::setForceSSL3 
  11. */ 
  12. public function testSetForceSSL3() 
  13. $client = new CurlClient(); 
  14.  
  15. $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\CurlClient', $client->setForceSSL3(true)); 
  16.  
  17. /** 
  18. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  19. */ 
  20. public function testRetrieveResponseThrowsExceptionOnGetRequestWithBody() 
  21. $this->setExpectedException('\\InvalidArgumentException'); 
  22.  
  23. $client = new CurlClient(); 
  24.  
  25. $client->retrieveResponse( 
  26. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),  
  27. 'foo',  
  28. array(),  
  29. 'GET' 
  30. ); 
  31.  
  32. /** 
  33. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  34. */ 
  35. public function testRetrieveResponseThrowsExceptionOnGetRequestWithBodyMethodConvertedToUpper() 
  36. $this->setExpectedException('\\InvalidArgumentException'); 
  37.  
  38. $client = new CurlClient(); 
  39.  
  40. $client->retrieveResponse( 
  41. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),  
  42. 'foo',  
  43. array(),  
  44. 'get' 
  45. ); 
  46.  
  47. /** 
  48. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  49. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  50. */ 
  51. public function testRetrieveResponseDefaultUserAgent() 
  52. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  53. $endPoint->expects($this->any()) 
  54. ->method('getHost') 
  55. ->will($this->returnValue('httpbin.org')); 
  56. $endPoint->expects($this->any()) 
  57. ->method('getAbsoluteUri') 
  58. ->will($this->returnValue('http://httpbin.org/get')); 
  59.  
  60. $client = new CurlClient(); 
  61.  
  62. $response = $client->retrieveResponse( 
  63. $endPoint,  
  64. '',  
  65. array(),  
  66. 'get' 
  67. ); 
  68.  
  69. $response = json_decode($response, true); 
  70.  
  71. $this->assertSame('PHPoAuthLib', $response['headers']['User-Agent']); 
  72.  
  73. /** 
  74. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  75. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  76. */ 
  77. public function testRetrieveResponseCustomUserAgent() 
  78. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  79. $endPoint->expects($this->any()) 
  80. ->method('getHost') 
  81. ->will($this->returnValue('httpbin.org')); 
  82. $endPoint->expects($this->any()) 
  83. ->method('getAbsoluteUri') 
  84. ->will($this->returnValue('http://httpbin.org/get')); 
  85.  
  86. $client = new CurlClient('My Super Awesome Http Client'); 
  87.  
  88. $response = $client->retrieveResponse( 
  89. $endPoint,  
  90. '',  
  91. array(),  
  92. 'get' 
  93. ); 
  94.  
  95. $response = json_decode($response, true); 
  96.  
  97. $this->assertSame('My Super Awesome Http Client', $response['headers']['User-Agent']); 
  98.  
  99. /** 
  100. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  101. */ 
  102. public function testRetrieveResponseWithCustomContentType() 
  103. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  104. $endPoint->expects($this->any()) 
  105. ->method('getHost') 
  106. ->will($this->returnValue('httpbin.org')); 
  107. $endPoint->expects($this->any()) 
  108. ->method('getAbsoluteUri') 
  109. ->will($this->returnValue('http://httpbin.org/get')); 
  110.  
  111. $client = new CurlClient(); 
  112.  
  113. $response = $client->retrieveResponse( 
  114. $endPoint,  
  115. '',  
  116. array('Content-Type' => 'foo/bar'),  
  117. 'get' 
  118. ); 
  119.  
  120. $response = json_decode($response, true); 
  121.  
  122. $this->assertSame('foo/bar', $response['headers']['Content-Type']); 
  123.  
  124. /** 
  125. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  126. */ 
  127. public function testRetrieveResponseWithFormUrlEncodedContentType() 
  128. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  129. $endPoint->expects($this->any()) 
  130. ->method('getHost') 
  131. ->will($this->returnValue('httpbin.org')); 
  132. $endPoint->expects($this->any()) 
  133. ->method('getAbsoluteUri') 
  134. ->will($this->returnValue('http://httpbin.org/post')); 
  135.  
  136. $client = new CurlClient(); 
  137.  
  138. $response = $client->retrieveResponse( 
  139. $endPoint,  
  140. array('foo' => 'bar', 'baz' => 'fab'),  
  141. array(),  
  142. 'POST' 
  143. ); 
  144.  
  145. $response = json_decode($response, true); 
  146.  
  147. $this->assertSame('application/x-www-form-urlencoded', $response['headers']['Content-Type']); 
  148. $this->assertEquals(array('foo' => 'bar', 'baz' => 'fab'), $response['form']); 
  149.  
  150. /** 
  151. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  152. */ 
  153. public function testRetrieveResponseHost() 
  154. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  155. $endPoint->expects($this->any()) 
  156. ->method('getHost') 
  157. ->will($this->returnValue('httpbin.org')); 
  158. $endPoint->expects($this->any()) 
  159. ->method('getAbsoluteUri') 
  160. ->will($this->returnValue('http://httpbin.org/post')); 
  161.  
  162. $client = new CurlClient(); 
  163.  
  164. $response = $client->retrieveResponse( 
  165. $endPoint,  
  166. array('foo' => 'bar', 'baz' => 'fab'),  
  167. array(),  
  168. 'POST' 
  169. ); 
  170.  
  171. $response = json_decode($response, true); 
  172.  
  173. $this->assertSame('httpbin.org', $response['headers']['Host']); 
  174.  
  175. /** 
  176. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  177. */ 
  178. public function testRetrieveResponsePostRequestWithRequestBodyAsString() 
  179. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  180. $endPoint->expects($this->any()) 
  181. ->method('getHost') 
  182. ->will($this->returnValue('httpbin.org')); 
  183. $endPoint->expects($this->any()) 
  184. ->method('getAbsoluteUri') 
  185. ->will($this->returnValue('http://httpbin.org/post')); 
  186.  
  187. $client = new CurlClient(); 
  188.  
  189. $response = $client->retrieveResponse( 
  190. $endPoint,  
  191. 'foo',  
  192. array(),  
  193. 'POST' 
  194. ); 
  195.  
  196. $response = json_decode($response, true); 
  197.  
  198. $this->assertSame('foo', $response['data']); 
  199.  
  200. /** 
  201. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  202. */ 
  203. public function testRetrieveResponsePutRequestWithRequestBodyAsString() 
  204. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  205. $endPoint->expects($this->any()) 
  206. ->method('getHost') 
  207. ->will($this->returnValue('httpbin.org')); 
  208. $endPoint->expects($this->any()) 
  209. ->method('getAbsoluteUri') 
  210. ->will($this->returnValue('http://httpbin.org/put')); 
  211.  
  212. $client = new CurlClient(); 
  213.  
  214. $response = $client->retrieveResponse( 
  215. $endPoint,  
  216. 'foo',  
  217. array(),  
  218. 'PUT' 
  219. ); 
  220.  
  221. $response = json_decode($response, true); 
  222.  
  223. $this->assertSame('foo', $response['data']); 
  224.  
  225. /** 
  226. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  227. */ 
  228. public function testRetrieveResponsePutRequestWithRequestBodyAsStringNoRedirects() 
  229. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  230. $endPoint->expects($this->any()) 
  231. ->method('getHost') 
  232. ->will($this->returnValue('httpbin.org')); 
  233. $endPoint->expects($this->any()) 
  234. ->method('getAbsoluteUri') 
  235. ->will($this->returnValue('http://httpbin.org/put')); 
  236.  
  237. $client = new CurlClient(); 
  238.  
  239. $client->setMaxRedirects(0); 
  240.  
  241. $response = $client->retrieveResponse( 
  242. $endPoint,  
  243. 'foo',  
  244. array(),  
  245. 'PUT' 
  246. ); 
  247.  
  248. $response = json_decode($response, true); 
  249.  
  250. $this->assertSame('foo', $response['data']); 
  251.  
  252. /** 
  253. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  254. */ 
  255. public function testRetrieveResponseWithForcedSsl3() 
  256. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  257. $endPoint->expects($this->any()) 
  258. ->method('getHost') 
  259. ->will($this->returnValue('httpbin.org')); 
  260. $endPoint->expects($this->any()) 
  261. ->method('getAbsoluteUri') 
  262. ->will($this->returnValue('https://httpbin.org/get')); 
  263.  
  264. $client = new CurlClient(); 
  265.  
  266. $client->setForceSSL3(true); 
  267.  
  268. $response = $client->retrieveResponse( 
  269. $endPoint,  
  270. '',  
  271. array('Content-Type' => 'foo/bar'),  
  272. 'get' 
  273. ); 
  274.  
  275. $response = json_decode($response, true); 
  276.  
  277. $this->assertSame('foo/bar', $response['headers']['Content-Type']); 
  278.  
  279. /** 
  280. * @covers OAuth\Common\Http\Client\CurlClient::retrieveResponse 
  281. */ 
  282. public function testRetrieveResponseThrowsExceptionOnInvalidUrl() 
  283. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  284.  
  285. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  286. $endPoint->expects($this->any()) 
  287. ->method('getHost') 
  288. ->will($this->returnValue('jkehfkefcmekjhcnkerjh')); 
  289. $endPoint->expects($this->any()) 
  290. ->method('getAbsoluteUri') 
  291. ->will($this->returnValue('jkehfkefcmekjhcnkerjh')); 
  292.  
  293. $client = new CurlClient(); 
  294.  
  295. $client->setForceSSL3(true); 
  296.  
  297. $response = $client->retrieveResponse( 
  298. $endPoint,  
  299. '',  
  300. array('Content-Type' => 'foo/bar'),  
  301. 'get' 
  302. ); 
  303.  
  304. $response = json_decode($response, true); 
  305.  
  306. $this->assertSame('foo/bar', $response['headers']['Content-Type']); 
  307.  
  308. public function testAdditionalParameters() 
  309. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  310. $endPoint->expects($this->any()) 
  311. ->method('getHost') 
  312. ->will($this->returnValue('httpbin.org')); 
  313. $endPoint->expects($this->any()) 
  314. ->method('getAbsoluteUri') 
  315. ->will($this->returnValue('http://httpbin.org/gzip')); 
  316.  
  317. $client = new CurlClient(); 
  318. $client->setCurlParameters(array( 
  319. CURLOPT_ENCODING => 'gzip',  
  320. )); 
  321.  
  322. $response = $client->retrieveResponse( 
  323. $endPoint,  
  324. '',  
  325. array(),  
  326. 'get' 
  327. ); 
  328.  
  329. $response = json_decode($response, true); 
  330.  
  331. $this->assertNotNull($response); 
  332. $this->assertSame('gzip', $response['headers']['Accept-Encoding']); 
  333. $this->assertTrue($response['gzipped']);