UserAgentTest.php 7.4 KB

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