Esempio n. 1
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
Esempio n. 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
Esempio n. 3
0
def test_deprecated_build_handler_exception():
    def call(treq, tres):
        raise FakeException('fail')

    response_body = mock.Mock(spec=InMemStream)

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        serializer=ThriftSerializer(FakeResult),
    )
    req.close_argstreams()

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

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

    response_body.write.assert_called_once_with(
        bytearray([
            0x0c,  # field type = TType.STRUCT
            0x00,
            0x01,  # field ID = 1
            0x0b,  # field type = TType.STRING
            0x00,
            0x01,  # field ID = 1
            0x00,
            0x00,
            0x00,
            0x04,  # string length = 5
        ] + list(b"fail") + [
            0x00,  # end exception struct
            0x00,  # end response struct
        ]))
    assert 1 == res.status_code
Esempio n. 4
0
def test_deprecated_build_handler_exception():
    def call(treq, tres):
        raise FakeException('fail')

    response_body = mock.Mock(spec=InMemStream)

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        serializer=ThriftSerializer(FakeResult),
    )
    req.close_argstreams()

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

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

    response_body.write.assert_called_once_with(
        bytearray([
            0x0c,                    # field type = TType.STRUCT
            0x00, 0x01,              # field ID = 1

            0x0b,                    # field type = TType.STRING
            0x00, 0x01,              # field ID = 1
            0x00, 0x00, 0x00, 0x04,  # string length = 5
        ] + list("fail") + [
            0x00,                    # end exception struct
            0x00,                    # end response struct
        ])
    )
    assert 1 == res.status_code