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

Commit

Permalink
Raise ValueError when anything but map[string]string is used for Thrift
Browse files Browse the repository at this point in the history
headers
  • Loading branch information
HelloGrayson committed Aug 7, 2015
1 parent 544cdb2 commit d32db3b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -5,7 +5,8 @@ Changelog
0.15.2 (unreleased)
-------------------

- Nothing changed yet.
- Raise informative and obvious ``ValueError`` when anything
but a map[string]string is passed as headers to the ``TChannel.thrift`` method.


0.15.1 (2015-08-07)
Expand Down
11 changes: 9 additions & 2 deletions tchannel/schemes/thrift.py
Expand Up @@ -21,11 +21,18 @@ def __init__(self, tchannel):
def __call__(self, request=None, headers=None, timeout=None,
retry_on=None, retry_limit=None):

if headers is None:
if not headers:
headers = {}

# serialize
headers = serializer.serialize_headers(headers=headers)
try:
headers = serializer.serialize_headers(headers=headers)
except (AttributeError, TypeError):
raise ValueError(
'headers must be a map[string]string (a shallow dict'
' where keys and values are strings)'
)

body = serializer.serialize_body(call_args=request.call_args)

response = yield self._tchannel.call(
Expand Down
41 changes: 41 additions & 0 deletions tests/schemes/test_thrift.py
Expand Up @@ -1141,3 +1141,44 @@ def testString(request, response, proxy):
yield tchannel.thrift(
service.testString('no return!?')
)


@pytest.mark.gen_test
@pytest.mark.callz
def test_headers_should_be_a_map_of_strings():

tchannel = TChannel('client')

with pytest.raises(ValueError):
yield tchannel.thrift(
request=True,
headers={'key': 1},
)

with pytest.raises(ValueError):
yield tchannel.thrift(
request=True,
headers={1: 'value'},
)

with pytest.raises(ValueError):
yield tchannel.thrift(
request=True,
headers=100,
)

with pytest.raises(ValueError):
yield tchannel.thrift(
request=True,
headers={
'key': {
'key': 'value',
},
},
)

with pytest.raises(ValueError):
yield tchannel.thrift(
request=True,
headers=Exception()
)

0 comments on commit d32db3b

Please sign in to comment.