Skip to content

Commit

Permalink
Add a binary response type
Browse files Browse the repository at this point in the history
  • Loading branch information
epsy committed May 10, 2016
1 parent a2820fe commit 3b8ad57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions napper/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ async def parse_response(self, response):
return await (await super().parse_response(response)).text()


class BytesResponse(ResponseType):
async def parse_response(self, response):
return await (await super().parse_response(response)).read()


class JsonResponse(TextResponse):
async def parse_response(self, response):
return json.loads(await super().parse_response(response))
Expand Down
8 changes: 8 additions & 0 deletions napper/tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,11 @@ async def test_unexpected_status_and_prepare_exc_await_twice(self):
await req
with self.assertRaises(self.CustomException):
r.exception.response

async def test_binary_response(self):
content = b'\x00\x01\x02\x03'
req = self.site.path.get()
req.response_type = response.BytesResponse()
with self.text_response(content):
result = await req
self.assertEqual(result, content)
8 changes: 7 additions & 1 deletion napper/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

class FakeTextResponse(object):
def __init__(self, response, status=200):
self._response = response
if isinstance(response, bytes):
self._bytes_response = response
else:
self._response = response
self.status = status

async def text(self, encoding=None):
Expand All @@ -26,6 +29,9 @@ async def text(self, encoding=None):
async def json(self, encoding=None, loads=json.loads):
return loads(self._response)

async def read(self):
return self._bytes_response

def close(self):
pass

Expand Down

0 comments on commit 3b8ad57

Please sign in to comment.