PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/general/IoTest.php

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