コード例 #1
0
 def test_store_with_extractor2(self):
     elements = {
         'cmd': XPathValue('/A/Command', extractor=lambda x: x[:-7]),
         'cmd2': XPathValue('/A/Command2')
     }
     mangler = self._make(elements=elements)
     result = mangler.store(
         '<A><Command>FQC1GBP/EUR/25Oct14</Command><Command2>FQC1GBP/EUR/25Oct14</Command2></A>'
     )
     self.assertEqual(
         result,
         '<A><Command>FQC1GBP/EUR/</Command><Command2>***</Command2></A>')
コード例 #2
0
 def test_mangle_attrs_not_copied_on_matches(self):
     elements = {
         'd': XPathValue('//dateTime/day'),
         'y': XPathValue('//dateTime/year')
     }
     mangler = self._make(elements=elements, copy_attrs_on_match=False)
     xml = '<a name="foo"><dateTime><year>2014</year><day daylight="false">12</day></dateTime></a>'
     result = mangler.mangle(xml, d="'01'", y="'2015'")
     self.assertEqual(
         result,
         '<a name="foo"><dateTime><year>2015</year><day>01</day></dateTime></a>'
     )
コード例 #3
0
 def test_mangle_with_wildcards(self):
     elements = {
         'd': XPathValue('//dateTime/day'),
         'y': XPathValue('//dateTime/year')
     }
     mangler = self._make(elements=elements)
     xml = '<a><dateTime><year>2014</year><day>12</day></dateTime></a>'
     from stubo.ext import eye_catcher
     result = mangler.mangle(xml, d=eye_catcher, y="'2015'")
     self.assertEqual(
         result,
         '<a><dateTime><year>2015</year><day>***</day></dateTime></a>')
コード例 #4
0
 def test_mangle_element_and_attr(self):
     attrs = {'daylight': XPathValue('//day/@daylight-savings')}
     elements = {
         'd': XPathValue('//dateTime/day'),
         'y': XPathValue('//dateTime/year')
     }
     mangler = self._make(elements=elements, attrs=attrs)
     xml = '<a><dateTime><year>2014</year><day daylight-savings="true">12</day></dateTime></a>'
     result = mangler.mangle(xml, d="'01'", y="'2015'", daylight="'false'")
     self.assertEqual(
         result,
         '<a><dateTime><year>2015</year><day daylight-savings="false">01</day></dateTime></a>'
     )
コード例 #5
0
 def test_mangle_date_with_embedded_quotes(self):
     elements = {
         'd': XPathValue('//dateTime/day'),
         'y': XPathValue('//dateTime/year')
     }
     mangler = self._make(elements=elements)
     xml = '<a><dateTime><year>2014</year><day>12</day></dateTime></a>'
     from lxml import etree
     result = mangler.mangle(
         xml, d=etree.XSLT.strparam(""" It's "the first" """), y="'2015'")
     self.assertEqual(
         result,
         """<a><dateTime><year>2015</year><day> It's "the first" </day></dateTime></a>"""
     )
コード例 #6
0
 def test_mangle_namespaces_ignore_non_matching_path(self):
     elements = {
         'd': XPathValue('//user:dateTime/info:day'),
         'y': XPathValue('//user:dateTime/info:yearxxx')
     }
     namespaces = dict(user="******",
                       info="http://www.my.com/infoschema")
     mangler = self._make(elements=elements, namespaces=namespaces)
     xml = """<a xmlns:user="******" xmlns:info="http://www.my.com/infoschema">
              <user:dateTime><info:year>2014</info:year><info:day>12</info:day>
              </user:dateTime></a>"""
     result = mangler.mangle(xml, d="'01'", y="'2015'")
     self.assertEqual(
         result,
         '<a xmlns:user="******" xmlns:info="http://www.my.com/infoschema"><user:dateTime><info:year>2014</info:year><info:day>01</info:day></user:dateTime></a>'
     )
コード例 #7
0
 def test_mangle_attr2(self):
     attrs = {'daylight': XPathValue('//day/@daylight-savings')}
     mangler = self._make(attrs=attrs)
     xml = '<a><dateTime><year>2014</year><day daylight-savings="true">12</day></dateTime></a>'
     result = mangler.mangle(xml, daylight="'false'")
     self.assertEqual(
         result,
         '<a><dateTime><year>2014</year><day daylight-savings="false">12</day></dateTime></a>'
     )
コード例 #8
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)
コード例 #9
0
    def test_response_namespace(self):
        from stubo.ext.xmlutils import XMLMangler

        mangler = XMLMangler(elements=dict(a=XPathValue('/path/to/a')))
        response_mangler = XMLMangler(elements=dict(
            a=XPathValue('//user:response', extractor=lambda x: x.upper())),
                                      copy_attrs_on_match=True,
                                      namespaces=dict(
                                          user="******"))

        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>',
                '<x xmlns:user="******"><user:response>abc</user:response></x>'
            ), "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'<x xmlns:user="******">'
                    u'<user:response>ABC</user:response></x>',
                    'status': 200
                }
            })
コード例 #10
0
    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)
コード例 #11
0
 def test_mangle_element_and_attr_namespaces(self):
     attrs = {
         'daylight':
         XPathValue('//user:dateTime/info:day/@daylight-savings')
     }
     elements = {
         'd': XPathValue('//user:dateTime/info:day'),
         'y': XPathValue('//user:dateTime/info:year')
     }
     namespaces = dict(user="******",
                       info="http://www.my.com/infoschema")
     mangler = self._make(elements=elements,
                          attrs=attrs,
                          namespaces=namespaces)
     xml = """<a xmlns:user="******" xmlns:info="http://www.my.com/infoschema">
              <user:dateTime><info:year>2014</info:year><info:day daylight-savings="true">12</info:day>
              </user:dateTime></a>"""
     result = mangler.mangle(xml, d="'01'", y="'2015'", daylight="'false'")
     self.assertEqual(
         result,
         '<a xmlns:user="******" xmlns:info="http://www.my.com/infoschema"><user:dateTime><info:year>2015</info:year><info:day daylight-savings="false">01</info:day></user:dateTime></a>'
     )
コード例 #12
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>'])
コード例 #13
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>'])
コード例 #14
0
ファイル: response.py プロジェクト: kwu83tw/stubo-app
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

# exit that rolls response dates


def roll_dt(date_str):
    date_str = date_str.strip()
    # roll date part of string
    return "{{% raw roll_date('{0}', as_date({1}), as_date({2})) %}}".format(
        date_str, 'recorded_on', 'played_on')


response_elements = dict(deparature_date=XPathValue('//DepartureDate',
                                                    extractor=roll_dt),
                         arrival_date=XPathValue('//ArrivalDate',
                                                 extractor=roll_dt))

namespaces = dict(soapenv="http://schemas.xmlsoap.org/soap/envelope/")

exit = XMLManglerExit(response_elements=response_elements,
                      response_namespaces=namespaces)


def exits(request, context):
    return exit.get_exit(request, context)
コード例 #15
0
 def test_store(self):
     elements = {'cmd': XPathValue('/A/Command')}
     mangler = self._make(elements=elements)
     result = mangler.store('<A><Command>FQC1GBP/EUR/25Oct14</Command></A>')
     self.assertEqual(result, '<A><Command>***</Command></A>')
コード例 #16
0
 def test_ctor_attrs(self):
     mangler = self._make(attrs=dict(
         daylight=XPathValue('@daylight-savings')))
     self.assertTrue('daylight' in mangler.attrs)
コード例 #17
0
 def test_ctor_elements(self):
     mangler = self._make(elements=dict(d=XPathValue('//dateTime/day')))
     self.assertTrue('d' in mangler.elements)
コード例 #18
0
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

elements = dict(year=XPathValue('//dispatchTime/dateTime/year'),
                month=XPathValue('//dispatchTime/dateTime/month'),
                day=XPathValue('//dispatchTime/dateTime/day'),
                hour=XPathValue('//dispatchTime/dateTime/hour'),
                minutes=XPathValue('//dispatchTime/dateTime/minutes'),
                seconds=XPathValue('//dispatchTime/dateTime/seconds'))

response_elements = dict(a=XPathValue('//a', extractor=lambda x: 'hello'))
    
exit = XMLManglerExit(elements=elements, response_elements=response_elements)
    
def exits(request, context):
    return exit.get_exit(request, context)
    


        


コード例 #19
0
import logging
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

log = logging.getLogger(__name__)

# remove date from <Command> value
"""
<X>
    <Command>FQC1GBP/EUR/20Oct14</Command>
</X>
"""        

elements = dict(screen_query=XPathValue('//X/Command', extractor=lambda x: x[:-7]))

    
exit = XMLManglerExit(elements=elements)
    
def exits(request, context):
    return exit.get_exit(request, context)
    


        


コード例 #20
0
ファイル: ocd.py プロジェクト: romeuflores/mirage
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

# exit that deals with responses with and without namespaces with various extractors 

def roll_dt(date_str):
    # source_date = '2014-12-10T15:30'
    # roll date part of string
    date_str = date_str.strip()
    return "{{% raw roll_date('{0}', as_date(recorded_on), as_date(played_on)) %}}".format(date_str)
                                                    
response_elements = dict(a=XPathValue('//response/b', extractor=lambda x: '{% raw getresponse_arg %}'),
                         b=XPathValue('//response/c', extractor=lambda x: "{{xmltree.xpath('/request/dt')[0].text}}"),
                         dt=XPathValue('//user:dt', extractor=roll_dt),
                         userid=XPathValue('//user:InformationSystemUserIdentity/info:UserId',
                                           extractor=lambda x: "{{xmltree.xpath('/request2/user')[0].text}}"))
    
exit = XMLManglerExit(response_elements=response_elements,
                      response_namespaces=dict(user="******", 
                                               info="http://www.my.com/infoschema"))
    
def exits(request, context):
    return exit.get_exit(request, context)
コード例 #21
0
ファイル: ignore.py プロジェクト: roopeshpraj/mirage
import logging
from stubo.ext.xmlutils import XPathValue, ignore_children
from stubo.ext.xmlexit import XMLManglerExit

log = logging.getLogger(__name__)

elements = dict(header=XPathValue('//header', ignore_children),
                miss1=XPathValue('/you_wont_find_me', ignore_children),
                miss2=XPathValue('//empty_el'),
                year=XPathValue('//dispatchTime/dateTime/year'),
                month=XPathValue('//dispatchTime/dateTime/month'),
                day=XPathValue('//dispatchTime/dateTime/day'),
                hour=XPathValue('//dispatchTime/dateTime/hour'),
                minutes=XPathValue('//dispatchTime/dateTime/minutes'),
                seconds=XPathValue('//dispatchTime/dateTime/seconds'))
    
ignore = XMLManglerExit(elements=elements)
    
def exits(request, context):
    return ignore.get_exit(request, context)
    


        


コード例 #22
0
import logging
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

log = logging.getLogger(__name__)

attrs = dict(year=XPathValue('//dispatchTime/date/@year'),
             month=XPathValue('//dispatchTime/date/@month'),
             day=XPathValue('//dispatchTime/date/@day'))

ignore = XMLManglerExit(attrs=attrs)


def exits(request, context):
    return ignore.get_exit(request, context)
コード例 #23
0
ファイル: ignore.py プロジェクト: roopeshpraj/mirage
import logging
from stubo.ext.xmlutils import XPathValue
from stubo.ext.xmlexit import XMLManglerExit

log = logging.getLogger(__name__)

elements = dict(year=XPathValue('//dispatchTime/dateTime/year',
                                extractor=lambda x: x),
                month=XPathValue('//dispatchTime/dateTime/month'),
                day=XPathValue('//dispatchTime/dateTime/day'),
                hour=XPathValue('//dispatchTime/dateTime/hour'),
                minutes=XPathValue('//dispatchTime/dateTime/minutes'),
                seconds=XPathValue('//dispatchTime/dateTime/seconds'))

attrs = dict(y=XPathValue('//dispatchTime/date/@year'),
             m=XPathValue('//dispatchTime/date/@month'),
             d=XPathValue('//dispatchTime/date/@day'))

ignore = XMLManglerExit(elements=elements, attrs=attrs)


def exits(request, context):
    return ignore.get_exit(request, context)