def wrapped(url): api._count += 1 path = xml_response if api._count > 1: root, ext = os.path.splitext(path) path = '%s-%i%s' % (root, api._count, ext) try: if request.config.option.fetch == 'all': raise ResponseRequired try: content = open(path, 'r').read() # If the XML response has been previously fetched compare # request arguments in order to see if there are any changes cached_params = utils.arguments_from_cached_xml(content) current_params = utils.arguments_from_url(url) if cached_params != current_params: raise ArgumentMismatch except IOError: if request.config.option.fetch in ('outdated', 'missing'): raise ResponseRequired raise pytest.skip('No cached XML response found!') except ArgumentMismatch: if request.config.option.fetch == 'outdated': raise ResponseRequired msg = ('Cached arguments in %s differ from the ones ' 'currently tested against!\ncached=%r\ncurrent=%r' % (path, cached_params, current_params)) return pytest.skip(msg) except AttributeError: # XML for error messages have no Argument elements! pass except ResponseRequired: # fetch XML via urllib2 rather than directly via # lxml.etree.parse() to avoid, for instance, problems with HTTP # 403 errors resp = requests.get(url, headers={'User-Agent': USER_AGENT}) root = lxml.etree.fromstring(resp.text) # overwrite sensitive information in XML document. for arg in root.xpath('//aws:Argument', namespaces={'aws': root.nsmap[None]}): if arg.get('Name') in ('Signature', 'AWSAccessKeyId', 'AssociateTag'): arg.set('Value', 'X' * 15) content = lxml.etree.tostring(root, pretty_print=True) # complain loudly about missing credentials # UNLESS it was actually on purpose! if (six.b('MissingClientTokenId') in content and getattr(request.function, 'refetch', True)): raise pytest.fail( 'Cannot fetch XML response without credentials!') if not os.path.exists(os.path.dirname(path)): os.mkdir(os.path.dirname(path)) open(path, 'wb').write(content) # We simply exchange the real host with the local one now! # Note: Although strictly speaking it does not matter which URL is # called exactly, to appeal to one's sense of correctness, let's # keep at least the correct path! url = url_reg.sub(r'%s\g<path>' % server.url, url) server.serve_content(content) return fnc(url)
def wrapped(url): api._count += 1 path = xml_response if api._count > 1: root, ext = os.path.splitext(path) path = '%s-%i%s' % (root, api._count, ext) try: if request.config.option.fetch == 'all': raise ResponseRequired try: content = open(path, 'r').read() # If the XML response has been previously fetched compare # request arguments in order to see if there are any changes cached_params = utils.arguments_from_cached_xml(content) current_params = utils.arguments_from_url(url) if cached_params != current_params: raise ArgumentMismatch except IOError: if request.config.option.fetch in ('outdated', 'missing'): raise ResponseRequired raise pytest.skip('No cached XML response found!') except ArgumentMismatch: if request.config.option.fetch == 'outdated': raise ResponseRequired msg = ('Cached arguments in %s differ from the ones ' 'currently tested against!\ncached=%r\ncurrent=%r' % (path, cached_params, current_params)) return pytest.skip(msg) except AttributeError: # XML for error messages have no Argument elements! pass except ResponseRequired: # fetch XML via urllib2 rather than directly via # lxml.etree.parse() to avoid, for instance, problems with HTTP # 403 errors resp = requests.get(url, headers={'User-Agent': USER_AGENT}) root = lxml.etree.fromstring(resp.text) # overwrite sensitive information in XML document. for arg in root.xpath('//aws:Argument', namespaces={'aws': root.nsmap[None]}): if arg.get('Name') in ('Signature', 'AWSAccessKeyId', 'AssociateTag'): arg.set('Value', 'X'*15) content = lxml.etree.tostring(root, pretty_print=True) # complain loudly about missing credentials # UNLESS it was actually on purpose! if (six.b('MissingClientTokenId') in content and getattr(request.function, 'refetch', True)): raise pytest.fail('Cannot fetch XML response without credentials!') if not os.path.exists(os.path.dirname(path)): os.mkdir(os.path.dirname(path)) open(path, 'wb').write(content) # We simply exchange the real host with the local one now! # Note: Although strictly speaking it does not matter which URL is # called exactly, to appeal to one's sense of correctness, let's # keep at least the correct path! url = url_reg.sub(r'%s\g<path>' % server.url, url) server.serve_content(content) return fnc(url)
def wrapped(url): api._count += 1 path = xml_response if api._count > 1: root, ext = os.path.splitext(path) path = '%s-%i%s' % (root, api._count, ext) try: if request.config.option.fetch == 'all': raise ResponseRequired try: content = open(path, 'r').read() # If the XML response has been previously fetched compare # request arguments in order to see if there are any changes cached_params = utils.arguments_from_cached_xml(content) current_params = utils.arguments_from_url(url) if cached_params != current_params: raise ArgumentMismatch except IOError: if request.config.option.fetch in ('outdated', 'missing'): raise ResponseRequired raise pytest.skip('No cached XML response found!') except ArgumentMismatch: if request.config.option.fetch == 'outdated': raise ResponseRequired return pytest.skip('Cached arguments in %s differ from the ' 'ones currently tested against!' % path) #'\ncached=%r\ncurrent=%r' % (path, #cached_params, current_params)) except AttributeError: # XML for error messages have no Argument elements! pass except ResponseRequired: # fetch XML via urllib2 rather than directly via # lxml.etree.parse() to avoid, for instance, problems with HTTP # 403 errors try: req = urllib2.Request(url, headers={'User-Agent': USER_AGENT}) xml = urllib2.urlopen(req).read() except urllib2.HTTPError, e: xml = e.read() root = lxml.etree.fromstring(xml) # overwrite sensitive information in XML document. for arg in root.xpath('//aws:Argument', namespaces={'aws': root.nsmap[None]}): if arg.get('Name') in ('Signature', 'AWSAccessKeyId', 'AssociateTag'): arg.set('Value', 'X'*15) content = lxml.etree.tostring(root, pretty_print=True) # complain loudly about missing credentials # UNLESS it was actually on purpose! if ('MissingClientTokenId' in content and getattr(request.function, 'refetch', True)): raise pytest.fail('Cannot fetch XML response without credentials!') if not os.path.exists(os.path.dirname(path)): os.mkdir(os.path.dirname(path)) open(path, 'wb').write(content)