Skip to content

Commit a9f7eb5

Browse files
authored
validate region_id&suffix&network (#222)
1 parent 1782a72 commit a9f7eb5

File tree

6 files changed

+167
-3
lines changed

6 files changed

+167
-3
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ matrix:
1919
allow_failures:
2020
- php: hhvm-3.24
2121
- php: nightly
22+
- php: 7.4
2223
fast_finish: true
2324

2425
sudo: false
@@ -37,8 +38,8 @@ install:
3738
echo "xdebug.overload_var_dump = 0" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
3839
fi
3940
- >
40-
if { [ $(phpenv version-name) == "7.2" ] || [ $(phpenv version-name) == "7.3" ] || [$(phpenv version-name) == "7.4"]; } && [ -v COMPOSER_OPTS ]; then
41-
composer require --dev phpunit/phpunit "^5.7.11"
41+
if { [ $(phpenv version-name) == "7.2" ] || [ $(phpenv version-name) == "7.3" ] || [ $(phpenv version-name) == "7.4" ] ; } && [ -v COMPOSER_OPTS ]; then
42+
composer require --dev phpunit/phpunit "5.7.27"
4243
fi
4344
# Resolve dependencies
4445
- composer --version

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"drupal/coder": "^8.3",
5050
"symfony/dotenv": "^3.4",
5151
"league/climate": "^3.2.4",
52-
"phpunit/phpunit": "^5.7.27",
52+
"phpunit/phpunit": "5.7.27",
5353
"monolog/monolog": "^1.24",
5454
"composer/composer": "^1.8",
5555
"mikey179/vfsstream": "^1.6",

src/Filter/ApiFilter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public static function endpointSuffix($endpointSuffix)
111111
);
112112
}
113113

114+
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $endpointSuffix)) {
115+
throw new ClientException(
116+
'Invalid Endpoint Suffix',
117+
SDK::INVALID_ARGUMENT
118+
);
119+
}
120+
114121
return $endpointSuffix;
115122
}
116123

@@ -138,6 +145,13 @@ public static function network($network)
138145
);
139146
}
140147

148+
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $network)) {
149+
throw new ClientException(
150+
'Invalid Network Suffix',
151+
SDK::INVALID_ARGUMENT
152+
);
153+
}
154+
141155
return $network;
142156
}
143157

src/Filter/ClientFilter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public static function regionId($regionId)
3535
);
3636
}
3737

38+
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $regionId)) {
39+
throw new ClientException(
40+
'Invalid Region ID',
41+
SDK::INVALID_ARGUMENT
42+
);
43+
}
44+
3845
return strtolower($regionId);
3946
}
4047

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\Filter\ApiFilter;
8+
use AlibabaCloud\Client\Exception\ClientException;
9+
10+
class ApiFilterTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider endpointSuffix
14+
*
15+
* @param $expected
16+
* @param $contentType
17+
* @param $exceptionMessage
18+
*/
19+
public function testEndpointSuffix($expected, $contentType, $exceptionMessage)
20+
{
21+
try {
22+
self::assertEquals($expected, ApiFilter::endpointSuffix($contentType));
23+
} catch (ClientException $exception) {
24+
self::assertEquals($exceptionMessage, $exception->getMessage());
25+
}
26+
}
27+
28+
/**
29+
* @return array
30+
*/
31+
public function endpointSuffix()
32+
{
33+
return [
34+
[
35+
1,
36+
1,
37+
'Endpoint Suffix must be a string',
38+
],
39+
[
40+
'',
41+
'',
42+
'Endpoint Suffix cannot be empty',
43+
],
44+
[
45+
'EndpointSuffix中文',
46+
'EndpointSuffix中文',
47+
'Invalid Endpoint Suffix',
48+
],
49+
[
50+
'suffix',
51+
'suffix',
52+
'',
53+
]
54+
];
55+
}
56+
57+
/**
58+
* @dataProvider network
59+
*
60+
* @param $expected
61+
* @param $contentType
62+
* @param $exceptionMessage
63+
*/
64+
public function testNetwork($expected, $contentType, $exceptionMessage){
65+
try {
66+
self::assertEquals($expected, ApiFilter::network($contentType));
67+
} catch (ClientException $exception) {
68+
self::assertEquals($exceptionMessage, $exception->getMessage());
69+
}
70+
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function network(){
76+
return [
77+
[
78+
1,
79+
1,
80+
'Network Suffix must be a string',
81+
],
82+
[
83+
'',
84+
'',
85+
'Network Suffix cannot be empty',
86+
],
87+
[
88+
'Network中文',
89+
'Network中文',
90+
'Invalid Network Suffix',
91+
],
92+
[
93+
'share',
94+
'share',
95+
'',
96+
]
97+
];
98+
}
99+
}

tests/Unit/Filter/ClientFilterTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,49 @@ public function retry()
4949
];
5050
}
5151

52+
/**
53+
* @dataProvider regionId
54+
*
55+
* @param $expected
56+
* @param $contentType
57+
* @param $exceptionMessage
58+
*/
59+
public function testRegionId($expected, $contentType, $exceptionMessage){
60+
try {
61+
self::assertEquals($expected, ClientFilter::regionId($contentType));
62+
} catch (ClientException $exception) {
63+
self::assertEquals($exceptionMessage, $exception->getMessage());
64+
}
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function regionId(){
71+
return [
72+
[
73+
1,
74+
1,
75+
'Region ID must be a string',
76+
],
77+
[
78+
'',
79+
'',
80+
'Region ID cannot be empty',
81+
],
82+
[
83+
'regionId中文',
84+
'regionId中文',
85+
'Invalid Region ID',
86+
],
87+
[
88+
'cn-hangzhou',
89+
'cn-hangzhou',
90+
'',
91+
]
92+
];
93+
}
94+
5295
/**
5396
* @dataProvider accept
5497
*

0 commit comments

Comments
 (0)