Repository: encode/uvicorn

Issue:
Extra chunk when body is empty
Using the example from the Readme, but returning an empty body:

```python
class App():

    def __init__(self, scope):
        assert scope['type'] == 'http'
        self.scope = scope

    async def __call__(self, receive, send):
        await send({
            'type': 'http.response.start',
            'status': 200,
            'headers': [
                [b'content-type', b'text/plain'],
            ],
        })
        await send({
            'type': 'http.response.body',
            'body': b'',  # <- This is the only change from the example
        })
```

Running:
```bash
uvicorn app:App
```

Curl says:
```bash
> curl http://127.0.0.1:8000 -v
* Rebuilt URL to: http://127.0.0.1:8000/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< server: uvicorn
< date: Thu, 17 Jan 2019 15:11:08 GMT
< content-type: text/plain
< transfer-encoding: chunked
< 
* Leftovers after chunking: 5 bytes
* Connection #0 to host 127.0.0.1 left intact
```

It sounds like those 5 bytes shouldn't be there. I think it's sending an extra chunk: there should be only 1 chunk with size 0. Maybe it's easier to see with telnet:

```bash
> telnet 127.0.0.1 8000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
GET / HTTP/1.1 

HTTP/1.1 200 OK
server: uvicorn
date: Thu, 17 Jan 2019 15:13:32 GMT
content-type: text/plain
transfer-encoding: chunked

0

0

```

I noticed it because it seems to break aiohttp's client. Any other UA seems to be happy with it.
Any status code is affected too, so 301/302 redirects are "always" affected for example. 

Comments:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 60f9f9b..43be62e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,7 +8,7 @@ python:
     - "3.7-dev"
 
 install:
-    - pip install -r requirements.txt
+    - pip install -U -r requirements.txt
 
 script:
     - scripts/test
diff --git a/uvicorn/protocols/http/httptools_impl.py b/uvicorn/protocols/http/httptools_impl.py
index 0b140b2..10af654 100644
--- a/uvicorn/protocols/http/httptools_impl.py
+++ b/uvicorn/protocols/http/httptools_impl.py
@@ -493,7 +493,10 @@ class RequestResponseCycle:
             if self.scope["method"] == "HEAD":
                 self.expected_content_length = 0
             elif self.chunked_encoding:
-                content = [b"%x\r\n" % len(body), body, b"\r\n"]
+                if body:
+                    content = [b"%x\r\n" % len(body), body, b"\r\n"]
+                else:
+                    content = []
                 if not more_body:
                     content.append(b"0\r\n\r\n")
                 self.transport.write(b"".join(content))


Test patch:
diff --git a/tests/protocols/test_http.py b/tests/protocols/test_http.py
index c1cd9dc..f1b3ca1 100644
--- a/tests/protocols/test_http.py
+++ b/tests/protocols/test_http.py
@@ -240,6 +240,35 @@ def test_chunked_encoding(protocol_cls):
     assert not protocol.transport.is_closing()
 
 
+@pytest.mark.parametrize("protocol_cls", [HttpToolsProtocol, H11Protocol])
+def test_chunked_encoding_empty_body(protocol_cls):
+    class App():
+        def __init__(self, scope):
+            assert scope['type'] == 'http'
+            self.scope = scope
+
+        async def __call__(self, receive, send):
+            await send({
+                'type': 'http.response.start',
+                'status': 200,
+                'headers': [
+                    [b'content-type', b'text/plain'],
+                ],
+            })
+            await send({
+                'type': 'http.response.body',
+                'body': b'',
+            })
+
+    protocol = get_connected_protocol(App, protocol_cls)
+    protocol.data_received(SIMPLE_GET_REQUEST)
+    protocol.loop.run_one()
+    assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
+    print(protocol.transport.buffer)
+    assert protocol.transport.buffer.count(b'0\r\n\r\n') == 1
+    assert not protocol.transport.is_closing()
+
+
 @pytest.mark.parametrize("protocol_cls", [HttpToolsProtocol, H11Protocol])
 def test_pipelined_requests(protocol_cls):
     def app(scope):