Example #1
0
def test_frame_should_have_a_copy_method():
    original = Frame(Command.SEND, {"abc": "def"}, "zorro")
    original_encoded = original.encode()

    copy = original.copy()
    copy.command = Command.CONNECT
    copy.body = "batman"
    copy.headers["geh"] = "xyz"

    assert original.encode() == original_encoded
Example #2
0
def test_parsing_frame_with_headers(headers):
    parser = Parser()
    frame = Frame(Command.CONNECT, headers)
    parser.parse(frame.encode())
    parsed_frame = parser.pop_frame()

    assert parsed_frame.command == Command.CONNECT
    assert parsed_frame.headers == headers
    assert parsed_frame.body == b""
Example #3
0
def test_parsing_frame_with_headers_and_body(body):
    parser = Parser()
    frame = Frame(Command.CONNECT, {"abc": "def"}, body)
    parser.parse(frame.encode())
    parsed_frame = parser.pop_frame()

    assert parsed_frame.command == Command.CONNECT
    assert "abc" in parsed_frame.headers
    assert parsed_frame.headers["abc"] == "def"
    assert "content-length" in parsed_frame.headers
    assert int(parsed_frame.headers["content-length"]) == len(body)
    assert parsed_frame.body == body
Example #4
0
def test_parser_should_accept_frames_with_crlf_eols():
    parser = Parser()
    frame = Frame(Command.CONNECT, {"abc": "def"}, b"zorro")
    encoded_frame = frame.encode().replace(b"\n", b"\r\n")
    parser.parse(encoded_frame)
    parsed_frame = parser.pop_frame()

    assert parsed_frame.command == Command.CONNECT
    assert "abc" in parsed_frame.headers
    assert parsed_frame.headers["abc"] == "def"
    assert "content-length" in parsed_frame.headers
    assert int(parsed_frame.headers["content-length"]) == 5
    assert parsed_frame.body == b"zorro"
Example #5
0
def test_parsing_multiple_frames_with_headers_and_body():
    parser = Parser()
    frame = Frame(Command.CONNECT, {"abc": "def"}, b"zorro")
    parser.parse(frame.encode() * 2)

    for _ in range(2):
        parsed_frame = parser.pop_frame()

        assert parsed_frame.command == Command.CONNECT
        assert "abc" in parsed_frame.headers
        assert parsed_frame.headers["abc"] == "def"
        assert "content-length" in parsed_frame.headers
        assert int(parsed_frame.headers["content-length"]) == len(b"zorro")
        assert parsed_frame.body == b"zorro"
    def test_handle_read(self):
        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 = ('{"jsonrpc": "2.0", "id": "e8a936a6-d886-4cfa-97b9-2d54209053f'
                'f", "result": []}')
        frame = Frame(command=Command.MESSAGE, headers=headers, body=body)
        dispatcher = AsyncDispatcher(FakeConnection(), frame_handler)
        dispatcher.handle_read(FakeAsyncDispatcher(None, data=frame.encode()))

        self.assertTrue(frame_handler.has_outgoing_messages)
        recv_frame = frame_handler.pop_message()
        self.assertEqual(Command.MESSAGE, recv_frame.command)
        self.assertEqual(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_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
Example #9
0
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"