diff --git a/microdot.py b/microdot.py index cf874e2..a814436 100644 --- a/microdot.py +++ b/microdot.py @@ -166,9 +166,9 @@ class Response(): self.headers['Set-Cookie'] = [http_cookie] def complete(self): - if not 'Content-Length' in self.headers: + if 'Content-Length' not in self.headers: self.headers['Content-Length'] = str(len(self.body)) - if not 'Content-Type' in self.headers: + if 'Content-Type' not in self.headers: self.headers['Content-Type'] = 'text/plain' def write(self, stream): diff --git a/tests/test_microdot.py b/tests/test_microdot.py index 65ffd27..8db1ce0 100644 --- a/tests/test_microdot.py +++ b/tests/test_microdot.py @@ -27,7 +27,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n')) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nfoo')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nfoo')) def test_post_request(self): app = Microdot() @@ -46,7 +46,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n')) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nbar')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nbar')) def test_before_after_request(self): app = Microdot() @@ -82,7 +82,7 @@ class TestMicrodot(unittest.TestCase): self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nbar')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nbar')) mock_socket.clear_requests() fd = mock_socket.add_request('GET', '/baz') @@ -92,7 +92,7 @@ class TestMicrodot(unittest.TestCase): self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nbaz')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nbaz')) def test_404(self): app = Microdot() @@ -107,7 +107,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 404 N/A\r\n')) self.assertIn(b'Content-Length: 9\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nNot found')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nNot found')) def test_404_handler(self): app = Microdot() @@ -126,7 +126,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n')) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\n404')) + self.assertTrue(fd.response.endswith(b'\r\n\r\n404')) def test_500(self): app = Microdot() @@ -141,7 +141,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 500 N/A\r\n')) self.assertIn(b'Content-Length: 21\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\nInternal server error')) + self.assertTrue(fd.response.endswith(b'\r\n\r\nInternal server error')) def test_500_handler(self): app = Microdot() @@ -160,7 +160,7 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n')) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\n501')) + self.assertTrue(fd.response.endswith(b'\r\n\r\n501')) def test_exception_handler(self): app = Microdot() @@ -179,4 +179,4 @@ class TestMicrodot(unittest.TestCase): self.assertTrue(fd.response.startswith(b'HTTP/1.0 501 N/A\r\n')) self.assertIn(b'Content-Length: 3\r\n', fd.response) self.assertIn(b'Content-Type: text/plain\r\n', fd.response) - self.assertTrue(fd.response.endswith(b'\r\n501')) + self.assertTrue(fd.response.endswith(b'\r\n\r\n501')) diff --git a/tests/test_response.py b/tests/test_response.py index 112d2fb..7e130ae 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -17,13 +17,11 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'foo') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'Content-Length: 3\r\n' - b'Content-Type: text/plain\r\n' - b'\r\n' - b'foo') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'Content-Length: 3\r\n', response) + self.assertIn(b'Content-Type: text/plain\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\nfoo')) def test_create_from_string_with_content_length(self): res = Response('foo', headers={'Content-Length': '2'}) @@ -32,13 +30,11 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'foo') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'Content-Length: 2\r\n' - b'Content-Type: text/plain\r\n' - b'\r\n' - b'foo') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'Content-Length: 2\r\n', response) + self.assertIn(b'Content-Type: text/plain\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\nfoo')) def test_create_from_bytes(self): res = Response(b'foo') @@ -47,13 +43,11 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'foo') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'Content-Length: 3\r\n' - b'Content-Type: text/plain\r\n' - b'\r\n' - b'foo') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'Content-Length: 3\r\n', response) + self.assertIn(b'Content-Type: text/plain\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\nfoo')) def test_create_empty(self): res = Response(headers={'X-Foo': 'Bar'}) @@ -62,13 +56,12 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'X-Foo: Bar\r\n' - b'Content-Length: 0\r\n' - b'Content-Type: text/plain\r\n' - b'\r\n') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'X-Foo: Bar\r\n', response) + self.assertIn(b'Content-Length: 0\r\n', response) + self.assertIn(b'Content-Type: text/plain\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\n')) def test_create_json(self): res = Response({'foo': 'bar'}) @@ -77,13 +70,11 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'{"foo": "bar"}') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'Content-Type: application/json\r\n' - b'Content-Length: 14\r\n' - b'\r\n' - b'{"foo": "bar"}') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'Content-Length: 14\r\n', response) + self.assertIn(b'Content-Type: application/json\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\n{"foo": "bar"}')) res = Response([1, '2']) self.assertEqual(res.status_code, 200) @@ -91,13 +82,11 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.body, b'[1, "2"]') fd = io.BytesIO() res.write(fd) - self.assertEqual( - fd.getvalue(), - b'HTTP/1.0 200 OK\r\n' - b'Content-Type: application/json\r\n' - b'Content-Length: 8\r\n' - b'\r\n' - b'[1, "2"]') + response = fd.getvalue() + self.assertIn(b'HTTP/1.0 200 OK\r\n', response) + self.assertIn(b'Content-Length: 8\r\n', response) + self.assertIn(b'Content-Type: application/json\r\n', response) + self.assertTrue(response.endswith(b'\r\n\r\n[1, "2"]')) def test_create_from_other(self): res = Response(123)