|
| 1 | +import json |
| 2 | + |
| 3 | +from http_request_recorder import RecordedRequest |
| 4 | + |
| 5 | + |
| 6 | +from unittest import TestCase |
| 7 | +from sipgate_e2e_test_utils.rpc_matchers import xml_rpc, json_rpc |
| 8 | + |
| 9 | + |
| 10 | +class TestSipgateRpcMatchers(TestCase): |
| 11 | + def test_xml_rpc(self) -> None: |
| 12 | + assertions = [ |
| 13 | + (False, ('POST', '/jsonrpc', json.dumps({'method': 'test_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 14 | + (False, ('POST', '/jsonrpc', b'<?xml version="1.0"?><methodCall><methodName>test_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 15 | + (False, ('GET', '/rpc2', b'<?xml version="1.0"?><methodCall><methodName>test_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 16 | + (False, ('POST', '/rpc2', b'anydata')), |
| 17 | + (False, ('POST', '/rpc2', b'<?xml version="1.0"?><methodCall><methodName>another_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 18 | + (True, ('POST', '/rpc2', b'<?xml version="1.0"?><methodCall><methodName>test_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 19 | + (True, ('POST', '/RPC2', b'<?xml version="1.0"?><methodCall><methodName>test_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 20 | + ] |
| 21 | + |
| 22 | + for (expected, (method, path, body)) in assertions: |
| 23 | + with self.subTest(f'expect {expected} for method={method} path={path} body={body.decode()}'): |
| 24 | + request = RecordedRequest() |
| 25 | + request.method = method |
| 26 | + request.path = path |
| 27 | + request.body = body |
| 28 | + |
| 29 | + self.assertEqual(expected, xml_rpc('test_method')(request)) |
| 30 | + |
| 31 | + def test_json_rpc(self) -> None: |
| 32 | + assertions = [ |
| 33 | + (False, ('POST', '/rpc2', b'<?xml version="1.0"?><methodCall><methodName>test_method</methodName><params><param><value></value></param></params></methodCall>')), |
| 34 | + (False, ('POST', '/rpc2', json.dumps({'method': 'test_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 35 | + (False, ('GET', '/jsonrpc', json.dumps({'method': 'test_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 36 | + (False, ('POST', '/jsonrpc', b'anydata')), |
| 37 | + (False, ('POST', '/jsonrpc', json.dumps({'method': 'another_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 38 | + (True, ('POST', '/jsonrpc', json.dumps({'method': 'test_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 39 | + (True, ('POST', '/JSONRPC', json.dumps({'method': 'test_method', 'version': '1.1', 'params': [], 'id': 42}).encode())), |
| 40 | + ] |
| 41 | + |
| 42 | + for (expected, (method, path, body)) in assertions: |
| 43 | + with self.subTest(f'expect {expected} for method={method} path={path} body={body.decode()}'): |
| 44 | + request = RecordedRequest() |
| 45 | + request.method = method |
| 46 | + request.path = path |
| 47 | + request.body = body |
| 48 | + |
| 49 | + self.assertEqual(expected, json_rpc('test_method')(request)) |
0 commit comments