Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XML body format instead of form encoding #302

Merged
merged 4 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion tests/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import six
import unittest
from collections import defaultdict
from mock import Mock
from mock import Mock, patch
from xml.dom.minidom import parseString

from xero.exceptions import XeroExceptionUnknown
from xero.manager import Manager


Expand Down Expand Up @@ -357,3 +358,31 @@ def test_user_agent_inheritance(self):
credentials = Mock(base_url="", user_agent="MY_COMPANY-MY_CONSUMER_KEY")
manager = Manager("reports", credentials, user_agent="DemoCompany-1234567890")
self.assertEqual(manager.user_agent, "DemoCompany-1234567890")

@patch("xero.basemanager.requests.post")
def test_request_content_type(self, request):
"""The Content-Type should be application/xml
"""

# Default used when no user_agent set on manager and credentials has nothing to offer.
credentials = Mock(base_url="", user_agent=None)
manager = Manager("reports", credentials)
try:
manager._get_data(lambda: ("_", {}, "post", {}, {}, True))()
except XeroExceptionUnknown:
pass

call = request.mock_calls[0]
self.assertTrue(call.kwargs["headers"]["Content-Type"], "application/xml")

def test_request_body_format(self):
"""The body content should be in valid XML format
"""

# Default used when no user_agent set on manager and credentials has nothing to offer.
credentials = Mock(base_url="", user_agent=None)
manager = Manager("reports", credentials)

body = manager.save_or_put({"bing": "bong"})[3]

self.assertTrue(body, "<Invoice><bing>bong</bing></Invoice>")
4 changes: 3 additions & 1 deletion xero/basemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ def wrapper(*args, **kwargs):
if headers is None:
headers = {}

headers["Content-Type"] = "application/xml"

if isinstance(self.credentials, OAuth2Credentials):
if self.credentials.tenant_id:
headers["Xero-tenant-id"] = self.credentials.tenant_id
Expand Down Expand Up @@ -312,7 +314,7 @@ def _online_invoice(self, id):

def save_or_put(self, data, method="post", headers=None, summarize_errors=True):
uri = "/".join([self.base_url, self.name])
body = {"xml": self._prepare_data_for_save(data)}
body = self._prepare_data_for_save(data)
params = self.extra_params.copy()
if not summarize_errors:
params["summarizeErrors"] = "false"
Expand Down