def __init__(self, elements=None, attrs=None, copy_attrs_on_match=True, response_elements=None, response_attrs=None, response_namespaces=None): """Create a XMLManglerExit. :Params: - `elements` (optional): Matcher elements. A dict of element -> name to :class:`~stubo.ext.xmlutils.XPathValue`. An instance of :class:`dict` - `attrs` (optional): Matcher attributes. A dict of attribute -> name to :class:`~stubo.ext.xmlutils.XPathValue`. An instance of :class:`dict` - `copy_attrs_on_match` (optional): For matching elements, copy any element attributes. Defaults to True - `response_elements` (optional): Response elements. A dict of element -> name to :class:`~stubo.ext.xmlutils.XPathValue`. An instance of :class:`dict` - `response_attrs` (optional): Response attributes. A dict of attribute -> name to :class:`~stubo.ext.xmlutils.XPathValue`. An instance of :class:`dict` - `response_namespaces` (optional): Response namespaces. An instance of :class:`dict`. """ self.mangler = self.response_mangler = None if elements or attrs: self.mangler = XMLMangler(elements=elements, attrs=attrs, copy_attrs_on_match=copy_attrs_on_match) if response_elements or response_attrs: self.response_mangler = XMLMangler(elements=response_elements, attrs=response_attrs, copy_attrs_on_match=True, namespaces=response_namespaces)
def test_response(self): from stubo.ext.xmlutils import XMLMangler mangler = XMLMangler(elements=dict(a=XPathValue('/path/to/a'))) response_mangler = XMLMangler(elements=dict( a=XPathValue('/response', extractor=lambda x: x.upper())), copy_attrs_on_match=True) request = DummyModel(body='<path><to><a>xyz</a></to></path>', headers={}) from stubo.model.stub import Stub, create stub = Stub( create('<path><to><a>xyz</a></to></path>', '<response>abc</response>'), "foo") from stubo.ext.transformer import StuboTemplateProcessor context = dict(stub=stub, template_processor=StuboTemplateProcessor()) putter = self._make(response_mangler=response_mangler, mangler=mangler, request=StuboRequest(request), context=context) putter.doMatcher() response = putter.doResponse() self.assertEqual( response.stub.payload, { 'request': { 'bodyPatterns': { 'contains': [u'<path><to><a>***</a></to></path>'] }, 'method': 'POST' }, 'response': { 'body': u'<response>ABC</response>', 'status': 200 } })
def test_ctor_fails_if_xpath_has_no_extractor(self): from stubo.ext.xmlutils import XMLMangler mangler = XMLMangler(elements=dict(a=XPathValue('/path/to/a'))) response_mangler = XMLMangler(elements=dict(a=XPathValue('/response')), copy_attrs_on_match=True) request = DummyModel(body='<path><to><a>xyz</a></to></path>', headers={}) from stubo.model.stub import Stub, create stub = Stub( create('<path><to><a>xyz</a></to></path>', '<response>abc</response>'), "foo") from stubo.ext.transformer import StuboTemplateProcessor context = dict(stub=stub, template_processor=StuboTemplateProcessor()) with self.assertRaises(ValueError): self._make(response_mangler=response_mangler, mangler=mangler, request=StuboRequest(request), context=context)
def test_ctor(self): from stubo.ext.xmlutils import XMLMangler mangler = XMLMangler(elements=dict(a=XPathValue('/path/to/a'))) response_mangler = XMLMangler( elements=dict(a=XPathValue('/response', extractor=lambda x: x)), copy_attrs_on_match=True) request = DummyModel(body='<path><to><a>xyz</a></to></path>', headers={}) from stubo.model.stub import Stub, create stub = Stub( create('<path><to><a>xyz</a></to></path>', '<response>abc</response>'), "foo") from stubo.ext.transformer import StuboTemplateProcessor context = dict(stub=stub, template_processor=StuboTemplateProcessor()) putter = self._make(response_mangler=response_mangler, mangler=mangler, request=StuboRequest(request), context=context) self.assertEqual(putter.mangler, mangler) self.assertEqual(putter.response_mangler, response_mangler)
def test_matcher_strip_ns(self): from stubo.ext.xmlutils import XMLMangler mangler = XMLMangler(elements=dict(a=XPathValue('/path/to/a'))) request = DummyModel(body='<path><to><a>xyz</a></to></path>', headers={}) from stubo.model.stub import Stub, create stub = Stub( create('<path xmlns="http://great.com"><to><a>xyz</a></to></path>', '<response>abc</response>'), "foo") from stubo.ext.transformer import StuboTemplateProcessor context = dict(stub=stub, template_processor=StuboTemplateProcessor()) putter = self._make(mangler=mangler, request=StuboRequest(request), context=context) result = putter.doMatcher() self.assertEqual(result.stub.contains_matchers(), [u'<path><to><a>***</a></to></path>'])
def test_matcher2(self): from stubo.ext.xmlutils import XMLMangler mangler = XMLMangler(elements=dict( a=XPathValue('/path/to/a', extractor=lambda x: x[1:-1]))) request = DummyModel(body='<path><to><a>xyz</a></to></path>', headers={}) from stubo.model.stub import Stub, create stub = Stub( create('<path><to><a>y</a></to></path>', '<response>abc</response>'), "foo") from stubo.ext.transformer import StuboTemplateProcessor context = dict(stub=stub, template_processor=StuboTemplateProcessor()) getter = self._make(mangler=mangler, request=StuboRequest(request), context=context) response = getter.doMatcher() self.assertEqual(response.stub.contains_matchers(), [u'<path><to><a>y</a></to></path>'])
def _make(self, **kwargs): from stubo.ext.xmlutils import XMLMangler return XMLMangler(**kwargs)