From 9bc0dc642d40e539a5cda2e806a1e9e51e96c3dd Mon Sep 17 00:00:00 2001 From: Alexandre Fayolle Date: Wed, 28 Feb 2024 17:26:46 +0100 Subject: [PATCH] [IMP] webservice: combine the url with collection's url when a call is made with just a path, combine it with the collection's url --- webservice/components/request_adapter.py | 10 ++++++++-- webservice/tests/test_webservice.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/webservice/components/request_adapter.py b/webservice/components/request_adapter.py index 4d7d00cb..4356bcf2 100644 --- a/webservice/components/request_adapter.py +++ b/webservice/components/request_adapter.py @@ -65,8 +65,14 @@ def _get_headers_for_api_key(self, **kw): def _get_url(self, url=None, url_params=None, **kwargs): if not url: - # TODO: if url is given, we should validate the domain - # to avoid abusing a webservice backend for different calls. url = self.collection.url + elif not url.startswith(self.collection.url): + if not url.startswith("http"): + url = self.collection.url + url + else: + # TODO: if url is given, we should validate the domain + # to avoid abusing a webservice backend for different calls. + pass + url_params = url_params or kwargs return url.format(**url_params) diff --git a/webservice/tests/test_webservice.py b/webservice/tests/test_webservice.py index aee0ca54..46d734bc 100644 --- a/webservice/tests/test_webservice.py +++ b/webservice/tests/test_webservice.py @@ -88,6 +88,28 @@ def test_web_service_get(self): responses.calls[0].request.headers["Content-Type"], "application/xml" ) + @responses.activate + def test_web_service_get_url_combine(self): + endpoint = "api/test" + responses.add(responses.GET, self.url + endpoint, body="{}") + result = self.webservice.call("get", url="api/test") + self.assertEqual(result, b"{}") + self.assertEqual(len(responses.calls), 1) + self.assertEqual( + responses.calls[0].request.headers["Content-Type"], "application/xml" + ) + + @responses.activate + def test_web_service_get_url_combine_full_url(self): + endpoint = "api/test" + responses.add(responses.GET, self.url + endpoint, body="{}") + result = self.webservice.call("get", url="http://localhost.demo.odoo/api/test") + self.assertEqual(result, b"{}") + self.assertEqual(len(responses.calls), 1) + self.assertEqual( + responses.calls[0].request.headers["Content-Type"], "application/xml" + ) + @responses.activate def test_web_service_post(self): responses.add(responses.POST, self.url, body="{}")