From f041b06a0e3966ec234c0840dd6327435481e638 Mon Sep 17 00:00:00 2001 From: Paul Cuzner Date: Tue, 1 Dec 2020 10:04:55 +1300 Subject: [PATCH] cephadm: add tests for invalid HTTP methods Add tests to confirm that the request handler does not respond to methods other than GET Signed-off-by: Paul Cuzner --- src/cephadm/tests/test_cephadm.py | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 0a8829779f82c..367a2089a7db5 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -582,3 +582,53 @@ iMN28C2bKGao5UHvdER1rGy7 assert exporter.cephadm_cache.host['scrape_errors'] assert exporter.cephadm_cache.host['scrape_errors'] == ['ValueError exception: wah'] assert exporter.cephadm_cache.errors == ['host thread stopped'] + + # Test the requesthandler does the right thing with invalid methods... + # ie. return a "501" - Not Implemented / Unsupported Method + def test_invalid_method_HEAD(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="HEAD") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_DELETE(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="DELETE") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_POST(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="POST") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_PUT(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="PUT") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_CONNECT(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="CONNECT") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_TRACE(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="TRACE") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_OPTIONS(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="OPTIONS") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) + + def test_invalid_method_PATCH(self): + hdrs={"Authorization":f"Bearer {TestCephadmExporter.token}"} + req=Request("http://localhost:9443/v1/metadata/health",headers=hdrs, method="PATCH") + with pytest.raises(HTTPError, match=r"HTTP Error 501: .*") as e: + urlopen(req) -- 2.39.5