From 6afa9fb84e308f72d96ac2ea5e0386484a4d272a Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 4 Oct 2015 00:28:37 +0800 Subject: [PATCH 1/2] Fix #505 --- aiohttp/client_reqrep.py | 6 ++++- tests/test_client_functional.py | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 34d2016185c..8c70592f4c5 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -583,6 +583,10 @@ def _setup_connection(self, connection): self.content = self.flow_control_class( connection.reader, loop=connection.loop) + def _need_parse_response_body(self): + return (self.method.lower() != 'head' and + self.status not in [204, 304]) + @asyncio.coroutine def start(self, connection, read_until_eof=False): """Start response processing.""" @@ -610,7 +614,7 @@ def start(self, connection, read_until_eof=False): self.headers = CIMultiDictProxy(message.headers) # payload - response_with_body = self.method.lower() != 'head' + response_with_body = self._need_parse_response_body() self._reader.set_parser( aiohttp.HttpPayloadParser(message, readall=read_until_eof, diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 8d22cdfd959..92b88cb4b6c 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -208,6 +208,36 @@ def go(): yield from asyncio.sleep(0, loop=self.loop) self.loop.run_until_complete(go()) + def test_HTTP_304(self): + with test_utils.run_server(self.loop, router=Functional) as httpd: + @asyncio.coroutine + def go(): + r = yield from client.request( + 'get', httpd.url('not_modified_304', 0), + loop=self.loop) + content = yield from r.content.read() + content = content.decode() + + self.assertEqual(r.status, 304) + self.assertEqual('', content) + r.close() + self.loop.run_until_complete(go()) + + def test_HTTP_304_WITH_BODY(self): + with test_utils.run_server(self.loop, router=Functional) as httpd: + @asyncio.coroutine + def go(): + r = yield from client.request( + 'get', httpd.url('not_modified_304', 1), + loop=self.loop) + content = yield from r.content.read() + content = content.decode() + + self.assertEqual(r.status, 304) + self.assertEqual('', content) + r.close() + self.loop.run_until_complete(go()) + def test_POST_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') @@ -1312,6 +1342,20 @@ def redirect_307(self, match): self._start_response(307), headers={'Location': self._path}) + @test_utils.Router.define('/not_modified_304/([0-9]+)$') + def not_modified_304(self, match): + no = int(match.group(1).upper()) + + if no == 0: + self._response( + self._start_response(304), + body='test', + ) + else: + self._response( + self._start_response(304), + ) + @test_utils.Router.define('/encoding/(gzip|deflate)$') def encoding(self, match): mode = match.group(1) From 180496fb46779fa2e38aa173030c6a93ac449bc7 Mon Sep 17 00:00:00 2001 From: carlcarl Date: Sun, 4 Oct 2015 03:34:46 +0800 Subject: [PATCH 2/2] Move 304 status testing to newstyle function --- tests/test_client_functional.py | 44 ------------------------ tests/test_client_functional_newstyle.py | 42 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 92b88cb4b6c..8d22cdfd959 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -208,36 +208,6 @@ def go(): yield from asyncio.sleep(0, loop=self.loop) self.loop.run_until_complete(go()) - def test_HTTP_304(self): - with test_utils.run_server(self.loop, router=Functional) as httpd: - @asyncio.coroutine - def go(): - r = yield from client.request( - 'get', httpd.url('not_modified_304', 0), - loop=self.loop) - content = yield from r.content.read() - content = content.decode() - - self.assertEqual(r.status, 304) - self.assertEqual('', content) - r.close() - self.loop.run_until_complete(go()) - - def test_HTTP_304_WITH_BODY(self): - with test_utils.run_server(self.loop, router=Functional) as httpd: - @asyncio.coroutine - def go(): - r = yield from client.request( - 'get', httpd.url('not_modified_304', 1), - loop=self.loop) - content = yield from r.content.read() - content = content.decode() - - self.assertEqual(r.status, 304) - self.assertEqual('', content) - r.close() - self.loop.run_until_complete(go()) - def test_POST_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') @@ -1342,20 +1312,6 @@ def redirect_307(self, match): self._start_response(307), headers={'Location': self._path}) - @test_utils.Router.define('/not_modified_304/([0-9]+)$') - def not_modified_304(self, match): - no = int(match.group(1).upper()) - - if no == 0: - self._response( - self._start_response(304), - body='test', - ) - else: - self._response( - self._start_response(304), - ) - @test_utils.Router.define('/encoding/(gzip|deflate)$') def encoding(self, match): mode = match.group(1) diff --git a/tests/test_client_functional_newstyle.py b/tests/test_client_functional_newstyle.py index 125136ef6c1..7e41e6739ef 100644 --- a/tests/test_client_functional_newstyle.py +++ b/tests/test_client_functional_newstyle.py @@ -119,3 +119,45 @@ def go(): connector.close() self.loop.run_until_complete(go()) + + def test_HTTP_304(self): + @asyncio.coroutine + def handler(request): + body = yield from request.read() + self.assertEqual(b'', body) + return web.Response(status=304) + + @asyncio.coroutine + def go(): + _, srv, url = yield from self.create_server('GET', '/', handler) + connector = aiohttp.TCPConnector(loop=self.loop) + r = yield from client.request('GET', url, + connector=connector, loop=self.loop) + content = yield from r.read() + self.assertEqual(r.status, 304) + self.assertEqual(content, b'') + r.release() + connector.close() + + self.loop.run_until_complete(go()) + + def test_HTTP_304_WITH_BODY(self): + @asyncio.coroutine + def handler(request): + body = yield from request.read() + self.assertEqual(b'', body) + return web.Response(body=b'test', status=304) + + @asyncio.coroutine + def go(): + _, srv, url = yield from self.create_server('GET', '/', handler) + connector = aiohttp.TCPConnector(loop=self.loop) + r = yield from client.request('GET', url, + connector=connector, loop=self.loop) + content = yield from r.read() + self.assertEqual(r.status, 304) + self.assertEqual(content, b'') + r.release() + connector.close() + + self.loop.run_until_complete(go())