From 4dc718e8c3bd2a8bbe1f2d7a98ad03421a70217f Mon Sep 17 00:00:00 2001 From: Alistair Coles Date: Mon, 8 Sep 2014 14:06:00 +0100 Subject: [PATCH] Extra unit tests for check_delete_headers A few extra tests to verify check_delete_headers in constraints.py. A little duplication of coverage of existing proxy/controllers/test_obj.py:TestObjController.test_POST_delete_at but these tests call the recently refactored function directly, and also add tests for X-Delete-After taking precedence over X-Delete-At. Change-Id: I129cef15a6feac8a60fd4efbb3535d93f0eaab36 --- test/unit/common/test_constraints.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/unit/common/test_constraints.py b/test/unit/common/test_constraints.py index a28de653d1..44476d12c8 100644 --- a/test/unit/common/test_constraints.py +++ b/test/unit/common/test_constraints.py @@ -325,6 +325,49 @@ class TestConstraints(unittest.TestCase): else: self.fail("Should have failed with HTTPBadRequest") + def test_check_delete_headers_sets_delete_at(self): + t = time.time() + 1000 + # check delete-at is passed through + headers = {'Content-Length': '0', + 'Content-Type': 'text/plain', + 'X-Delete-At': str(int(t))} + req = Request.blank('/', headers=headers) + constraints.check_delete_headers(req) + self.assertTrue('X-Delete-At' in req.headers) + self.assertEqual(req.headers['X-Delete-At'], str(int(t))) + + # check delete-after is converted to delete-at + headers = {'Content-Length': '0', + 'Content-Type': 'text/plain', + 'X-Delete-After': '42'} + req = Request.blank('/', headers=headers) + with mock.patch('time.time', lambda: t): + constraints.check_delete_headers(req) + self.assertTrue('X-Delete-At' in req.headers) + expected = str(int(t) + 42) + self.assertEqual(req.headers['X-Delete-At'], expected) + + # check delete-after takes precedence over delete-at + headers = {'Content-Length': '0', + 'Content-Type': 'text/plain', + 'X-Delete-After': '42', + 'X-Delete-At': str(int(t) + 40)} + req = Request.blank('/', headers=headers) + with mock.patch('time.time', lambda: t): + constraints.check_delete_headers(req) + self.assertTrue('X-Delete-At' in req.headers) + self.assertEqual(req.headers['X-Delete-At'], expected) + + headers = {'Content-Length': '0', + 'Content-Type': 'text/plain', + 'X-Delete-After': '42', + 'X-Delete-At': str(int(t) + 44)} + req = Request.blank('/', headers=headers) + with mock.patch('time.time', lambda: t): + constraints.check_delete_headers(req) + self.assertTrue('X-Delete-At' in req.headers) + self.assertEqual(req.headers['X-Delete-At'], expected) + def test_check_mount(self): self.assertFalse(constraints.check_mount('', '')) with mock.patch("swift.common.utils.ismount", MockTrue()):