|
| 1 | +import json |
| 2 | +import unittest |
| 3 | + |
| 4 | +from not_found_handler import ALLOWED_METHODS, not_found |
| 5 | + |
| 6 | + |
| 7 | +class TestNotFoundHandler(unittest.TestCase): |
| 8 | + """Tests for the not_found_handler functionality""" |
| 9 | + |
| 10 | + def test_method_not_allowed_unsupported_method(self): |
| 11 | + """Test that unsupported HTTP methods return 405 Method Not Allowed""" |
| 12 | + event = {"httpMethod": "PATCH"} |
| 13 | + |
| 14 | + response = not_found(event, None) |
| 15 | + |
| 16 | + self.assertEqual(response["statusCode"], 405) |
| 17 | + self.assertEqual(response["headers"]["Content-Type"], "application/json") |
| 18 | + self.assertEqual(response["headers"]["Allow"], "GET, POST, DELETE, PUT") |
| 19 | + |
| 20 | + # Verify the response body structure |
| 21 | + body = json.loads(response["body"]) |
| 22 | + self.assertEqual(body["resourceType"], "OperationOutcome") |
| 23 | + self.assertEqual(body["issue"][0]["severity"], "error") |
| 24 | + self.assertEqual(body["issue"][0]["code"], "not-supported") |
| 25 | + self.assertEqual(body["issue"][0]["diagnostics"], "Method Not Allowed") |
| 26 | + |
| 27 | + def test_method_not_allowed_other_unsupported_method(self): |
| 28 | + """Test that other unsupported HTTP methods also return 405""" |
| 29 | + unsupported_methods = ["PUTT", "PATCH", "HEAD", "OPTIONS", "TRACE", "CONNECT"] |
| 30 | + |
| 31 | + for method in unsupported_methods: |
| 32 | + with self.subTest(method=method): |
| 33 | + event = {"httpMethod": method} |
| 34 | + response = not_found(event, None) |
| 35 | + |
| 36 | + self.assertEqual(response["statusCode"], 405) |
| 37 | + self.assertEqual(response["headers"]["Allow"], "GET, POST, DELETE, PUT") |
| 38 | + |
| 39 | + def test_not_found_allowed_method(self): |
| 40 | + """Test that allowed HTTP methods return 404 Not Found""" |
| 41 | + allowed_methods = ALLOWED_METHODS # ["GET", "POST", "DELETE", "PUT"] |
| 42 | + |
| 43 | + for method in allowed_methods: |
| 44 | + with self.subTest(method=method): |
| 45 | + event = {"httpMethod": method} |
| 46 | + response = not_found(event, None) |
| 47 | + |
| 48 | + self.assertEqual(response["statusCode"], 404) |
| 49 | + self.assertEqual(response["headers"]["Content-Type"], "application/json") |
| 50 | + self.assertNotIn("Allow", response["headers"]) # No Allow header for 404 |
| 51 | + |
| 52 | + # Verify the response body structure |
| 53 | + body = json.loads(response["body"]) |
| 54 | + self.assertEqual(body["resourceType"], "OperationOutcome") |
| 55 | + self.assertEqual(body["issue"][0]["severity"], "error") |
| 56 | + self.assertEqual(body["issue"][0]["code"], "not-found") |
| 57 | + self.assertEqual(body["issue"][0]["diagnostics"], "The requested resource was not found.") |
| 58 | + |
| 59 | + def test_not_found_missing_http_method(self): |
| 60 | + """Test that missing httpMethod defaults to 405 Method Not Allowed""" |
| 61 | + event = {} |
| 62 | + |
| 63 | + response = not_found(event, None) |
| 64 | + |
| 65 | + self.assertEqual(response["statusCode"], 405) |
| 66 | + self.assertEqual(response["headers"]["Content-Type"], "application/json") |
| 67 | + self.assertEqual(response["headers"]["Allow"], "GET, POST, DELETE, PUT") |
| 68 | + |
| 69 | + def test_not_found_none_http_method(self): |
| 70 | + """Test that None httpMethod defaults to 405 Method Not Allowed""" |
| 71 | + event = {"httpMethod": None} |
| 72 | + |
| 73 | + response = not_found(event, None) |
| 74 | + |
| 75 | + self.assertEqual(response["statusCode"], 405) |
| 76 | + self.assertEqual(response["headers"]["Content-Type"], "application/json") |
| 77 | + self.assertEqual(response["headers"]["Allow"], "GET, POST, DELETE, PUT") |
| 78 | + |
| 79 | + def test_allowed_methods_constant(self): |
| 80 | + """Test that ALLOWED_METHODS contains the expected HTTP methods""" |
| 81 | + expected_methods = ["GET", "POST", "DELETE", "PUT"] |
| 82 | + self.assertEqual(ALLOWED_METHODS, expected_methods) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + unittest.main() |
0 commit comments