Skip to content

Commit 511a2b2

Browse files
authored
Merge pull request #652 from gschwind/main-fix-request-storage-2
Fix ComplexInput.from_json to handle data_format.encoding="base64"
2 parents 61a8f0c + ce91aed commit 511a2b2

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

pywps/inout/inputs.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright 2018 Open Source Geospatial Foundation and others #
33
# licensed under MIT, Please consult LICENSE.txt for details #
44
##################################################################
5-
5+
import base64
66
import re
77
from pywps import xml_util as etree
88

@@ -220,12 +220,15 @@ def from_json(cls, json_input):
220220
instance.url = json_input['href']
221221
elif json_input.get('data'):
222222
data = json_input['data']
223-
# remove cdata tag if it exists (issue #553)
224-
if isinstance(data, str):
225-
match = CDATA_PATTERN.match(data)
226-
if match:
227-
data = match.group(1)
228-
instance.data = data
223+
if data_format.encoding == 'base64':
224+
instance.data = base64.b64decode(data)
225+
else:
226+
# remove cdata tag if it exists (issue #553)
227+
if isinstance(data, str):
228+
match = CDATA_PATTERN.match(data)
229+
if match:
230+
data = match.group(1)
231+
instance.data = data
229232

230233
return instance
231234

tests/test_inout.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,34 @@ def test_complex_input_binary_data(self):
291291
assert complex.json['data'] == '<![CDATA[some data]]>'
292292
self.assertEqual(complex.prop, 'data')
293293

294+
def test_complex_input_data_with_binary_data_format(self):
295+
complex = inout.inputs.ComplexInput(
296+
identifier="complexinput",
297+
title='MyComplex',
298+
abstract='My complex input',
299+
keywords=['kw1', 'kw2'],
300+
workdir=self.tmp_dir,
301+
supported_formats=[FORMATS.ZIP],
302+
metadata=[Metadata("special data")],
303+
default="/some/file/path",
304+
default_type=SOURCE_TYPE.FILE,
305+
translations={"fr-CA": {"title": "Mon input", "abstract": "Une description"}},
306+
data_format=FORMATS.ZIP
307+
)
308+
complex.as_reference = False
309+
complex.method = "GET"
310+
complex.max_size = 1000
311+
complex.data = b"some data"
312+
# the data is enclosed by a CDATA tag in json
313+
assert complex.json['data'] == 'c29tZSBkYXRh'
314+
# dump to json and load it again
315+
complex2 = inout.inputs.ComplexInput.from_json(complex.json)
316+
317+
self.assert_complex_equals(complex, complex2)
318+
self.assertEqual(complex.prop, 'data')
319+
self.assertEqual(complex2.prop, 'data')
320+
self.assertEqual(complex.data, complex2.data)
321+
294322
def test_complex_input_stream(self):
295323
complex = self.make_complex_input()
296324
complex.stream = StringIO("{'name': 'test', 'input1': ']]'}")

0 commit comments

Comments
 (0)