예제 #1
0
def test_build_handler():

    def call(treq, tres, tchan):
        assert treq.transport.headers == {
            'as': 'thrift', 'cn': 'test_caller'
        }

        tres.write_header('foo', 'baar')
        return "world"

    response_header = InMemStream()
    response_body = InMemStream()

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        scheme=ThriftArgScheme(FakeResult),
        headers={'cn': 'test_caller', 'as': 'thrift'},
    )
    req.close_argstreams()

    res = Response(
        argstreams=[
            InMemStream(),
            response_header,
            response_body,
        ],
        scheme=ThriftArgScheme(FakeResult),
    )
    tchannel = mock.Mock()

    handler = build_handler(FakeResult, call)
    yield handler(req, res, tchannel)

    serialized_headers = yield response_header.read()
    assert serialized_headers == bytearray(
        [
            0x00, 0x01,  # num headers = 1
            0x00, 0x03,  # strlen('foo') = 3
        ] + list('foo') + [
            0x00, 0x04,  # strlen('baar') = 4
        ] + list('baar')
    )

    serialized_body = yield response_body.read()
    assert serialized_body == bytearray([
        0x0b,                    # field type = TType.STRING
        0x00, 0x00,              # field ID = 0
        0x00, 0x00, 0x00, 0x05,  # string length = 5
    ] + list("world") + [
        0x00,                    # end struct
    ])

    assert 0 == res.status_code
예제 #2
0
def test_deprecated_build_handler():

    def call(treq, tres):
        assert treq.transport.headers == {
            'as': 'thrift', 'cn': 'test_caller'
        }

        tres.write_header('foo', 'baar')
        return "world"

    response_header = InMemStream()
    response_body = InMemStream()

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        serializer=ThriftSerializer(FakeResult),
        headers={'cn': 'test_caller', 'as': 'thrift'},
    )
    req.close_argstreams()

    res = Response(
        argstreams=[
            InMemStream(),
            response_header,
            response_body,
        ],
        serializer=ThriftSerializer(FakeResult),
    )

    handler = deprecated_build_handler(FakeResult, call)
    yield handler(req, res)

    serialized_headers = yield response_header.read()
    assert serialized_headers == bytearray(
        [
            0x00, 0x01,  # num headers = 1
            0x00, 0x03,  # strlen('foo') = 3
        ] + list('foo') + [
            0x00, 0x04,  # strlen('baar') = 4
        ] + list('baar')
    )

    serialized_body = yield response_body.read()
    assert serialized_body == bytearray([
        0x0b,                    # field type = TType.STRING
        0x00, 0x00,              # field ID = 0
        0x00, 0x00, 0x00, 0x05,  # string length = 5
    ] + list("world") + [
        0x00,                    # end struct
    ])

    assert 0 == res.status_code
예제 #3
0
def test_InMemStream():
    stream = InMemStream()
    yield stream.write("1")
    yield stream.write("2")
    buf = yield stream.read()
    assert buf == "12"

    yield stream.write("3")
    buf = yield stream.read()
    assert buf == "3"

    # check internal stream buffer.
    assert len(stream._stream) == 0

    stream.close()
    with pytest.raises(StreamingError):
        yield stream.write("4")
예제 #4
0
def test_error_during_stream(io_loop):
    stream = InMemStream()
    try:
        1 / 0
    except Exception as e:
        stream.set_exception(e)

    with pytest.raises(ZeroDivisionError):
        yield stream.read()

    with pytest.raises(ZeroDivisionError):
        yield stream.write("a")