RoaRequestTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request;
  3. use AlibabaCloud\Client\Accept;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Credentials\BearerTokenCredential;
  6. use AlibabaCloud\Client\Credentials\StsCredential;
  7. use AlibabaCloud\Client\Encode;
  8. use AlibabaCloud\Client\Exception\ClientException;
  9. use AlibabaCloud\Client\Request\RoaRequest;
  10. use AlibabaCloud\Client\Support\Path;
  11. use AlibabaCloud\Client\Support\Sign;
  12. use AlibabaCloud\Client\Tests\Mock\Services\CS\DescribeClusterServicesRequest;
  13. use GuzzleHttp\Psr7\Request;
  14. use PHPUnit\Framework\TestCase;
  15. use ReflectionException;
  16. use ReflectionMethod;
  17. use ReflectionObject;
  18. use RuntimeException;
  19. use AlibabaCloud\Client\Support\Stringy;
  20. /**
  21. * Class RoaRequestTest
  22. *
  23. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request
  24. *
  25. * @coversDefaultClass \AlibabaCloud\Client\Request\RoaRequest
  26. */
  27. class RoaRequestTest extends TestCase
  28. {
  29. /**
  30. * @throws ReflectionException
  31. * @throws ClientException
  32. */
  33. public function testAssignPathParametersWithMagicMethod()
  34. {
  35. // Setup
  36. $request = new DescribeClusterServicesRequest();
  37. $clusterId = \time();
  38. $expected = '/clusters/' . $clusterId . '/services';
  39. // Test
  40. $request->withClusterId($clusterId);
  41. $method = new ReflectionMethod(
  42. Path::class,
  43. 'assign'
  44. );
  45. $actual = $method->invokeArgs($request, [$request->pathPattern, $request->pathParameters]);
  46. // Assert
  47. self::assertEquals($expected, $actual);
  48. }
  49. /**
  50. * @throws ReflectionException
  51. * @throws ClientException
  52. */
  53. public function testAssignPathParametersWithOption()
  54. {
  55. // Setup
  56. $request = new DescribeClusterServicesRequest();
  57. $clusterId = \time();
  58. $expected = '/clusters/' . $clusterId . '/services';
  59. // Test
  60. $request->pathParameter('ClusterId', $clusterId);
  61. $method = new ReflectionMethod(
  62. Path::class,
  63. 'assign'
  64. );
  65. $actual = $method->invokeArgs($request, [$request->pathPattern, $request->pathParameters]);
  66. // Assert
  67. self::assertEquals($expected, $actual);
  68. }
  69. /**
  70. * @throws ReflectionException
  71. * @throws ClientException
  72. */
  73. public function testConstructAcsHeader()
  74. {
  75. // Setup
  76. $request = new DescribeClusterServicesRequest();
  77. $request->withClusterId(\time());
  78. $request->regionId('cn-hangzhou');
  79. $clusterId = \time();
  80. $request->resolveParameter();
  81. $expected = "x-acs-region-id:cn-hangzhou\n" .
  82. "x-acs-signature-method:HMAC-SHA1\n";
  83. // Test
  84. $request->pathParameter('ClusterId', $clusterId);
  85. $method = new ReflectionMethod(
  86. Sign::class,
  87. 'acsHeaderString'
  88. );
  89. $method->setAccessible(true);
  90. $requestPsr = new Request(
  91. $request->method,
  92. $request->uri,
  93. $request->getHeaders()
  94. );
  95. $actual = $method->invokeArgs($request, [$requestPsr->getHeaders()]);
  96. // Assert
  97. self::assertTrue(Stringy::contains($actual, $expected));
  98. }
  99. /**
  100. * @param array $query
  101. * @param string $expected
  102. *
  103. * @dataProvider buildQueryString
  104. * @throws ClientException
  105. */
  106. public function testBuildQueryString(array $query, $expected)
  107. {
  108. // Setup
  109. $request = new DescribeClusterServicesRequest();
  110. AlibabaCloud::accessKeyClient('foo', 'bar')
  111. ->regionId('cn-hangzhou')
  112. ->asDefaultClient();
  113. // Test
  114. $request->options(['query' => $query]);
  115. $request->resolveParameter();
  116. // Assert
  117. self::assertEquals($expected, Encode::create($request->options['query'])->ksort()->toString());
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function buildQueryString()
  123. {
  124. return [
  125. [
  126. [
  127. 'a' => 'a',
  128. 'b' => 'b',
  129. ],
  130. 'Version=2015-12-15&a=a&b=b',
  131. ],
  132. [
  133. [
  134. 'b' => 'b',
  135. 'c' => 'c',
  136. ],
  137. 'Version=2015-12-15&b=b&c=c',
  138. ],
  139. [
  140. [
  141. 'b' => 'b',
  142. 'c' => 'c',
  143. 'd' => '',
  144. ],
  145. 'Version=2015-12-15&b=b&c=c&d',
  146. ],
  147. ];
  148. }
  149. /**
  150. * @param $format
  151. * @param $expected
  152. *
  153. * @dataProvider contentType
  154. */
  155. public function testContentType($format, $expected)
  156. {
  157. self::assertEquals($expected, Accept::create($format)->toString());
  158. }
  159. /**
  160. * @return array
  161. */
  162. public function contentType()
  163. {
  164. return [
  165. ['JSON', 'application/json',],
  166. ['XML', 'application/xml',],
  167. ['RAW', 'application/octet-stream',],
  168. ['NON', 'application/octet-stream',],
  169. ['FORM', 'application/x-www-form-urlencoded',],
  170. ];
  171. }
  172. /**
  173. * @param string $key
  174. * @param string $value
  175. *
  176. * @dataProvider pathParameter
  177. * @throws ClientException
  178. */
  179. public function testPathParameter($key, $value)
  180. {
  181. // Setup
  182. $request = new DescribeClusterServicesRequest();
  183. // Test
  184. $request->pathParameter($key, $value);
  185. // Assert
  186. self::assertEquals($value, $request->pathParameters[$key]);
  187. }
  188. /**
  189. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  190. * @expectedExceptionMessage Name cannot be empty
  191. * @throws ClientException
  192. */
  193. public function testPathParameterWithNameEmpty()
  194. {
  195. // Setup
  196. $request = new DescribeClusterServicesRequest();
  197. // Test
  198. $request->pathParameter('', 'value');
  199. }
  200. /**
  201. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  202. * @expectedExceptionMessage Name must be a string
  203. * @throws ClientException
  204. */
  205. public function testPathParameterWithNameFormat()
  206. {
  207. // Setup
  208. $request = new DescribeClusterServicesRequest();
  209. // Test
  210. $request->pathParameter(null, 'value');
  211. }
  212. /**
  213. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  214. * @expectedExceptionMessage Value cannot be empty
  215. * @throws ClientException
  216. */
  217. public function testPathParameterWithValueEmpty()
  218. {
  219. // Setup
  220. $request = new DescribeClusterServicesRequest();
  221. // Test
  222. $request->pathParameter('name', '');
  223. }
  224. /**
  225. * @return array
  226. */
  227. public function pathParameter()
  228. {
  229. return [
  230. ['1', '1'],
  231. ['2', '2'],
  232. ['3', '3'],
  233. ['4', '4'],
  234. ];
  235. }
  236. /**
  237. * @param $pattern
  238. *
  239. * @dataProvider pathPattern
  240. * @throws ClientException
  241. */
  242. public function testPathPattern($pattern)
  243. {
  244. // Setup
  245. $request = new DescribeClusterServicesRequest();
  246. // Test
  247. $request->pathPattern($pattern);
  248. // Assert
  249. self::assertEquals($pattern, $request->pathPattern);
  250. }
  251. /**
  252. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  253. * @expectedExceptionMessage Pattern cannot be empty
  254. * @throws ClientException
  255. */
  256. public function testPathPatternWithEmpty()
  257. {
  258. // Setup
  259. $request = new DescribeClusterServicesRequest();
  260. // Test
  261. $request->pathPattern('');
  262. }
  263. /**
  264. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  265. * @expectedExceptionMessage Pattern must be a string
  266. * @throws ClientException
  267. */
  268. public function testPathPatternWithFormat()
  269. {
  270. // Setup
  271. $request = new DescribeClusterServicesRequest();
  272. // Test
  273. $request->pathPattern(null);
  274. }
  275. /**
  276. * @return array
  277. */
  278. public function pathPattern()
  279. {
  280. return [
  281. ['1'],
  282. ['2'],
  283. ['3'],
  284. ['4'],
  285. ];
  286. }
  287. /**
  288. * @param $version
  289. *
  290. * @dataProvider version
  291. * @throws ClientException
  292. */
  293. public function testVersion($version)
  294. {
  295. // Setup
  296. $request = new DescribeClusterServicesRequest();
  297. AlibabaCloud::accessKeyClient('foo', 'bar')
  298. ->regionId('cn-hangzhou')
  299. ->asDefaultClient();
  300. // Test
  301. $request->version($version);
  302. $request->resolveParameter();
  303. // Assert
  304. self::assertEquals($version, $request->version);
  305. self::assertEquals($version, $request->options['query']['Version']);
  306. self::assertEquals($version, $request->options['headers']['x-acs-version']);
  307. }
  308. /**
  309. * @return array
  310. */
  311. public function version()
  312. {
  313. return [
  314. ['1'],
  315. ['2'],
  316. ['3'],
  317. ['4'],
  318. ];
  319. }
  320. /**
  321. * @return array
  322. * @throws ClientException
  323. */
  324. public function resolveQuery()
  325. {
  326. return [
  327. [
  328. new StsCredential('foo', 'bar', 'token'),
  329. ],
  330. [
  331. new BearerTokenCredential('token'),
  332. ],
  333. ];
  334. }
  335. /**
  336. * @throws ClientException
  337. * @dataProvider resolveQuery
  338. */
  339. public function testResolveParameters()
  340. {
  341. // Setup
  342. AlibabaCloud::bearerTokenClient('token')->name('token');
  343. $clusterId = time();
  344. $request = new DescribeClusterServicesRequest();
  345. $request->withClusterId($clusterId);
  346. $request->client('token');
  347. $request->options(
  348. [
  349. 'form_params' => [
  350. 'test' => 'test',
  351. ],
  352. ]
  353. );
  354. $request->regionId('cn-hangzhou');
  355. $request->method('post');
  356. $request->options(
  357. [
  358. 'query' => [
  359. 'A' => 'A',
  360. ],
  361. ]
  362. );
  363. $expected = "http://localhost/clusters/{$clusterId}/services?A=A&Version=2015-12-15";
  364. // Test
  365. $request->resolveParameter();
  366. // Assert
  367. self::assertEquals($expected, (string)$request->uri);
  368. }
  369. /**
  370. * @throws ClientException
  371. */
  372. public function testCall()
  373. {
  374. $request = new RoaRequest();
  375. self::assertEquals([], $request->pathParameters);
  376. $request->withPrefix('with');
  377. self::assertEquals('with', $request->getPrefix());
  378. self::assertEquals(['Prefix' => 'with',], $request->pathParameters);
  379. $request->withprefix('with');
  380. self::assertEquals('with', $request->getprefix());
  381. self::assertEquals(['Prefix' => 'with', 'prefix' => 'with',], $request->pathParameters);
  382. }
  383. /**
  384. * @expectedException RuntimeException
  385. * @expectedExceptionMessage Call to undefined method AlibabaCloud\Client\Request\RoaRequest::nowithvalue()
  386. * @throws ClientException
  387. */
  388. public function testCallException()
  389. {
  390. $request = new RoaRequest();
  391. $request->nowithvalue('value');
  392. }
  393. /**
  394. * @expectedException RuntimeException
  395. * @expectedExceptionMessage Please use withParameter instead of setParameter
  396. * @throws ClientException
  397. */
  398. public function testExceptionWithSet()
  399. {
  400. $request = AlibabaCloud::roa();
  401. $request->setParameter();
  402. }
  403. /**
  404. * @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
  405. * @throws ReflectionException
  406. * @throws ClientException
  407. */
  408. public function testResolveSecurityToken()
  409. {
  410. // Setup
  411. $request = AlibabaCloud::roa();
  412. $object = new ReflectionObject($request);
  413. AlibabaCloud::stsClient('foo', 'bar', 'token')->asDefaultClient();
  414. // Test
  415. $method = $object->getMethod('resolveSecurityToken');
  416. $method->setAccessible(true);
  417. $method->invoke($request);
  418. // Assert
  419. self::assertEquals('token', $request->options['headers']['x-acs-security-token']);
  420. }
  421. /**
  422. * @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
  423. * @throws ReflectionException
  424. * @throws ClientException
  425. */
  426. public function testNoSecurityToken()
  427. {
  428. // Setup
  429. $request = AlibabaCloud::roa();
  430. $object = new ReflectionObject($request);
  431. AlibabaCloud::stsClient('foo', 'bar')->asDefaultClient();
  432. // Test
  433. $method = $object->getMethod('resolveSecurityToken');
  434. $method->setAccessible(true);
  435. $method->invoke($request);
  436. // Assert
  437. self::assertFalse(isset($request->options['headers']['x-acs-security-token']));
  438. }
  439. }