|
| 1 | +import pytest |
| 2 | + |
1 | 3 | from qiniu.http.region import Region, ServiceName
|
2 | 4 | from qiniu.region import LegacyRegion
|
3 |
| -from qiniu.compat import json |
| 5 | +from qiniu.compat import json, is_py2 |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mocked_hosts(): |
| 10 | + mocked_hosts = { |
| 11 | + ServiceName.UP: ['https://up.python-example.qiniu.com', 'https://up-2.python-example.qiniu.com'], |
| 12 | + ServiceName.IO: ['https://io.python-example.qiniu.com'], |
| 13 | + ServiceName.RS: ['https://rs.python-example.qiniu.com'], |
| 14 | + ServiceName.RSF: ['https://rsf.python-example.qiniu.com'], |
| 15 | + ServiceName.API: ['https://api.python-example.qiniu.com'] |
| 16 | + } |
| 17 | + yield mocked_hosts |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def mock_legacy_region(mocked_hosts): |
| 22 | + region = LegacyRegion( |
| 23 | + up_host=mocked_hosts[ServiceName.UP][0], |
| 24 | + up_host_backup=mocked_hosts[ServiceName.UP][1], |
| 25 | + io_host=mocked_hosts[ServiceName.IO][0], |
| 26 | + rs_host=mocked_hosts[ServiceName.RS][0], |
| 27 | + rsf_host=mocked_hosts[ServiceName.RSF][0], |
| 28 | + api_host=mocked_hosts[ServiceName.API][0] |
| 29 | + ) |
| 30 | + yield region |
4 | 31 |
|
5 | 32 |
|
6 | 33 | class TestLegacyRegion:
|
7 |
| - def test_compatible_with_http_region(self): |
8 |
| - mocked_hosts = { |
9 |
| - ServiceName.UP: ['https://up.python-example.qiniu.com', 'https://up-2.python-example.qiniu.com'], |
10 |
| - ServiceName.IO: ['https://io.python-example.qiniu.com'], |
11 |
| - ServiceName.RS: ['https://rs.python-example.qiniu.com'], |
12 |
| - ServiceName.RSF: ['https://rsf.python-example.qiniu.com'], |
13 |
| - ServiceName.API: ['https://api.python-example.qiniu.com'] |
14 |
| - } |
| 34 | + def test_get_hosts_from_self(self, mocked_hosts, mock_legacy_region, qn_auth, bucket_name): |
| 35 | + cases = [ |
| 36 | + # up will always query from the old version, |
| 37 | + # which version implements the `get_up_host_*` method |
| 38 | + ( |
| 39 | + mock_legacy_region.get_io_host(qn_auth.get_access_key(), None), |
| 40 | + mocked_hosts[ServiceName.IO][0] |
| 41 | + ), |
| 42 | + ( |
| 43 | + mock_legacy_region.get_rs_host(qn_auth.get_access_key(), None), |
| 44 | + mocked_hosts[ServiceName.RS][0] |
| 45 | + ), |
| 46 | + ( |
| 47 | + mock_legacy_region.get_rsf_host(qn_auth.get_access_key(), None), |
| 48 | + mocked_hosts[ServiceName.RSF][0] |
| 49 | + ), |
| 50 | + ( |
| 51 | + mock_legacy_region.get_api_host(qn_auth.get_access_key(), None), |
| 52 | + mocked_hosts[ServiceName.API][0] |
| 53 | + ) |
| 54 | + ] |
| 55 | + for actual, expect in cases: |
| 56 | + assert actual == expect |
| 57 | + |
| 58 | + def test_get_hosts_from_query(self, qn_auth, bucket_name): |
| 59 | + up_token = qn_auth.upload_token(bucket_name) |
| 60 | + region = LegacyRegion() |
| 61 | + up_host = region.get_up_host_by_token(up_token, None) |
| 62 | + up_host_backup = region.get_up_host_backup_by_token(up_token, None) |
| 63 | + if is_py2: |
| 64 | + up_host = up_host.encode() |
| 65 | + up_host_backup = up_host_backup.encode() |
| 66 | + assert type(up_host) is str and len(up_host) > 0 |
| 67 | + assert type(up_host_backup) is str and len(up_host_backup) > 0 |
| 68 | + assert up_host != up_host_backup |
15 | 69 |
|
16 |
| - region = LegacyRegion( |
17 |
| - up_host=mocked_hosts[ServiceName.UP][0], |
18 |
| - up_host_backup=mocked_hosts[ServiceName.UP][1], |
19 |
| - io_host=mocked_hosts[ServiceName.IO][0], |
20 |
| - rs_host=mocked_hosts[ServiceName.RS][0], |
21 |
| - rsf_host=mocked_hosts[ServiceName.RSF][0], |
22 |
| - api_host=mocked_hosts[ServiceName.API][0] |
23 |
| - ) |
24 |
| - assert isinstance(region, Region) |
| 70 | + def test_compatible_with_http_region(self, mocked_hosts, mock_legacy_region): |
| 71 | + assert isinstance(mock_legacy_region, Region) |
25 | 72 | assert mocked_hosts == {
|
26 | 73 | k: [
|
27 | 74 | e.get_value()
|
28 |
| - for e in region.services[k] |
| 75 | + for e in mock_legacy_region.services[k] |
29 | 76 | ]
|
30 | 77 | for k in mocked_hosts
|
31 | 78 | }
|
|
0 commit comments