dev
This commit is contained in:
@@ -17,3 +17,30 @@ def test_calc_response_contract(client, alter_token, minimal_calc_config):
|
||||
assert isinstance(data['skills'], list)
|
||||
assert isinstance(data['info'], list)
|
||||
assert isinstance(data['buffer'], bool)
|
||||
|
||||
|
||||
def test_character_response_contract_with_gzip(client, alter_token):
|
||||
response = client.get(
|
||||
'/api/character',
|
||||
headers={'alter-token': alter_token, 'accept-encoding': 'gzip'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['content-encoding'] == 'gzip'
|
||||
assert response.headers['vary'] == 'Accept-Encoding'
|
||||
payload = response.json()
|
||||
assert payload['code'] == 200
|
||||
assert payload['message'] == 'Success'
|
||||
assert set(['alter', 'skills', 'equips', 'suits']).issubset(payload['data'])
|
||||
|
||||
|
||||
def test_character_response_contract_without_gzip(client, alter_token):
|
||||
response = client.get(
|
||||
'/api/character',
|
||||
headers={'alter-token': alter_token, 'accept-encoding': 'identity'},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'content-encoding' not in response.headers
|
||||
assert response.headers['vary'] == 'Accept-Encoding'
|
||||
assert response.json()['code'] == 200
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import gzip
|
||||
import json
|
||||
|
||||
from api.utils.character_response import (
|
||||
character_response_cache_key,
|
||||
character_response_encoding,
|
||||
clear_character_response_local_cache,
|
||||
get_character_response_bytes,
|
||||
)
|
||||
from config.main import config
|
||||
|
||||
|
||||
def setup_function():
|
||||
clear_character_response_local_cache()
|
||||
|
||||
|
||||
def teardown_function():
|
||||
clear_character_response_local_cache()
|
||||
|
||||
|
||||
def test_character_response_encoding_matches_gzip_middleware():
|
||||
assert character_response_encoding('gzip, deflate, br') == 'gzip'
|
||||
assert character_response_encoding('identity') == 'identity'
|
||||
assert character_response_encoding(None) == 'identity'
|
||||
|
||||
|
||||
def test_character_response_caches_final_gzip_bytes_locally():
|
||||
calls = 0
|
||||
|
||||
def load_info():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return {'alter': 'weapon_master', 'skills': []}
|
||||
|
||||
first = get_character_response_bytes(None, 'GF.swordman_male.weapon_master', '0', 'gzip', load_info)
|
||||
second = get_character_response_bytes(None, 'GF.swordman_male.weapon_master', '0', 'gzip', load_info)
|
||||
|
||||
assert first == second
|
||||
assert calls == 1
|
||||
assert json.loads(gzip.decompress(first)) == {
|
||||
'code': 200,
|
||||
'message': 'Success',
|
||||
'data': {'alter': 'weapon_master', 'skills': []},
|
||||
}
|
||||
|
||||
|
||||
def test_character_response_caches_identity_and_gzip_separately():
|
||||
calls = 0
|
||||
|
||||
def load_info():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return {'name': '极诣·剑魂'}
|
||||
|
||||
identity = get_character_response_bytes(None, 'GF.swordman_male.weapon_master', '0', 'identity', load_info)
|
||||
compressed = get_character_response_bytes(None, 'GF.swordman_male.weapon_master', '0', 'gzip', load_info)
|
||||
|
||||
assert calls == 2
|
||||
assert gzip.decompress(compressed) == identity
|
||||
assert json.loads(identity)['data']['name'] == '极诣·剑魂'
|
||||
|
||||
|
||||
def test_character_response_reuses_redis_final_bytes_after_local_cache_clear(monkeypatch):
|
||||
class FakeRedis:
|
||||
def __init__(self):
|
||||
self.values = {}
|
||||
|
||||
def get(self, key):
|
||||
return self.values.get(key)
|
||||
|
||||
def set(self, key, value):
|
||||
self.values[key] = value
|
||||
|
||||
redis = FakeRedis()
|
||||
monkeypatch.setattr(config, 'DEBUG_MODE', False)
|
||||
first = get_character_response_bytes(
|
||||
redis,
|
||||
'GF.swordman_male.weapon_master',
|
||||
'0',
|
||||
'gzip',
|
||||
lambda: {'alter': 'weapon_master'},
|
||||
)
|
||||
clear_character_response_local_cache()
|
||||
|
||||
def fail_to_load_info():
|
||||
raise AssertionError('Redis cache hit must not rebuild character info')
|
||||
|
||||
second = get_character_response_bytes(
|
||||
redis,
|
||||
'GF.swordman_male.weapon_master',
|
||||
'0',
|
||||
'gzip',
|
||||
fail_to_load_info,
|
||||
)
|
||||
|
||||
redis_key = character_response_cache_key('GF.swordman_male.weapon_master', '0', 'gzip')
|
||||
assert redis.values[redis_key] == first
|
||||
assert second == first
|
||||
Reference in New Issue
Block a user