Skip to content

Commit 5145cf5

Browse files
author
宋神宗
authored
release 1.3.0 (#139)
1 parent c3ddbd0 commit 5145cf5

File tree

5 files changed

+153
-13
lines changed

5 files changed

+153
-13
lines changed

src/Request/RoaRequest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,15 @@ private function resolveContentType()
174174
*/
175175
private function resolveSecurityToken()
176176
{
177-
if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
178-
$this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
177+
if (!$this->credential() instanceof StsCredential) {
178+
return;
179179
}
180+
181+
if (!$this->credential()->getSecurityToken()) {
182+
return;
183+
}
184+
185+
$this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
180186
}
181187

182188
/**
@@ -202,16 +208,16 @@ private function signature()
202208
/**
203209
* @var AccessKeyCredential $credential
204210
*/
205-
$credential = $this->credential();
206-
$accessKeyId = $credential->getAccessKeyId();
207-
$signature = $this->httpClient()
208-
->getSignature()
209-
->sign(
210-
$this->stringToSign(),
211-
$credential->getAccessKeySecret()
212-
);
213-
214-
return "acs $accessKeyId:$signature";
211+
$credential = $this->credential();
212+
$access_key_id = $credential->getAccessKeyId();
213+
$signature = $this->httpClient()
214+
->getSignature()
215+
->sign(
216+
$this->stringToSign(),
217+
$credential->getAccessKeySecret()
218+
);
219+
220+
return "acs $access_key_id:$signature";
215221
}
216222

217223
/**

tests/Unit/Filter/HttpFilterTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace AlibabaCloud\Client\Tests\Unit\Filter;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use AlibabaCloud\Client\Filter\HttpFilter;
7+
use AlibabaCloud\Client\Exception\ClientException;
8+
9+
class HttpFilterTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider contentType
13+
*
14+
* @param $expected
15+
* @param $contentType
16+
* @param $exceptionMessage
17+
*/
18+
public function testContentType($expected, $contentType, $exceptionMessage)
19+
{
20+
try {
21+
self::assertEquals($expected, HttpFilter::contentType($contentType));
22+
} catch (ClientException $exception) {
23+
self::assertEquals($exceptionMessage, $exception->getMessage());
24+
}
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function contentType()
31+
{
32+
return [
33+
[
34+
'',
35+
'',
36+
'Content-Type cannot be empty',
37+
],
38+
[
39+
1,
40+
1,
41+
'Content-Type must be a string',
42+
],
43+
[
44+
'a',
45+
'a',
46+
'a',
47+
],
48+
];
49+
}
50+
51+
52+
/**
53+
* @dataProvider accept
54+
*
55+
* @param $expected
56+
* @param $contentType
57+
* @param $exceptionMessage
58+
*/
59+
public function testAccept($expected, $contentType, $exceptionMessage)
60+
{
61+
try {
62+
self::assertEquals($expected, HttpFilter::accept($contentType));
63+
} catch (ClientException $exception) {
64+
self::assertEquals($exceptionMessage, $exception->getMessage());
65+
}
66+
}
67+
68+
69+
/**
70+
* @return array
71+
*/
72+
public function accept()
73+
{
74+
return [
75+
[
76+
'',
77+
'',
78+
'Accept cannot be empty',
79+
],
80+
[
81+
1,
82+
1,
83+
'Accept must be a string',
84+
],
85+
[
86+
'a',
87+
'a',
88+
'a',
89+
],
90+
];
91+
}
92+
}

tests/Unit/Request/RoaRequestTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,25 @@ public function testResolveSecurityToken()
456456
// Assert
457457
self::assertEquals('token', $request->options['headers']['x-acs-security-token']);
458458
}
459+
460+
/**
461+
* @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
462+
* @throws ReflectionException
463+
* @throws ClientException
464+
*/
465+
public function testNoSecurityToken()
466+
{
467+
// Setup
468+
$request = AlibabaCloud::roa();
469+
$object = new ReflectionObject($request);
470+
AlibabaCloud::stsClient('foo', 'bar')->asDefaultClient();
471+
472+
// Test
473+
$method = $object->getMethod('resolveSecurityToken');
474+
$method->setAccessible(true);
475+
$method->invoke($request);
476+
477+
// Assert
478+
self::assertFalse(isset($request->options['headers']['x-acs-security-token']));
479+
}
459480
}

tests/Unit/Request/RpcRequestTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,25 @@ public function testResolveSecurityToken()
295295
// Assert
296296
self::assertEquals('token', $request->options['query']['SecurityToken']);
297297
}
298+
299+
/**
300+
* @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
301+
* @throws ReflectionException
302+
* @throws ClientException
303+
*/
304+
public function testNoSecurityToken()
305+
{
306+
// Setup
307+
$request = AlibabaCloud::rpc();
308+
$object = new ReflectionObject($request);
309+
AlibabaCloud::stsClient('foo', 'bar')->asDefaultClient();
310+
311+
// Test
312+
$method = $object->getMethod('resolveSecurityToken');
313+
$method->setAccessible(true);
314+
$method->invoke($request);
315+
316+
// Assert
317+
self::assertFalse(isset($request->options['query']['SecurityToken']));
318+
}
298319
}

tests/Unit/Request/Traits/ClientTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testHttpClientWithCustomChain()
5656
$name = 'testHttpClientWithCustomChain';
5757
AlibabaCloud::flush();
5858
CredentialsProvider::chain(
59-
static function() use ($name) {
59+
static function () use ($name) {
6060
AlibabaCloud::ecsRamRoleClient('role')->name($name);
6161
}
6262
);

0 commit comments

Comments
 (0)