Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #164 from uber/fix-status
Browse files Browse the repository at this point in the history
Actually fix status code
  • Loading branch information
abhinav committed Sep 10, 2015
2 parents 2452cb1 + 4b70262 commit 5ee8ea7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changes by Version
==================

0.16.5 (unreleased)
-------------------

- Actually fix status code being unset in responses when using the Thrift
scheme.


0.16.4 (2015-09-09)
-------------------

Expand Down
5 changes: 3 additions & 2 deletions tchannel/serializer/thrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def serialize_header(self, headers):

def deserialize_header(self, headers):
headers = headers or {}
headers = io.BytesIO(headers)
headers = self._headers_rw.read(headers)
if headers:
headers = io.BytesIO(headers)
headers = self._headers_rw.read(headers)
result = dict(headers)

return result
Expand Down
2 changes: 2 additions & 0 deletions tchannel/tornado/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def handle_call(self, request, connection):
# instantiate a tchannel.Response
new_resp = response_from_mixed(new_resp)

response.code = new_resp.status

# assign resp values to dep response
response.write_header(new_resp.headers)

Expand Down
45 changes: 45 additions & 0 deletions tests/schemes/test_thrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
from tchannel.testing.data.generated.ThriftTest import ThriftTest
from tchannel.tornado import TChannel as DeprecatedTChannel

from tchannel.tornado.connection import TornadoConnection
from tchannel.messages.call_request import CallRequestMessage


# TODO - where possible, in req/res style test, create parameterized tests,
# each test should test w headers and wout
Expand Down Expand Up @@ -1243,3 +1246,45 @@ def testString(request):
resp = future.result()

assert resp == 'sbb'


@pytest.mark.gen_test
@pytest.mark.call
def test_exception_status_code_is_set():

# Given this test server:

server = TChannel(name='server')

@server.thrift.register(ThriftTest)
def testException(request):
raise ThriftTest.Xception(
errorCode=1001,
message=request.body.arg
)

server.listen()

# Make a call:

conn = yield TornadoConnection.outgoing(server.hostport)
res = yield conn.send(
CallRequestMessage(
service=b'service',
headers={b'cn': b'client', b'as': b'thrift'},
args=[
b'ThriftTest::testException',
b'',
bytearray([
0x0B, # type = string
0x00, 0x01, # field ID 1

0x00, 0x00, 0x00, 0x00, # empty string

0x00, # STOP
]),
],
)
)

assert 1 == res.status_code
22 changes: 22 additions & 0 deletions tests/thrift/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from thrift.Thrift import TType

from tchannel.serializer.thrift import ThriftSerializer
from tchannel.thrift.server import build_handler
from tchannel.thrift.server import deprecated_build_handler
from tchannel.tornado.request import Request
from tchannel.tornado.response import Response
Expand Down Expand Up @@ -190,3 +191,24 @@ def call(treq, tres):
])
)
assert 1 == res.status_code


@pytest.mark.gen_test
def test_build_handler_application_exception():
def call(req):
raise FakeException('fail')

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

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

assert res.status == 1

0 comments on commit 5ee8ea7

Please sign in to comment.