Debian Codegraph @

test/units/utils/test_jsonrpc.py

raw
1 from __future__ import annotations
2
3 import json
4 import pickle
5
6 from ansible._internal._datatag._tags import Origin
7 from ansible.utils.jsonrpc import JsonRpcServer
8
9
10 def test_response_type_cleansing() -> None:
11 """Avoid unpickling errors in module contexts by ensuring that non-scalar JsonRpc responses are not pickled with tags."""
12
13 class RPCTest:
14 def returns_list_with_tagged_str(self) -> list:
15 return [Origin(description="blar").tag("taggedstr")]
16
17 s = JsonRpcServer()
18 s.register(RPCTest())
19 req = dict(method="returns_list_with_tagged_str", id=1, params=(tuple(), {}))
20 jsonrpc_res = s.handle_request(json.dumps(req))
21
22 deserialized_res = json.loads(jsonrpc_res)
23
24 pickled_res = deserialized_res.get("result")
25
26 assert pickled_res is not None
27
28 res = pickle.loads(pickled_res.encode(errors="surrogateescape"))
29
30 assert res == ["taggedstr"]
31 assert not Origin.is_tagged_on(res[0])