Exemplo n.º 1
0
    def test_defaultFetcher(self):
        """util._defaultFetcher"""
        if mock:

            class Response(object):
                """urllib2.Reponse mock"""
                def __init__(self, url,
                             contenttype, content,
                             exception=None, args=None):
                    self.url = url

                    mt, params = cgi.parse_header(contenttype)
                    self.mimetype = mt
                    self.charset = params.get('charset', None)

                    self.text = content

                    self.exception = exception
                    self.args = args

                def geturl(self):
                    return self.url

                def info(self):
                    mimetype, charset = self.mimetype, self.charset
                    class Info(object):
                        def gettype(self):
                            return mimetype
                        def getparam(self, name):
                            return charset

                    return Info()

                def read(self):
                    # returns fake text or raises fake exception
                    if not self.exception:
                        return self.text
                    else:
                        raise self.exception(*self.args)

            def urlopen(url,
                        contenttype=None, content=None,
                        exception=None, args=None):
                # return an mock which returns parameterized Response
                def x(*ignored):
                    if exception:
                        raise exception(*args)
                    else:
                        return Response(url,
                                        contenttype, content,
                                        exception=exception, args=args)
                return x

            # positive tests
            tests = {
                # content-type, contentstr: encoding, contentstr
                ('text/css', u'€'.encode('utf-8')):
                        (None, u'€'.encode('utf-8')),
                ('text/css;charset=utf-8', u'€'.encode('utf-8')):
                        ('utf-8', u'€'.encode('utf-8')),
                ('text/css;charset=ascii', 'a'):
                        ('ascii', 'a')
            }
            url = 'http://example.com/test.css'
            for (contenttype, content), exp in tests.items():

                mock("urllib2.urlopen", mock_obj=urlopen(url, contenttype, content))

                #print url, exp == _readUrl(url, encoding), exp, _readUrl(url, encoding)
                self.assertEqual(exp, _defaultFetcher(url))

            # wrong mimetype
            mock("urllib2.urlopen", mock_obj=urlopen(url, 'text/html', 'a'))
            self.assertRaises(ValueError, _defaultFetcher, url)

            # calling url results in fake exception
            tests = {
                '1': (ValueError, ['invalid value for url']),
                'e2': (urllib2.HTTPError, ['u', 500, 'server error', {}, None]),
                #_readUrl('http://cthedot.de/__UNKNOWN__.css')
                'e3': (urllib2.HTTPError, ['u', 404, 'not found', {}, None]),
                #_readUrl('mailto:a.css')
                'mailto:e4': (urllib2.URLError, ['urlerror']),
                # cannot resolve x, IOError
                'http://x': (urllib2.URLError, ['ioerror']),
            }
            for url, (exception, args) in tests.items():
                mock("urllib2.urlopen",
                        mock_obj=urlopen(url, exception=exception, args=args))
                self.assertRaises(exception, _defaultFetcher, url)

            restore()
Exemplo n.º 2
0
 def do(url):
     return _defaultFetcher(url)