Ejemplo n.º 1
0
    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
                }
            })
Ejemplo n.º 2
0
 def test_ctor(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><to><a>xyz</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)
     self.assertEqual(getter.mangler, mangler)
Ejemplo n.º 3
0
 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>'])
Ejemplo n.º 4
0
 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>'])
Ejemplo n.º 5
0
    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)