| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request;
- use PHPUnit\Framework\TestCase;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Request\UserAgent;
- use AlibabaCloud\Client\Request\RpcRequest;
- use AlibabaCloud\Client\Exception\ClientException;
- /**
- * Class UserAgentTest
- */
- class UserAgentTest extends TestCase
- {
- public static function testUserAgentString()
- {
- $userAgent = UserAgent::toString();
- self::assertStringStartsWith('AlibabaCloud', $userAgent);
- }
- public static function testUserAgentStringWithAppend()
- {
- $userAgent = UserAgent::toString([
- 'Append' => '1.0.0',
- 'Append2' => '2.0.0',
- 'PHP' => '2.0.0',
- ]);
- self::assertStringStartsWith('AlibabaCloud', $userAgent);
- self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', $userAgent);
- }
- public static function testUserAgentAppend()
- {
- UserAgent::append('Append', '1.0.0');
- $userAgent = UserAgent::toString();
- self::assertStringEndsWith('Append/1.0.0', $userAgent);
- }
- public static function testUserAgentWith()
- {
- UserAgent::with([
- 'Append' => '1.0.0',
- 'Append2' => '2.0.0',
- ]);
- $userAgent = UserAgent::toString();
- self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', $userAgent);
- }
- /**
- * @throws ClientException
- */
- public static function testGuard()
- {
- UserAgent::append('PHP', '7.3');
- self::assertStringEndsNotWith('PHP/7.3', UserAgent::toString());
- UserAgent::append('Client', '1.0.0');
- self::assertStringEndsNotWith('Client/1.0.0', UserAgent::toString());
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name cannot be empty
- */
- public static function testGuardWithNameEmpty()
- {
- UserAgent::append('', '7.3');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name must be a string
- */
- public static function testGuardWithNameFormat()
- {
- UserAgent::append(null, '7.3');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value cannot be empty
- */
- public static function testGuardWithValueEmpty()
- {
- UserAgent::append('PHP', '');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value must be a string
- */
- public static function testGuardWithValueFormat()
- {
- UserAgent::append('PHP', null);
- }
- /**
- * @throws ClientException
- */
- public static function testAppendGlobalUserAgent()
- {
- AlibabaCloud::appendUserAgent('cli', '1.0.0');
- AlibabaCloud::appendUserAgent('cmp', 'fit2cloud');
- self::assertStringEndsWith('cli/1.0.0 cmp/fit2cloud', UserAgent::toString());
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name cannot be empty
- */
- public static function testAppendUserAgentWithNameEmpty()
- {
- AlibabaCloud::appendUserAgent('', '1.0.0');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name must be a string
- */
- public static function testAppendUserAgentWithNameFormat()
- {
- AlibabaCloud::appendUserAgent(null, '1.0.0');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value cannot be empty
- */
- public static function testAppendUserAgentWithValueEmpty()
- {
- AlibabaCloud::appendUserAgent('cli', '');
- }
- /**
- * @throws ClientException
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value must be a string
- */
- public static function testAppendUserAgentWithValueFormat()
- {
- AlibabaCloud::appendUserAgent('cli', null);
- }
- public static function testWithGlobalUserAgent()
- {
- AlibabaCloud::withUserAgent([
- 'Append' => '1.0.0',
- 'Append2' => '2.0.0',
- ]);
- self::assertStringEndsWith('Append/1.0.0 Append2/2.0.0', UserAgent::toString());
- AlibabaCloud::withUserAgent([]);
- self::assertStringEndsWith('PHP/' . PHP_VERSION, UserAgent::toString());
- }
- /**
- * @throws ClientException
- */
- public static function testAppendForRequest()
- {
- AlibabaCloud::appendUserAgent('cli', '1.0.0');
- $request = new RpcRequest();
- $request->appendUserAgent('cmp', 'fit2cloud');
- // Execution request to get UA information, expected exception.
- try {
- $request->request();
- } catch (\Exception $exception) {
- }
- self::assertStringEndsWith('cli/1.0.0 cmp/fit2cloud', $request->options['headers']['User-Agent']);
- self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
- }
- /**
- * @throws ClientException
- */
- public static function testWithForRequest()
- {
- AlibabaCloud::appendUserAgent('cli', '1.0.0');
- $request = new RpcRequest();
- $request->withUserAgent([
- 'Append' => '1.0.0',
- 'Append2' => '2.0.0',
- ]);
- // Execution request to get UA information, expected exception.
- try {
- $request->request();
- } catch (\Exception $exception) {
- }
- self::assertStringEndsWith(
- 'cli/1.0.0 Append/1.0.0 Append2/2.0.0',
- $request->options['headers']['User-Agent']
- );
- self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
- }
- /**
- * @throws ClientException
- */
- public static function testRequestFirst()
- {
- AlibabaCloud::appendUserAgent('cli', '1.0.0');
- $request = new RpcRequest();
- $request->withUserAgent([
- 'Append' => '1.0.0',
- 'cli' => '2.0.0',
- ]);
- // Execution request to get UA information, expected exception.
- try {
- $request->request();
- } catch (\Exception $exception) {
- }
- self::assertStringEndsWith(
- 'cli/2.0.0 Append/1.0.0',
- $request->options['headers']['User-Agent']
- );
- self::assertStringEndsWith('cli/1.0.0', UserAgent::toString());
- }
- public static function testNull()
- {
- AlibabaCloud::withUserAgent([
- 'Append' => null,
- ]);
- self::assertStringEndsWith('Append', UserAgent::toString());
- }
- protected function tearDown()
- {
- UserAgent::clear();
- }
- }
|