OAuthTestUnitCommonHttpClientStreamClientTest

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

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/Common/Http/Client/StreamClientTest.php  
  1. class StreamClientTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. */ 
  4. public function testConstructCorrectInstance() 
  5. $client = new StreamClient(); 
  6.  
  7. $this->assertInstanceOf('\\OAuth\\Common\\Http\\Client\\AbstractClient', $client); 
  8.  
  9. /** 
  10. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  11. */ 
  12. public function testRetrieveResponseThrowsExceptionOnGetRequestWithBody() 
  13. $this->setExpectedException('\\InvalidArgumentException'); 
  14.  
  15. $client = new StreamClient(); 
  16.  
  17. $client->retrieveResponse( 
  18. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),  
  19. 'foo',  
  20. array(),  
  21. 'GET' 
  22. ); 
  23.  
  24. /** 
  25. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  26. */ 
  27. public function testRetrieveResponseThrowsExceptionOnGetRequestWithBodyMethodConvertedToUpper() 
  28. $this->setExpectedException('\\InvalidArgumentException'); 
  29.  
  30. $client = new StreamClient(); 
  31.  
  32. $client->retrieveResponse( 
  33. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),  
  34. 'foo',  
  35. array(),  
  36. 'get' 
  37. ); 
  38.  
  39. /** 
  40. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  41. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  42. */ 
  43. public function testRetrieveResponseDefaultUserAgent() 
  44. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  45. $endPoint->expects($this->any()) 
  46. ->method('getHost') 
  47. ->will($this->returnValue('httpbin.org')); 
  48. $endPoint->expects($this->any()) 
  49. ->method('getAbsoluteUri') 
  50. ->will($this->returnValue('http://httpbin.org/get')); 
  51.  
  52. $client = new StreamClient(); 
  53.  
  54. $response = $client->retrieveResponse( 
  55. $endPoint,  
  56. '',  
  57. array(),  
  58. 'get' 
  59. ); 
  60.  
  61. $response = json_decode($response, true); 
  62.  
  63. $this->assertSame('PHPoAuthLib', $response['headers']['User-Agent']); 
  64.  
  65. /** 
  66. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  67. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  68. */ 
  69. public function testRetrieveResponseCustomUserAgent() 
  70. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  71. $endPoint->expects($this->any()) 
  72. ->method('getHost') 
  73. ->will($this->returnValue('httpbin.org')); 
  74. $endPoint->expects($this->any()) 
  75. ->method('getAbsoluteUri') 
  76. ->will($this->returnValue('http://httpbin.org/get')); 
  77.  
  78. $client = new StreamClient('My Super Awesome Http Client'); 
  79.  
  80. $response = $client->retrieveResponse( 
  81. $endPoint,  
  82. '',  
  83. array(),  
  84. 'get' 
  85. ); 
  86.  
  87. $response = json_decode($response, true); 
  88.  
  89. $this->assertSame('My Super Awesome Http Client', $response['headers']['User-Agent']); 
  90.  
  91. /** 
  92. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  93. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  94. */ 
  95. public function testRetrieveResponseWithCustomContentType() 
  96. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  97. $endPoint->expects($this->any()) 
  98. ->method('getHost') 
  99. ->will($this->returnValue('httpbin.org')); 
  100. $endPoint->expects($this->any()) 
  101. ->method('getAbsoluteUri') 
  102. ->will($this->returnValue('http://httpbin.org/get')); 
  103.  
  104. $client = new StreamClient(); 
  105.  
  106. $response = $client->retrieveResponse( 
  107. $endPoint,  
  108. '',  
  109. array('Content-Type' => 'foo/bar'),  
  110. 'get' 
  111. ); 
  112.  
  113. $response = json_decode($response, true); 
  114.  
  115. $this->assertSame('foo/bar', $response['headers']['Content-Type']); 
  116.  
  117. /** 
  118. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  119. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  120. */ 
  121. public function testRetrieveResponseWithFormUrlEncodedContentType() 
  122. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  123. $endPoint->expects($this->any()) 
  124. ->method('getHost') 
  125. ->will($this->returnValue('httpbin.org')); 
  126. $endPoint->expects($this->any()) 
  127. ->method('getAbsoluteUri') 
  128. ->will($this->returnValue('http://httpbin.org/post')); 
  129.  
  130. $client = new StreamClient(); 
  131.  
  132. $response = $client->retrieveResponse( 
  133. $endPoint,  
  134. array('foo' => 'bar', 'baz' => 'fab'),  
  135. array(),  
  136. 'POST' 
  137. ); 
  138.  
  139. $response = json_decode($response, true); 
  140.  
  141. $this->assertSame('application/x-www-form-urlencoded', $response['headers']['Content-Type']); 
  142. $this->assertEquals(array('foo' => 'bar', 'baz' => 'fab'), $response['form']); 
  143.  
  144. /** 
  145. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  146. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  147. */ 
  148. public function testRetrieveResponseHost() 
  149. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  150. $endPoint->expects($this->any()) 
  151. ->method('getHost') 
  152. ->will($this->returnValue('httpbin.org')); 
  153. $endPoint->expects($this->any()) 
  154. ->method('getAbsoluteUri') 
  155. ->will($this->returnValue('http://httpbin.org/post')); 
  156.  
  157. $client = new StreamClient(); 
  158.  
  159. $response = $client->retrieveResponse( 
  160. $endPoint,  
  161. array('foo' => 'bar', 'baz' => 'fab'),  
  162. array(),  
  163. 'POST' 
  164. ); 
  165.  
  166. $response = json_decode($response, true); 
  167.  
  168. $this->assertSame('httpbin.org', $response['headers']['Host']); 
  169.  
  170. /** 
  171. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  172. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  173. */ 
  174. public function testRetrieveResponsePostRequestWithRequestBodyAsString() 
  175. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  176. $endPoint->expects($this->any()) 
  177. ->method('getHost') 
  178. ->will($this->returnValue('httpbin.org')); 
  179. $endPoint->expects($this->any()) 
  180. ->method('getAbsoluteUri') 
  181. ->will($this->returnValue('http://httpbin.org/post')); 
  182.  
  183. $client = new StreamClient(); 
  184.  
  185. $response = $client->retrieveResponse( 
  186. $endPoint,  
  187. 'foo',  
  188. array(),  
  189. 'POST' 
  190. ); 
  191.  
  192. $response = json_decode($response, true); 
  193.  
  194. $this->assertSame('foo', $response['data']); 
  195.  
  196. /** 
  197. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  198. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  199. */ 
  200. public function testRetrieveResponsePutRequestWithRequestBodyAsString() 
  201. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  202. $endPoint->expects($this->any()) 
  203. ->method('getHost') 
  204. ->will($this->returnValue('httpbin.org')); 
  205. $endPoint->expects($this->any()) 
  206. ->method('getAbsoluteUri') 
  207. ->will($this->returnValue('http://httpbin.org/put')); 
  208.  
  209. $client = new StreamClient(); 
  210.  
  211. $response = $client->retrieveResponse( 
  212. $endPoint,  
  213. 'foo',  
  214. array(),  
  215. 'PUT' 
  216. ); 
  217.  
  218. $response = json_decode($response, true); 
  219.  
  220. $this->assertSame('foo', $response['data']); 
  221.  
  222. /** 
  223. * @covers OAuth\Common\Http\Client\StreamClient::retrieveResponse 
  224. * @covers OAuth\Common\Http\Client\StreamClient::generateStreamContext 
  225. */ 
  226. public function testRetrieveResponseThrowsExceptionOnInvalidRequest() 
  227. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  228.  
  229. $endPoint = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'); 
  230. $endPoint->expects($this->any()) 
  231. ->method('getHost') 
  232. ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre')); 
  233. $endPoint->expects($this->any()) 
  234. ->method('getAbsoluteUri') 
  235. ->will($this->returnValue('dskjhfckjhekrsfhkehfkreljfrekljfkre')); 
  236.  
  237. $client = new StreamClient(); 
  238.  
  239. $response = $client->retrieveResponse( 
  240. $endPoint,  
  241. '',  
  242. array('Content-Type' => 'foo/bar'),  
  243. 'get' 
  244. ); 
  245.  
  246. $response = json_decode($response, true); 
  247.  
  248. $this->assertSame('foo/bar', $response['headers']['Content-Type']);