def test_handle_read(): frame_handler = FakeFrameHandler() headers = {Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: 'ad052acb-a934-4e10-8ec3-00c7417ef8d'} body = json.dumps({ "jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053ff", "result": [], }).encode("utf-8") frame = Frame(command=Command.MESSAGE, headers=headers, body=body) dispatcher = AsyncDispatcher(FakeConnection(), frame_handler) dispatcher.handle_read(FakeAsyncDispatcher(None, data=frame.encode())) assert frame_handler.has_outgoing_messages recv_frame = frame_handler.pop_message() assert Command.MESSAGE == recv_frame.command assert body == recv_frame.body
def test_handle_read(self): frame_handler = TestFrameHandler() headers = { Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: 'ad052acb-a934-4e10-8ec3-00c7417ef8d' } body = ('{"jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053f' 'f", "result": []}') frame = Frame(command=Command.MESSAGE, headers=headers, body=body) dispatcher = AsyncDispatcher(TestConnection(), frame_handler) dispatcher.handle_read(TestDispatcher(frame.encode())) self.assertTrue(frame_handler.has_outgoing_messages) recv_frame = frame_handler.pop_message() self.assertEquals(Command.MESSAGE, recv_frame.command) self.assertEquals(body, recv_frame.body)
def test_multipe_subscriptions(self): frame = Frame(Command.UNSUBSCRIBE, {'id': 'ad052acb-a934-4e10-8ec3-00c7417ef8d1'}) subscription = FakeSubscription('jms.queue.events', 'ad052acb-a934-4e10-8ec3-00c7417ef8d1') subscription2 = FakeSubscription('jms.queue.events', 'e8a93a6-d886-4cfa-97b9-2d54209053ff') destinations = defaultdict(list) destinations['jms.queue.events'].append(subscription) destinations['jms.queue.events'].append(subscription2) adapter = StompAdapterImpl(Reactor(), destinations, {}) adapter._sub_ids['ad052acb-a934-4e10-8ec3-00c7417ef8d1'] = subscription adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) self.assertEqual(len(adapter._sub_ids), 0) self.assertEqual(len(destinations), 1) self.assertEqual(destinations['jms.queue.events'], [subscription2])
def test_send_batch(self): body = ('[{"jsonrpc":"2.0","method":"Host.getAllVmStats","params":{},' '"id":"e8a936a6-d886-4cfa-97b9-2d54209053ff"},' '{"jsonrpc":"2.0","method":"Host.getAllVmStats","params":{},' '"id":"1202b274-5a06-4671-8b13-1d2715429668"}]') frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.vdsm_requests', Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '209'}, body=body ) ids = {} adapter = StompAdapterImpl(Reactor(), defaultdict(list), ids) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) data = adapter.pop_message() self.assertIsNot(data, None) self.assertEqual(len(ids), 2)
def test_send(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.vdsm_requests', Headers.REPLY_TO: 'jms.topic.vdsm_responses', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) ids = {} adapter = StompAdapterImpl(Reactor(), defaultdict(list), ids) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) data = adapter.pop_message() self.assertIsNot(data, None) request = JsonRpcRequest.decode(data) self.assertEqual(request.method, 'Host.getAllVmStats') self.assertEqual(len(ids), 1)
def test_receive_message(self): client = AsyncClient() id = 'ad052acb-a934-4e10-8ec3-00c7417ef8d1' headers = {Headers.CONTENT_LENGTH: '78', Headers.DESTINATION: 'jms.topic.vdsm_responses', Headers.CONTENT_TYPE: 'application/json', Headers.SUBSCRIPTION: id} body = ('{"jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053f' 'f", "result": []}') frame = Frame(command=Command.MESSAGE, headers=headers, body=body) def message_handler(sub, frame): client.queue_frame(frame) client.subscribe('', 'auto', id, message_handler) # ignore subscribe frame client.pop_message() client.handle_frame(None, frame) self.assertEqual(frame, client.pop_message())
def test_send_broker(self): frame = Frame(command=Command.SEND, headers={Headers.DESTINATION: 'jms.topic.destination', Headers.CONTENT_LENGTH: '103'}, body=('{"jsonrpc":"2.0","method":"Host.getAllVmStats",' '"params":{},"id":"e8a936a6-d886-4cfa-97b9-2d54209' '053ff"}' ) ) subscription = FakeSubscription('jms.topic.destination', 'e8a936a6-d886-4cfa-97b9-2d54209053ff') destinations = defaultdict(list) destinations['jms.topic.destination'].append(subscription) adapter = StompAdapterImpl(Reactor(), destinations, {}) subscription.set_client(adapter) adapter.handle_frame(FakeAsyncDispatcher(adapter), frame) resp_frame = adapter.pop_message() self.assertEqual(resp_frame.command, Command.MESSAGE)
def test_receive_error(self): client = AsyncClient() frame = Frame(command=Command.ERROR, body='Test error') with self.assertRaises(StompError): client.handle_frame(None, frame)
def test_frame_should_have_a_nice_repr(): assert repr(Frame(Command.SEND)) == "<StompFrame command='SEND'>"
def test_encoding_frame_should_fix_invalid_content_length(): frame = Frame(Command.SEND, {"content-length": 3}, "6chars") assert frame.encode() == b"SEND\ncontent-length:6\n\n6chars\x00"
def test_encoding_frame_with_headers_and_payload(headers, payload, expected): assert Frame(Command.SEND, headers, payload).encode() == expected
def test_encoding_frame_with_headers(headers, expected): assert Frame(Command.CONNECT, headers).encode() == expected
def test_encoding_frame_with_command_only(command, expected): assert Frame(command).encode() == expected
def _build_frame(self, destination, data="", headers=None): final_headers = {"destination": destination} if headers is not None: final_headers.update(headers) return Frame(Command.SEND, final_headers, data)