IoTest

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.

Defined (1)

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

/vendor/google/apiclient/tests/general/IoTest.php  
  1. class IoTest extends BaseTest 
  2.  
  3. public function testExecutorSelection() 
  4. $default = function_exists('curl_version') ? 'Google_IO_Curl' : 'Google_IO_Stream'; 
  5. $client = $this->getClient(); 
  6. $this->assertInstanceOf($default, $client->getIo()); 
  7. $config = new Google_Config(); 
  8. $config->setIoClass('Google_IO_Stream'); 
  9. $client = new Google_Client($config); 
  10. $this->assertInstanceOf('Google_IO_Stream', $client->getIo()); 
  11.  
  12. public function testStreamSetTimeout() 
  13. $io = new Google_IO_Stream($this->getClient()); 
  14. $this->timeoutChecker($io); 
  15.  
  16. public function testStreamParseHttpResponseBody() 
  17. $io = new Google_IO_Stream($this->getClient()); 
  18. $this->responseChecker($io); 
  19.  
  20. public function testStreamProcessEntityRequest() 
  21. $client = $this->getClient(); 
  22. $io = new Google_IO_Stream($client); 
  23. $this->processEntityRequest($io); 
  24.  
  25. public function testStreamCacheHit() 
  26. $client = $this->getClient(); 
  27. $io = new Google_IO_Stream($client); 
  28. $this->cacheHit($io); 
  29.  
  30. public function testStreamAuthCache() 
  31. $client = $this->getClient(); 
  32. $io = new Google_IO_Stream($client); 
  33. $this->authCache($io); 
  34.  
  35. /** 
  36. * @expectedException Google_IO_Exception 
  37. */ 
  38. public function testStreamInvalidRequest() 
  39. $io = new Google_IO_Stream($this->getClient()); 
  40. $this->invalidRequest($io); 
  41.  
  42. public function testCurlSetTimeout() 
  43. if (!function_exists('curl_version')) { 
  44. $this->markTestSkipped('cURL not present'); 
  45. $io = new Google_IO_Curl($this->getClient()); 
  46. $this->timeoutChecker($io); 
  47.  
  48. public function testCurlParseHttpResponseBody() 
  49. if (!function_exists('curl_version')) { 
  50. $this->markTestSkipped('cURL not present'); 
  51. $io = new Google_IO_Curl($this->getClient()); 
  52. $this->responseChecker($io); 
  53.  
  54. public function testCurlProcessEntityRequest() 
  55. if (!function_exists('curl_version')) { 
  56. $this->markTestSkipped('cURL not present'); 
  57. $client = $this->getClient(); 
  58. $io = new Google_IO_Curl($client); 
  59. $this->processEntityRequest($io); 
  60.  
  61. public function testCurlCacheHit() 
  62. if (!function_exists('curl_version')) { 
  63. $this->markTestSkipped('cURL not present'); 
  64. $client = $this->getClient(); 
  65. $io = new Google_IO_Curl($client); 
  66. $this->cacheHit($io); 
  67.  
  68. public function testCurlAuthCache() 
  69. if (!function_exists('curl_version')) { 
  70. $this->markTestSkipped('cURL not present'); 
  71. $client = $this->getClient(); 
  72. $io = new Google_IO_Curl($client); 
  73. $this->authCache($io); 
  74.  
  75. /** 
  76. * @expectedException Google_IO_Exception 
  77. */ 
  78. public function testCurlInvalidRequest() 
  79. if (!function_exists('curl_version')) { 
  80. $this->markTestSkipped('cURL not present'); 
  81. $io = new Google_IO_Curl($this->getClient()); 
  82. $this->invalidRequest($io); 
  83.  
  84. public function testCacheRevalidate() 
  85. $client = $this->getClient(); 
  86.  
  87. $req = new Google_Http_Request('/test', 'GET'); 
  88. $req->setRequestHeaders(array('Accept' => '*/*')); 
  89. $req->setResponseBody('{"a": "foo"}'); 
  90. $req->setResponseHttpCode(200); 
  91. $req->setResponseHeaders( 
  92. array( 
  93. 'cache-control' => 'private',  
  94. 'etag' => '"this-is-an-etag"',  
  95. 'expires' => '-1',  
  96. 'date' => 'Sun, 1 Jan 2012 09:00:56 GMT',  
  97. 'content-type' => 'application/json; charset=UTF-8',  
  98. ); 
  99.   
  100. $io = $this->getMockBuilder('Google_IO_Abstract') 
  101. ->setConstructorArgs(array($client)) 
  102. ->setMethods( 
  103. array( 
  104. 'getCachedRequest',  
  105. 'checkMustRevalidateCachedRequest' 
  106. ->getMockForAbstractClass(); 
  107.   
  108. $io->expects($this->once()) 
  109. ->method('getCachedRequest') 
  110. ->will($this->returnValue($req)); 
  111.   
  112. $io->expects($this->once()) 
  113. ->method('checkMustRevalidateCachedRequest') 
  114. ->will($this->returnValue(true)); 
  115.   
  116. $io->expects($this->once()) 
  117. ->method('executeRequest') 
  118. ->will( 
  119. $this->returnValue( 
  120. array( 
  121. '{"a": "foo"}',  
  122. array( 
  123. 'te' => 'gzip',  
  124. 'connection' => 'Keep-Alive, Foo, Bar',  
  125. 'foo' => '123',  
  126. 'keep-alive' => 'timeout=30',  
  127. 'cache-control' => 'private',  
  128. 'eTag' => '"this-is-a-new-etag"',  
  129. "expires" => 'Sun, 22 Jan 2022 09:00:56 GMT',  
  130. 'date' => 'Sun, 1 Jan 2012 09:00:56 GMT',  
  131. 'content-type' => 'application/json; charset=UTF-8',  
  132. ),  
  133. 304 
  134. ); 
  135.   
  136. $res = $io->makeRequest(new Google_Http_Request('/test', 'GET')); 
  137.   
  138. $this->assertEquals('{"a": "foo"}', $res->getResponseBody()); 
  139. $this->assertEquals(200, $res->getResponseHttpCode()); 
  140. $this->assertEquals( 
  141. array( 
  142. 'cache-control' => 'private',  
  143. 'etag' => '"this-is-a-new-etag"',  
  144. "expires" => 'Sun, 22 Jan 2022 09:00:56 GMT',  
  145. 'date' => 'Sun, 1 Jan 2012 09:00:56 GMT',  
  146. 'content-type' => 'application/json; charset=UTF-8',  
  147. ),  
  148. $res->getResponseHeaders() 
  149. ); 
  150.   
  151. // Asserting Functions 
  152.   
  153. public function timeoutChecker($io) 
  154. $this->assertEquals(100, $io->getTimeout()); 
  155. $io->setTimeout(120); 
  156. $this->assertEquals(120, $io->getTimeout()); 
  157.   
  158. public function invalidRequest($io) 
  159. $url = "http://localhost:1"; 
  160. $req = new Google_Http_Request($url, "GET"); 
  161. $io->makeRequest($req); 
  162.   
  163. public function cacheHit($io) 
  164. $url = "http://www.googleapis.com"; 
  165. // Create a cacheable request/response. 
  166. // Should not be revalidated. 
  167. $cacheReq = new Google_Http_Request($url, "GET"); 
  168. $cacheReq->setRequestHeaders( 
  169. array("Accept" => "*/*", ) 
  170. ); 
  171. $cacheReq->setResponseBody("{\"a\": \"foo\"}"); 
  172. $cacheReq->setResponseHttpCode(200); 
  173. $cacheReq->setResponseHeaders( 
  174. array( 
  175. "Cache-Control" => "private",  
  176. "ETag" => "\"this-is-an-etag\"",  
  177. "Expires" => "Sun, 22 Jan 2022 09:00:56 GMT",  
  178. "Date" => "Sun, 1 Jan 2012 09:00:56 GMT",  
  179. "Content-Type" => "application/json; charset=UTF-8",  
  180. ); 
  181.  
  182. // Populate the cache. 
  183. $io->setCachedRequest($cacheReq); 
  184.  
  185. // Execute the same mock request, and expect a cache hit. 
  186. $res = $io->makeRequest(new Google_Http_Request($url, "GET")); 
  187. $this->assertEquals("{\"a\": \"foo\"}", $res->getResponseBody()); 
  188. $this->assertEquals(200, $res->getResponseHttpCode()); 
  189.  
  190. public function authCache($io) 
  191. $url = "http://www.googleapis.com/protected/resource"; 
  192.  
  193. // Create a cacheable request/response, but it should not be cached. 
  194. $cacheReq = new Google_Http_Request($url, "GET"); 
  195. $cacheReq->setRequestHeaders( 
  196. array( 
  197. "Accept" => "*/*",  
  198. "Authorization" => "Bearer Foo" 
  199. ); 
  200. $cacheReq->setResponseBody("{\"a\": \"foo\"}"); 
  201. $cacheReq->setResponseHttpCode(200); 
  202. $cacheReq->setResponseHeaders( 
  203. array( 
  204. "Cache-Control" => "private",  
  205. "ETag" => "\"this-is-an-etag\"",  
  206. "Expires" => "Sun, 22 Jan 2022 09:00:56 GMT",  
  207. "Date: Sun, 1 Jan 2012 09:00:56 GMT",  
  208. "Content-Type" => "application/json; charset=UTF-8",  
  209. ); 
  210.  
  211. $result = $io->setCachedRequest($cacheReq); 
  212. $this->assertFalse($result); 
  213.  
  214. public function responseChecker($io) 
  215. $hasQuirk = false; 
  216. if (function_exists('curl_version')) { 
  217. $curlVer = curl_version(); 
  218. $hasQuirk = $curlVer['version_number'] < Google_IO_Curl::NO_QUIRK_VERSION; 
  219.  
  220. $rawHeaders = "HTTP/1.1 200 OK\r\n" 
  221. . "Expires: Sun, 22 Jan 2012 09:00:56 GMT\r\n" 
  222. . "Date: Sun, 22 Jan 2012 09:00:56 GMT\r\n" 
  223. . "Content-Type: application/json; charset=UTF-8\r\n"; 
  224. $size = strlen($rawHeaders); 
  225. $rawBody = "{}"; 
  226.  
  227. $rawResponse = "$rawHeaders\r\n$rawBody"; 
  228. list($headers, $body) = $io->parseHttpResponse($rawResponse, $size); 
  229. $this->assertEquals(3, sizeof($headers)); 
  230. $this->assertEquals(array(), json_decode($body, true)); 
  231.  
  232. // Test empty bodies. 
  233. $rawResponse = $rawHeaders . "\r\n"; 
  234. list($headers, $body) = $io->parseHttpResponse($rawResponse, $size); 
  235. $this->assertEquals(3, sizeof($headers)); 
  236. $this->assertEquals(null, json_decode($body, true)); 
  237.  
  238. // Test no content. 
  239. $rawerHeaders = "HTTP/1.1 204 No Content\r\n" 
  240. . "Date: Fri, 19 Sep 2014 15:52:14 GMT"; 
  241. list($headers, $body) = $io->parseHttpResponse($rawerHeaders, 0); 
  242. $this->assertEquals(1, sizeof($headers)); 
  243. $this->assertEquals(null, json_decode($body, true)); 
  244.  
  245. // Test transforms from proxies. 
  246. $connection_established_headers = array( 
  247. "HTTP/1.0 200 Connection established\r\n\r\n",  
  248. "HTTP/1.1 200 Connection established\r\n\r\n",  
  249. ); 
  250. foreach ($connection_established_headers as $established_header) { 
  251. $rawHeaders = "{$established_header}HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"; 
  252. $headersSize = strlen($rawHeaders); 
  253. // If we have a broken cURL version we have to simulate it to get the 
  254. // correct test result. 
  255. if ($hasQuirk && get_class($io) === 'Google_IO_Curl') { 
  256. $headersSize -= strlen($established_header); 
  257. $rawBody = "{}"; 
  258.  
  259. $rawResponse = "$rawHeaders\r\n$rawBody"; 
  260. list($headers, $body) = $io->parseHttpResponse($rawResponse, $headersSize); 
  261. $this->assertEquals(1, sizeof($headers)); 
  262. $this->assertEquals(array(), json_decode($body, true)); 
  263.  
  264. public function processEntityRequest($io) 
  265. $req = new Google_Http_Request("http://localhost.com"); 
  266. $req->setRequestMethod("POST"); 
  267.  
  268. // Verify that the content-length is calculated. 
  269. $req->setPostBody("{}"); 
  270. $io->processEntityRequest($req); 
  271. $this->assertEquals(2, $req->getRequestHeader("content-length")); 
  272.  
  273. // Test an empty post body. 
  274. $req->setPostBody(""); 
  275. $io->processEntityRequest($req); 
  276. $this->assertEquals(0, $req->getRequestHeader("content-length")); 
  277.  
  278. // Test a null post body. 
  279. $req->setPostBody(null); 
  280. $io->processEntityRequest($req); 
  281. $this->assertEquals(0, $req->getRequestHeader("content-length")); 
  282.  
  283. // Set an array in the postbody, and verify that it is url-encoded. 
  284. $req->setPostBody(array("a" => "1", "b" => 2)); 
  285. $io->processEntityRequest($req); 
  286. $this->assertEquals(7, $req->getRequestHeader("content-length")); 
  287. $this->assertEquals( 
  288. Google_IO_Abstract::FORM_URLENCODED,  
  289. $req->getRequestHeader("content-type") 
  290. ); 
  291. $this->assertEquals("a=1&b=2", $req->getPostBody()); 
  292.  
  293. // Verify that the content-type isn't reset. 
  294. $payload = array("a" => "1", "b" => 2); 
  295. $req->setPostBody($payload); 
  296. $req->setRequestHeaders(array("content-type" => "multipart/form-data")); 
  297. $io->processEntityRequest($req); 
  298. $this->assertEquals( 
  299. "multipart/form-data",  
  300. $req->getRequestHeader("content-type") 
  301. ); 
  302. $this->assertEquals($payload, $req->getPostBody());