From 34ad01460867f80774967b2e2a2961313d4dadcd Mon Sep 17 00:00:00 2001 From: Jason Yau Date: Sun, 21 May 2023 20:25:08 -0700 Subject: [PATCH] add unit test and documentation for custom HTTP headers (https://github.com/python-caldav/caldav/issues/285) --- examples/basic_usage_examples.py | 6 +++++- tests/test_caldav_unit.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/basic_usage_examples.py b/examples/basic_usage_examples.py index 30985435..5756dcd1 100644 --- a/examples/basic_usage_examples.py +++ b/examples/basic_usage_examples.py @@ -18,6 +18,7 @@ caldav_url = "https://calendar.example.com/dav" username = "somebody" password = "hunter2" +headers = {"X-MY-CUSTOMER-HEADER" : "123"} def run_examples(): @@ -30,7 +31,10 @@ def run_examples(): ## so the credentials aren't validated. ## The client object can be used as a context manager, like this: with caldav.DAVClient( - url=caldav_url, username=username, password=password + url=caldav_url, + username=username, + password=password, + headers = headers # Optional parameter to set HTTP headers on each request if needed ) as client: ## Typically the next step is to fetch a principal object. diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 93f495d2..cf9cc13a 100644 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -264,6 +264,18 @@ def testRequestNonAscii(self, mocked): assert response.status == 200 assert response.tree is None + @mock.patch("caldav.davclient.requests.Session.request") + def testRequestCustomHeaders(self, mocked): + """ + ref https://github.com/python-caldav/caldav/issues/285 + """ + mocked().status_code = 200 + mocked().headers = {} + cal_url = "http://me:hunter2@calendar.møøh.example:80/" + client = DAVClient(url=cal_url, headers={"X-NC-CalDAV-Webcal-Caching": "On"}) + assert client.headers["Content-Type"] == "text/xml" + assert client.headers["X-NC-CalDAV-Webcal-Caching"] == "On" + @mock.patch("caldav.davclient.requests.Session.request") def testEmptyXMLNoContentLength(self, mocked): """