PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/vendor/google-api-php-client/tests/general/IoTest.php

https://github.com/ShinichiU/opCalendarPlugin
PHP | 307 lines | 208 code | 32 blank | 67 comment | 9 complexity | a075981a9418e0e4aec804cf27656a18 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. // Asserting Functions
  123. public function timeoutChecker($io)
  124. {
  125. $this->assertEquals(100, $io->getTimeout());
  126. $io->setTimeout(120);
  127. $this->assertEquals(120, $io->getTimeout());
  128. }
  129. public function invalidRequest($io)
  130. {
  131. $url = "http://localhost:1";
  132. $req = new Google_Http_Request($url, "GET");
  133. $io->makeRequest($req);
  134. }
  135. public function cacheHit($io, $client)
  136. {
  137. $url = "http://www.googleapis.com";
  138. // Create a cacheable request/response.
  139. // Should not be revalidated.
  140. $cacheReq = new Google_Http_Request($url, "GET");
  141. $cacheReq->setRequestHeaders(
  142. array("Accept" => "*/*",)
  143. );
  144. $cacheReq->setResponseBody("{\"a\": \"foo\"}");
  145. $cacheReq->setResponseHttpCode(200);
  146. $cacheReq->setResponseHeaders(
  147. array(
  148. "Cache-Control" => "private",
  149. "ETag" => "\"this-is-an-etag\"",
  150. "Expires" => "Sun, 22 Jan 2022 09:00:56 GMT",
  151. "Date" => "Sun, 1 Jan 2012 09:00:56 GMT",
  152. "Content-Type" => "application/json; charset=UTF-8",
  153. )
  154. );
  155. // Populate the cache.
  156. $io->setCachedRequest($cacheReq);
  157. // Execute the same mock request, and expect a cache hit.
  158. $res = $io->makeRequest(new Google_Http_Request($url, "GET"));
  159. $this->assertEquals("{\"a\": \"foo\"}", $res->getResponseBody());
  160. $this->assertEquals(200, $res->getResponseHttpCode());
  161. }
  162. public function authCache($io, $client)
  163. {
  164. $url = "http://www.googleapis.com/protected/resource";
  165. // Create a cacheable request/response, but it should not be cached.
  166. $cacheReq = new Google_Http_Request($url, "GET");
  167. $cacheReq->setRequestHeaders(
  168. array(
  169. "Accept" => "*/*",
  170. "Authorization" => "Bearer Foo"
  171. )
  172. );
  173. $cacheReq->setResponseBody("{\"a\": \"foo\"}");
  174. $cacheReq->setResponseHttpCode(200);
  175. $cacheReq->setResponseHeaders(
  176. array(
  177. "Cache-Control" => "private",
  178. "ETag" => "\"this-is-an-etag\"",
  179. "Expires" => "Sun, 22 Jan 2022 09:00:56 GMT",
  180. "Date: Sun, 1 Jan 2012 09:00:56 GMT",
  181. "Content-Type" => "application/json; charset=UTF-8",
  182. )
  183. );
  184. $result = $io->setCachedRequest($cacheReq);
  185. $this->assertFalse($result);
  186. }
  187. public function responseChecker($io)
  188. {
  189. $hasQuirk = false;
  190. if (function_exists('curl_version')) {
  191. $curlVer = curl_version();
  192. $hasQuirk = $curlVer['version_number'] < Google_IO_Curl::NO_QUIRK_VERSION;
  193. }
  194. $rawHeaders = "HTTP/1.1 200 OK\r\n"
  195. . "Expires: Sun, 22 Jan 2012 09:00:56 GMT\r\n"
  196. . "Date: Sun, 22 Jan 2012 09:00:56 GMT\r\n"
  197. . "Content-Type: application/json; charset=UTF-8\r\n";
  198. $size = strlen($rawHeaders);
  199. $rawBody = "{}";
  200. $rawResponse = "$rawHeaders\r\n$rawBody";
  201. list($headers, $body) = $io->parseHttpResponse($rawResponse, $size);
  202. $this->assertEquals(3, sizeof($headers));
  203. $this->assertEquals(array(), json_decode($body, true));
  204. // Test empty bodies.
  205. $rawResponse = $rawHeaders . "\r\n";
  206. list($headers, $body) = $io->parseHttpResponse($rawResponse, $size);
  207. $this->assertEquals(3, sizeof($headers));
  208. $this->assertEquals(null, json_decode($body, true));
  209. // Test no content.
  210. $rawerHeaders = "HTTP/1.1 204 No Content\r\n"
  211. . "Date: Fri, 19 Sep 2014 15:52:14 GMT";
  212. list($headers, $body) = $io->parseHttpResponse($rawerHeaders, 0);
  213. $this->assertEquals(1, sizeof($headers));
  214. $this->assertEquals(null, json_decode($body, true));
  215. // Test transforms from proxies.
  216. $connection_established_headers = array(
  217. "HTTP/1.0 200 Connection established\r\n\r\n",
  218. "HTTP/1.1 200 Connection established\r\n\r\n",
  219. );
  220. foreach ($connection_established_headers as $established_header) {
  221. $rawHeaders = "{$established_header}HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n";
  222. $headersSize = strlen($rawHeaders);
  223. // If we have a broken cURL version we have to simulate it to get the
  224. // correct test result.
  225. if ($hasQuirk && get_class($io) === 'Google_IO_Curl') {
  226. $headersSize -= strlen($established_header);
  227. }
  228. $rawBody = "{}";
  229. $rawResponse = "$rawHeaders\r\n$rawBody";
  230. list($headers, $body) = $io->parseHttpResponse($rawResponse, $headersSize);
  231. $this->assertEquals(1, sizeof($headers));
  232. $this->assertEquals(array(), json_decode($body, true));
  233. }
  234. }
  235. public function processEntityRequest($io, $client)
  236. {
  237. $req = new Google_Http_Request("http://localhost.com");
  238. $req->setRequestMethod("POST");
  239. // Verify that the content-length is calculated.
  240. $req->setPostBody("{}");
  241. $io->processEntityRequest($req);
  242. $this->assertEquals(2, $req->getRequestHeader("content-length"));
  243. // Test an empty post body.
  244. $req->setPostBody("");
  245. $io->processEntityRequest($req);
  246. $this->assertEquals(0, $req->getRequestHeader("content-length"));
  247. // Test a null post body.
  248. $req->setPostBody(null);
  249. $io->processEntityRequest($req);
  250. $this->assertEquals(0, $req->getRequestHeader("content-length"));
  251. // Set an array in the postbody, and verify that it is url-encoded.
  252. $req->setPostBody(array("a" => "1", "b" => 2));
  253. $io->processEntityRequest($req);
  254. $this->assertEquals(7, $req->getRequestHeader("content-length"));
  255. $this->assertEquals(
  256. Google_IO_Abstract::FORM_URLENCODED,
  257. $req->getRequestHeader("content-type")
  258. );
  259. $this->assertEquals("a=1&b=2", $req->getPostBody());
  260. // Verify that the content-type isn't reset.
  261. $payload = array("a" => "1", "b" => 2);
  262. $req->setPostBody($payload);
  263. $req->setRequestHeaders(array("content-type" => "multipart/form-data"));
  264. $io->processEntityRequest($req);
  265. $this->assertEquals(
  266. "multipart/form-data",
  267. $req->getRequestHeader("content-type")
  268. );
  269. $this->assertEquals($payload, $req->getPostBody());
  270. }
  271. }