def test_notWrapped(self): """Make sure that if we set a non-wrapped fetcher as default, it will not wrap exceptions.""" # A fetcher that will raise an exception when it encounters a # host that will not resolve fetcher = fetchers.Urllib2Fetcher() fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False) self.assertNotIsInstance(fetchers.getDefaultFetcher(), fetchers.ExceptionWrappingFetcher) with self.assertRaises(URLError): fetchers.fetch('http://invalid.janrain.com/')
def test_notWrapped(self): """Make sure that if we set a non-wrapped fetcher as default, it will not wrap exceptions.""" # A fetcher that will raise an exception when it encounters a # host that will not resolve fetcher = fetchers.Urllib2Fetcher() fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False) self.assertFalse( isinstance(fetchers.getDefaultFetcher(), fetchers.ExceptionWrappingFetcher)) try: fetchers.fetch('http://invalid.janrain.com/') except fetchers.HTTPFetchingError: self.fail('Should not be wrapping exception') except Exception as exc: self.assertIsInstance(exc, urllib.error.URLError) pass else: self.fail('Should have raised an exception')
def setUp(self): self.fetcher = fetchers.Urllib2Fetcher()
class TestSilencedUrllib2Fetcher(TestUrllib2Fetcher): """Test silenced `Urllib2Fetcher` class.""" fetcher = fetchers.ExceptionWrappingFetcher(fetchers.Urllib2Fetcher()) invalid_url_error = fetchers.HTTPFetchingError
class TestUrllib2Fetcher(unittest.TestCase): """Test `Urllib2Fetcher` class.""" fetcher = fetchers.Urllib2Fetcher() invalid_url_error = ValueError def setUp(self): self.http_mock = Mock(side_effect=[]) opener = OpenerDirector() opener.add_handler(TestHandler(self.http_mock)) install_opener(opener) def tearDown(self): # Uninstall custom opener install_opener(None) def add_response(self, url, status_code, headers, body=None): response = addinfourl(StringIO(body or ''), headers, url, status_code) responses = list(self.http_mock.side_effect) responses.append(response) self.http_mock.side_effect = responses def test_success(self): # Test success response self.add_response('http://example.cz/success/', 200, {'Content-Type': 'text/plain'}, 'BODY') response = self.fetcher.fetch('http://example.cz/success/') expected = fetchers.HTTPResponse('http://example.cz/success/', 200, {'Content-Type': 'text/plain'}, 'BODY') assertResponse(expected, response) def test_redirect(self): # Test redirect response - a final response comes from another URL. self.add_response('http://example.cz/success/', 200, {'Content-Type': 'text/plain'}, 'BODY') response = self.fetcher.fetch('http://example.cz/redirect/') expected = fetchers.HTTPResponse('http://example.cz/success/', 200, {'Content-Type': 'text/plain'}, 'BODY') assertResponse(expected, response) def test_error(self): # Test error responses - returned as obtained self.add_response('http://example.cz/error/', 500, {'Content-Type': 'text/plain'}, 'BODY') response = self.fetcher.fetch('http://example.cz/error/') expected = fetchers.HTTPResponse('http://example.cz/error/', 500, {'Content-Type': 'text/plain'}, 'BODY') assertResponse(expected, response) def test_invalid_url(self): with six.assertRaisesRegex(self, self.invalid_url_error, 'Bad URL scheme:'): self.fetcher.fetch('invalid://example.cz/') def test_connection_error(self): # Test connection error self.http_mock.side_effect = HTTPError('http://example.cz/error/', 500, 'Error message', {'Content-Type': 'text/plain'}, StringIO('BODY')) response = self.fetcher.fetch('http://example.cz/error/') expected = fetchers.HTTPResponse('http://example.cz/error/', 500, {'Content-Type': 'text/plain'}, 'BODY') assertResponse(expected, response)
logger = logging.getLogger(__name__) user_page = Blueprint('user', __name__) # monkey-patch uidutil.log to use the standard logging framework openid_logger = logging.getLogger('openid') def log_openid_messages(message, level=0): openid_logger.info(message) oidutil.log = log_openid_messages # force the use urllib2 with a timeout fetcher = fetchers.Urllib2Fetcher() fetcher.urlopen = lambda req: urllib2.urlopen(req, timeout=5) fetchers.setDefaultFetcher(fetcher) @user_page.route('/login', methods=['GET', 'POST']) def login(): if 'id' in session: return redirect(url_for('general.index')) errors = list(request.args.getlist('error')) if request.method == 'POST': login_method = request.form.get('login') if login_method == 'musicbrainz': return musicbrainz_login() elif login_method == 'google': return google_login()