UserAgentTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Request;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Request\UserAgent;
  6. use AlibabaCloud\Client\Request\RpcRequest;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. /**
  9. * Class UserAgentTest
  10. */
  11. class UserAgentTest extends TestCase
  12. {
  13. /**
  14. * @after
  15. */
  16. protected function finalize()
  17. {
  18. parent::tearDown();
  19. UserAgent::clear();
  20. }
  21. public static function testUserAgentString()
  22. {
  23. $userAgent = UserAgent::toString();
  24. self::assertStringStartsWith('AlibabaCloud', $userAgent);
  25. }
  26. public static function testUserAgentStringWithAppend()
  27. {
  28. $userAgent = UserAgent::toString([
  29. 'Append' => '1.0.0',
  30. 'Append2' => '2.0.0',
  31. 'PHP' => '2.0.0',
  32. ]);
  33. self::assertStringStartsWith('AlibabaCloud', $userAgent);
  34. self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', $userAgent);
  35. }
  36. public static function testUserAgentAppend()
  37. {
  38. UserAgent::append('Append', '1.0.0');
  39. $userAgent = UserAgent::toString();
  40. self::assertStringEndsWith('Append/1.0.0', $userAgent);
  41. }
  42. public static function testUserAgentWith()
  43. {
  44. UserAgent::with([
  45. 'Append' => '1.0.0',
  46. 'Append2' => '2.0.0',
  47. ]);
  48. $userAgent = UserAgent::toString();
  49. self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', $userAgent);
  50. }
  51. /**
  52. * @throws ClientException
  53. */
  54. public static function testGuard()
  55. {
  56. UserAgent::append('PHP', '7.3');
  57. self::assertStringEndsNotWith('PHP/7.3', UserAgent::toString());
  58. UserAgent::append('Client', '1.0.0');
  59. self::assertStringEndsNotWith('Client/1.0.0', UserAgent::toString());
  60. }
  61. /**
  62. * @throws ClientException
  63. */
  64. public function testGuardWithNameEmpty()
  65. {
  66. $this->expectException(ClientException::class);
  67. $this->expectExceptionMessage("Name cannot be empty");
  68. UserAgent::append('', '7.3');
  69. }
  70. /**
  71. * @throws ClientException
  72. */
  73. public function testGuardWithNameFormat()
  74. {
  75. $this->expectException(ClientException::class);
  76. $this->expectExceptionMessage("Name must be a string");
  77. UserAgent::append(null, '7.3');
  78. }
  79. /**
  80. * @throws ClientException
  81. */
  82. public function testGuardWithValueEmpty()
  83. {
  84. $this->expectException(ClientException::class);
  85. $this->expectExceptionMessage("Value cannot be empty");
  86. UserAgent::append('PHP', '');
  87. }
  88. /**
  89. * @throws ClientException
  90. */
  91. public function testGuardWithValueFormat()
  92. {
  93. $this->expectException(ClientException::class);
  94. $this->expectExceptionMessage("Value must be a string");
  95. UserAgent::append('PHP', null);
  96. }
  97. /**
  98. * @throws ClientException
  99. */
  100. public static function testAppendGlobalUserAgent()
  101. {
  102. AlibabaCloud::appendUserAgent('cli', '1.0.0');
  103. AlibabaCloud::appendUserAgent('cmp', 'fit2cloud');
  104. self::assertStringEndsWith('cli/1.0.0 cmp/fit2cloud', UserAgent::toString());
  105. }
  106. /**
  107. * @throws ClientException
  108. */
  109. public function testAppendUserAgentWithNameEmpty()
  110. {
  111. $this->expectException(ClientException::class);
  112. $this->expectExceptionMessage("Name cannot be empty");
  113. AlibabaCloud::appendUserAgent('', '1.0.0');
  114. }
  115. /**
  116. * @throws ClientException
  117. */
  118. public function testAppendUserAgentWithNameFormat()
  119. {
  120. $this->expectException(ClientException::class);
  121. $this->expectExceptionMessage("Name must be a string");
  122. AlibabaCloud::appendUserAgent(null, '1.0.0');
  123. }
  124. /**
  125. * @throws ClientException
  126. */
  127. public function testAppendUserAgentWithValueEmpty()
  128. {
  129. $this->expectException(ClientException::class);
  130. $this->expectExceptionMessage("Value cannot be empty");
  131. AlibabaCloud::appendUserAgent('cli', '');
  132. }
  133. /**
  134. * @throws ClientException
  135. */
  136. public function testAppendUserAgentWithValueFormat()
  137. {
  138. $this->expectException(ClientException::class);
  139. $this->expectExceptionMessage("Value must be a string");
  140. AlibabaCloud::appendUserAgent('cli', null);
  141. }
  142. public static function testWithGlobalUserAgent()
  143. {
  144. AlibabaCloud::withUserAgent([
  145. 'Append' => '1.0.0',
  146. 'Append2' => '2.0.0',
  147. ]);
  148. self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', UserAgent::toString());
  149. AlibabaCloud::withUserAgent([]);
  150. self::assertStringEndsWith('PHP/' . PHP_VERSION, UserAgent::toString());
  151. }
  152. /**
  153. * @throws ClientException
  154. */
  155. public static function testAppendForRequest()
  156. {
  157. AlibabaCloud::appendUserAgent('cli', '1.0.0');
  158. $request = new RpcRequest();
  159. $request->appendUserAgent('cmp', 'fit2cloud');
  160. // Execution request to get UA information, expected exception.
  161. try {
  162. $request->request();
  163. } catch (\Exception $exception) {
  164. }
  165. self::assertStringEndsWith('cli/1.0.0 cmp/fit2cloud', $request->options['headers']['User-Agent']);
  166. self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
  167. }
  168. /**
  169. * @throws ClientException
  170. */
  171. public static function testWithForRequest()
  172. {
  173. AlibabaCloud::appendUserAgent('cli', '1.0.0');
  174. $request = new RpcRequest();
  175. $request->withUserAgent([
  176. 'Append' => '1.0.0',
  177. 'Append2' => '2.0.0',
  178. ]);
  179. // Execution request to get UA information, expected exception.
  180. try {
  181. $request->request();
  182. } catch (\Exception $exception) {
  183. }
  184. self::assertStringEndsWith(
  185. 'cli/1.0.0 Append/1.0.0 Append2/2.0.0',
  186. $request->options['headers']['User-Agent']
  187. );
  188. self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
  189. }
  190. /**
  191. * @throws ClientException
  192. */
  193. public static function testRequestFirst()
  194. {
  195. AlibabaCloud::appendUserAgent('cli', '1.0.0');
  196. $request = new RpcRequest();
  197. $request->withUserAgent([
  198. 'Append' => '1.0.0',
  199. 'cli' => '2.0.0',
  200. ]);
  201. // Execution request to get UA information, expected exception.
  202. try {
  203. $request->request();
  204. } catch (\Exception $exception) {
  205. }
  206. self::assertStringEndsWith(
  207. 'cli/2.0.0 Append/1.0.0',
  208. $request->options['headers']['User-Agent']
  209. );
  210. self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
  211. }
  212. public static function testNull()
  213. {
  214. AlibabaCloud::withUserAgent([
  215. 'Append' => null,
  216. ]);
  217. self::assertStringEndsWith('Append', UserAgent::toString());
  218. }
  219. }