Skip to content

Commit

Permalink
publisher: post request fix
Browse files Browse the repository at this point in the history
* Fixes issues with POST request for sending the service XML to
  XSLS.

Signed-off-by: Lars Holm Nielsen <lars.holm.nielsen@cern.ch>
  • Loading branch information
lnielsen committed Jun 17, 2015
1 parent ffc3445 commit 867329e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cernservicexml/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import requests

from ._compat import StringIO
from .document import ServiceDocument


Expand All @@ -45,4 +46,5 @@ class XSLSPublisher(object):
def send(cls, document, api_url='http://xsls.cern.ch'):
"""Send service document to XSLS."""
assert isinstance(document, ServiceDocument)
return requests.post(api_url, data=document.to_xml())
return requests.post(api_url, files=dict(
file=StringIO(document.to_xml())))
11 changes: 10 additions & 1 deletion tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from datetime import datetime

import cgi
from cernservicexml import ServiceDocument, XSLSPublisher
from cernservicexml._compat import import_httpretty

Expand All @@ -31,7 +32,15 @@ def test_xslspublisher_send():
resp = XSLSPublisher.send(doc, api_url=EXAMPLE_URL)
assert resp.status_code == 200

assert httpretty.last_request().body.decode('utf-8') == \
# Parse multipart/form-data (not supported by HTTPretty)
content_type = httpretty.last_request().headers['Content-Type'].split(';')
content_type = [x.strip() for x in content_type]
key, boundary = content_type[1].split("=")
data = cgi.parse_multipart(httpretty.last_request().rfile,
{'boundary': boundary.encode('utf-8')})

assert 'file' in data
assert data['file'][0].decode('utf-8') == \
'<serviceupdate xmlns="http://sls.cern.ch/SLS/XML/update">' \
'<id>myid</id><availability>100</availability>' \
'<timestamp>2015-01-01T00:00:00</timestamp></serviceupdate>'

0 comments on commit 867329e

Please sign in to comment.