RestTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /*
  3. * Copyright 2011 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. class RestTest extends BaseTest
  18. {
  19. /**
  20. * @var Google_Http_REST $rest
  21. */
  22. private $rest;
  23. public function setUp()
  24. {
  25. $this->rest = new Google_Http_REST();
  26. }
  27. public function testDecodeResponse()
  28. {
  29. $url = 'http://localhost';
  30. $client = $this->getClient();
  31. $response = new Google_Http_Request($url);
  32. $response->setResponseHttpCode(204);
  33. $decoded = $this->rest->decodeHttpResponse($response, $client);
  34. $this->assertEquals(null, $decoded);
  35. foreach (array(200, 201) as $code) {
  36. $headers = array('foo', 'bar');
  37. $response = new Google_Http_Request($url, 'GET', $headers);
  38. $response->setResponseBody('{"a": 1}');
  39. $response->setResponseHttpCode($code);
  40. $decoded = $this->rest->decodeHttpResponse($response, $client);
  41. $this->assertEquals(array("a" => 1), $decoded);
  42. }
  43. $response = new Google_Http_Request($url);
  44. $response->setResponseHttpCode(500);
  45. $error = "";
  46. try {
  47. $this->rest->decodeHttpResponse($response, $client);
  48. } catch (Exception $e) {
  49. $error = $e->getMessage();
  50. }
  51. $this->assertEquals(trim($error), "Error calling GET http://localhost: (500)");
  52. }
  53. public function testDecodeEmptyResponse()
  54. {
  55. $url = 'http://localhost';
  56. $response = new Google_Http_Request($url, 'GET', array());
  57. $response->setResponseBody('{}');
  58. $response->setResponseHttpCode(200);
  59. $decoded = $this->rest->decodeHttpResponse($response);
  60. $this->assertEquals(array(), $decoded);
  61. }
  62. public function testCreateRequestUri()
  63. {
  64. $basePath = "http://localhost";
  65. $restPath = "/plus/{u}";
  66. // Test Path
  67. $params = array();
  68. $params['u']['type'] = 'string';
  69. $params['u']['location'] = 'path';
  70. $params['u']['value'] = 'me';
  71. $value = $this->rest->createRequestUri($basePath, $restPath, $params);
  72. $this->assertEquals("http://localhost/plus/me", $value);
  73. // Test Query
  74. $params = array();
  75. $params['u']['type'] = 'string';
  76. $params['u']['location'] = 'query';
  77. $params['u']['value'] = 'me';
  78. $value = $this->rest->createRequestUri($basePath, '/plus', $params);
  79. $this->assertEquals("http://localhost/plus?u=me", $value);
  80. // Test Booleans
  81. $params = array();
  82. $params['u']['type'] = 'boolean';
  83. $params['u']['location'] = 'path';
  84. $params['u']['value'] = '1';
  85. $value = $this->rest->createRequestUri($basePath, $restPath, $params);
  86. $this->assertEquals("http://localhost/plus/true", $value);
  87. $params['u']['location'] = 'query';
  88. $value = $this->rest->createRequestUri($basePath, '/plus', $params);
  89. $this->assertEquals("http://localhost/plus?u=true", $value);
  90. // Test encoding
  91. $params = array();
  92. $params['u']['type'] = 'string';
  93. $params['u']['location'] = 'query';
  94. $params['u']['value'] = '@me/';
  95. $value = $this->rest->createRequestUri($basePath, '/plus', $params);
  96. $this->assertEquals("http://localhost/plus?u=%40me%2F", $value);
  97. }
  98. /**
  99. * @expectedException Google_Service_Exception
  100. */
  101. public function testBadErrorFormatting()
  102. {
  103. $request = new Google_Http_Request("/a/b");
  104. $request->setResponseHttpCode(500);
  105. $request->setResponseBody(
  106. '{
  107. "error": {
  108. "code": 500,
  109. "message": null
  110. }
  111. }'
  112. );
  113. Google_Http_Rest::decodeHttpResponse($request);
  114. }
  115. /**
  116. * @expectedException Google_Service_Exception
  117. */
  118. public function tesProperErrorFormatting()
  119. {
  120. $request = new Google_Http_Request("/a/b");
  121. $request->setResponseHttpCode(401);
  122. $request->setResponseBody(
  123. '{
  124. error: {
  125. errors: [
  126. {
  127. "domain": "global",
  128. "reason": "authError",
  129. "message": "Invalid Credentials",
  130. "locationType": "header",
  131. "location": "Authorization",
  132. }
  133. ],
  134. "code": 401,
  135. "message": "Invalid Credentials"
  136. }'
  137. );
  138. Google_Http_Rest::decodeHttpResponse($request);
  139. }
  140. }