Skip to content

Commit

Permalink
convert request body to given type (xml by default, json if requested)
Browse files Browse the repository at this point in the history
  • Loading branch information
petri committed Feb 20, 2017
1 parent ea01e8b commit 2121230
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ History

* Implement request generator loading (issue #2)
* Implement response parser loading (issue #1))
* Implement request conversion (issue #4)

0.2.0 (2017-02-19)
------------------
Expand Down
10 changes: 9 additions & 1 deletion httpreverse/httpreverse.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-

import re
import json
from importlib import import_module
from collections import ChainMap
import yaml
import jinja2
import xmltodict

# parameter name extraction regexp
prm_xpr = re.compile('(\\$([a-zA-Z0-9_])+)+')
Expand Down Expand Up @@ -54,7 +56,7 @@ def _parametrize_mapping(mapping, context):
return mapping


def parametrize(opspec, context={}, implicit=False):
def parametrize(opspec, context={}, implicit=False, tojson=False, toxml=True):
"assign parameter values, optionally implicitly using parameter names"

# request parameters
Expand All @@ -73,6 +75,12 @@ def parametrize(opspec, context={}, implicit=False):
if rbody:
_parametrize_mapping(rbody, context)

# convert body to the type given
if "json" in opspec["request"].get("type", "") and tojson:
opspec["request"]["body"] = json.dumps(rbody)
elif "xml" in opspec["request"].get("type", "") and toxml:
opspec["request"]["body"] = xmltodict.unparse(rbody)

return opspec


Expand Down
31 changes: 30 additions & 1 deletion tests/test_httpreverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
Tests for `httpreverse` module.
"""

import sys, os, unittest
import sys, os, unittest, json
import yaml
import xmltodict
from httpreverse import expand_jinja, apply_template, parametrize
from httpreverse import _load_parser, _load_generator

Expand Down Expand Up @@ -114,3 +115,31 @@ def test2_generator_loading(self):
opspec["request"]["generator"] = "httpreverse.util:parse_json"
_load_generator(opspec, assign=True)
assert opspec["request"]["generator"].__name__ == "parse_json"


class Test05_body_conversion(BaseTestCase):

def setUp(self):
super().setUp()
self.expanded = expand_jinja(self.source, context=self.context)
self.parsed = yaml.load(self.expanded)

# this will be converted to JSON and XML
self.data = {"root": {"size": "double", "customer": ["John", "Jane"]}}

testopname = "list-singlerooms"
opspec = self.parsed["operations"][testopname]
self.opspec = apply_template(opspec, templates=self.parsed["templates"])
self.opspec["request"]["body"] = self.data

def test1_json_convert(self):
self.opspec["request"]["type"] = "application/json"
parametrize(self.opspec, tojson=True)
body = self.opspec["request"]["body"]
assert self.data == json.loads(body)

def test2_xml_convert(self):
self.opspec["request"]["type"] = "application/xml"
parametrize(self.opspec)
body = self.opspec["request"]["body"]
assert self.data == xmltodict.parse(body)

0 comments on commit 2121230

Please sign in to comment.