more robust header checking in tests
This commit is contained in:
@@ -166,9 +166,9 @@ class Response():
|
|||||||
self.headers['Set-Cookie'] = [http_cookie]
|
self.headers['Set-Cookie'] = [http_cookie]
|
||||||
|
|
||||||
def complete(self):
|
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))
|
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'
|
self.headers['Content-Type'] = 'text/plain'
|
||||||
|
|
||||||
def write(self, stream):
|
def write(self, stream):
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_post_request(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
@@ -46,7 +46,7 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_before_after_request(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
@@ -82,7 +82,7 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
|
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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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()
|
mock_socket.clear_requests()
|
||||||
fd = mock_socket.add_request('GET', '/baz')
|
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'Set-Cookie: foo=bar\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Length: 3\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.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):
|
def test_404(self):
|
||||||
app = Microdot()
|
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.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-Length: 9\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_404_handler(self):
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
@@ -126,7 +126,7 @@ class TestMicrodot(unittest.TestCase):
|
|||||||
self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
|
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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_500(self):
|
||||||
app = Microdot()
|
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.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-Length: 21\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_500_handler(self):
|
||||||
app = Microdot()
|
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.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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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):
|
def test_exception_handler(self):
|
||||||
app = Microdot()
|
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.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-Length: 3\r\n', fd.response)
|
||||||
self.assertIn(b'Content-Type: text/plain\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'))
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'foo')
|
self.assertEqual(res.body, b'foo')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'Content-Length: 3\r\n', response)
|
||||||
b'Content-Length: 3\r\n'
|
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||||
b'Content-Type: text/plain\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||||
b'\r\n'
|
|
||||||
b'foo')
|
|
||||||
|
|
||||||
def test_create_from_string_with_content_length(self):
|
def test_create_from_string_with_content_length(self):
|
||||||
res = Response('foo', headers={'Content-Length': '2'})
|
res = Response('foo', headers={'Content-Length': '2'})
|
||||||
@@ -32,13 +30,11 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'foo')
|
self.assertEqual(res.body, b'foo')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'Content-Length: 2\r\n', response)
|
||||||
b'Content-Length: 2\r\n'
|
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||||
b'Content-Type: text/plain\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||||
b'\r\n'
|
|
||||||
b'foo')
|
|
||||||
|
|
||||||
def test_create_from_bytes(self):
|
def test_create_from_bytes(self):
|
||||||
res = Response(b'foo')
|
res = Response(b'foo')
|
||||||
@@ -47,13 +43,11 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'foo')
|
self.assertEqual(res.body, b'foo')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'Content-Length: 3\r\n', response)
|
||||||
b'Content-Length: 3\r\n'
|
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||||
b'Content-Type: text/plain\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\nfoo'))
|
||||||
b'\r\n'
|
|
||||||
b'foo')
|
|
||||||
|
|
||||||
def test_create_empty(self):
|
def test_create_empty(self):
|
||||||
res = Response(headers={'X-Foo': 'Bar'})
|
res = Response(headers={'X-Foo': 'Bar'})
|
||||||
@@ -62,13 +56,12 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'')
|
self.assertEqual(res.body, b'')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'X-Foo: Bar\r\n', response)
|
||||||
b'X-Foo: Bar\r\n'
|
self.assertIn(b'Content-Length: 0\r\n', response)
|
||||||
b'Content-Length: 0\r\n'
|
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||||
b'Content-Type: text/plain\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\n'))
|
||||||
b'\r\n')
|
|
||||||
|
|
||||||
def test_create_json(self):
|
def test_create_json(self):
|
||||||
res = Response({'foo': 'bar'})
|
res = Response({'foo': 'bar'})
|
||||||
@@ -77,13 +70,11 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'{"foo": "bar"}')
|
self.assertEqual(res.body, b'{"foo": "bar"}')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'Content-Length: 14\r\n', response)
|
||||||
b'Content-Type: application/json\r\n'
|
self.assertIn(b'Content-Type: application/json\r\n', response)
|
||||||
b'Content-Length: 14\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\n{"foo": "bar"}'))
|
||||||
b'\r\n'
|
|
||||||
b'{"foo": "bar"}')
|
|
||||||
|
|
||||||
res = Response([1, '2'])
|
res = Response([1, '2'])
|
||||||
self.assertEqual(res.status_code, 200)
|
self.assertEqual(res.status_code, 200)
|
||||||
@@ -91,13 +82,11 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.body, b'[1, "2"]')
|
self.assertEqual(res.body, b'[1, "2"]')
|
||||||
fd = io.BytesIO()
|
fd = io.BytesIO()
|
||||||
res.write(fd)
|
res.write(fd)
|
||||||
self.assertEqual(
|
response = fd.getvalue()
|
||||||
fd.getvalue(),
|
self.assertIn(b'HTTP/1.0 200 OK\r\n', response)
|
||||||
b'HTTP/1.0 200 OK\r\n'
|
self.assertIn(b'Content-Length: 8\r\n', response)
|
||||||
b'Content-Type: application/json\r\n'
|
self.assertIn(b'Content-Type: application/json\r\n', response)
|
||||||
b'Content-Length: 8\r\n'
|
self.assertTrue(response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||||
b'\r\n'
|
|
||||||
b'[1, "2"]')
|
|
||||||
|
|
||||||
def test_create_from_other(self):
|
def test_create_from_other(self):
|
||||||
res = Response(123)
|
res = Response(123)
|
||||||
|
|||||||
Reference in New Issue
Block a user