74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from core.h5_jsbridge import H5JsBridgeEncoder, build_h5_sign_input
|
|
|
|
|
|
class H5JsBridgeTests(unittest.TestCase):
|
|
def test_build_get_sign_input_matches_frontend_sorting(self):
|
|
cookie = {
|
|
"kpn": "NEBULA",
|
|
"kpf": "ANDROID_PHONE",
|
|
"userId": "10001",
|
|
"did": "ANDROID_aaaaaaaaaaaaaaaa",
|
|
"egid": "DFPBBBB",
|
|
"token": "ignored",
|
|
"__NS_sig3": "ignored",
|
|
}
|
|
|
|
self.assertEqual(
|
|
build_h5_sign_input(cookie, {"source": "bottom_guide_first"}),
|
|
"did=ANDROID_aaaaaaaaaaaaaaaa"
|
|
"egid=DFPBBBB"
|
|
"kpf=ANDROID_PHONE"
|
|
"kpn=NEBULA"
|
|
"sigCatVer=1"
|
|
"source=bottom_guide_first"
|
|
"userId=10001",
|
|
)
|
|
|
|
def test_build_post_json_sign_input_appends_raw_body(self):
|
|
cookie = {"kpn": "NEBULA", "did": "ANDROID_1"}
|
|
body = '{"b":2,"a":1}'
|
|
|
|
self.assertEqual(
|
|
build_h5_sign_input(cookie, body=body, method="POST", request_type="json"),
|
|
"did=ANDROID_1kpn=NEBULAsigCatVer=1" + body,
|
|
)
|
|
|
|
def test_object_values_are_blank_like_frontend(self):
|
|
cookie = {"kpn": "NEBULA"}
|
|
|
|
self.assertEqual(
|
|
build_h5_sign_input(cookie, {"obj": {"x": 1}}),
|
|
"kpn=NEBULAobj=sigCatVer=1",
|
|
)
|
|
|
|
def test_encoder_process_reuses_line_protocol(self):
|
|
server_code = (
|
|
"import json, sys\n"
|
|
"for line in sys.stdin:\n"
|
|
" req = json.loads(line)\n"
|
|
" print(json.dumps({'id': req.get('id'), 'ok': True, "
|
|
"'result': 'a' * 68, 'cInfo': 123}), flush=True)\n"
|
|
)
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
server = Path(temp_dir) / "fake_h5_server.py"
|
|
server.write_text(server_code, encoding="utf-8")
|
|
encoder = H5JsBridgeEncoder(server=server, node_bin=sys.executable, timeout=5)
|
|
try:
|
|
first = encoder.encode("one")
|
|
second = encoder.encode("two")
|
|
finally:
|
|
encoder.close()
|
|
|
|
self.assertEqual(first.sig3, "a" * 68)
|
|
self.assertEqual(second.sig3, "a" * 68)
|
|
self.assertEqual(first.c_info, 123)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|