From 60d1d03c1c465c5d5bacba5d6cc03edd50a226ad Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Mon, 26 Mar 2012 07:15:49 +0200 Subject: [PATCH] browser2: Handle server issues of postbin.org And move tests outside. --- weboob/tools/browser2/__init__.py | 18 ++++++++ weboob/tools/browser2/browser.py | 46 +------------------ weboob/tools/browser2/test.py | 76 +++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 weboob/tools/browser2/__init__.py create mode 100644 weboob/tools/browser2/test.py diff --git a/weboob/tools/browser2/__init__.py b/weboob/tools/browser2/__init__.py new file mode 100644 index 00000000..b51e0587 --- /dev/null +++ b/weboob/tools/browser2/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2012 Laurent Bachelier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . diff --git a/weboob/tools/browser2/browser.py b/weboob/tools/browser2/browser.py index d87a70f7..e385d0ff 100644 --- a/weboob/tools/browser2/browser.py +++ b/weboob/tools/browser2/browser.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . +from __future__ import absolute_import + import requests from requests.status_codes import codes @@ -203,47 +205,3 @@ class Browser(object): kwargs.setdefault('headers', {}).setdefault('Content-Length', '0') kwargs.setdefault('timeout', self.TIMEOUT) return self.session.request(*args, **kwargs) - - -def test_base(): - b = Browser() - r = b.location('http://httpbin.org/headers') - assert isinstance(r.text, unicode) - assert 'Firefox' in r.text - assert 'python' not in r.text - assert 'identity' not in r.text - assert b.url == 'http://httpbin.org/headers' - - -def test_redirects(): - b = Browser() - b.location('http://httpbin.org/redirect/1') - assert b.url == 'http://httpbin.org/get' - - -def test_brokenpost(): - """ - Tests _fix_redirect() - """ - b = Browser() - # postbin is picky with empty posts. that's good! - r = b.location('http://www.postbin.org/', {}) - # ensures empty data (but not None) does a POST - assert r.request.method == 'POST' - # ensure we were redirected after submitting a post - assert len(r.url) >= len('http://www.postbin.org/') - # send a POST with data - b.location(r.url, {'hello': 'world'}) - r = b.location(r.url + '/feed') - assert 'hello' in r.text - assert 'world' in r.text - - -def test_weboob(): - class BooBrowser(Browser): - PROFILE = Weboob('0.0') - - b = BooBrowser() - r = b.location('http://httpbin.org/headers') - assert 'weboob/0.0' in r.text - assert 'identity' in r.text diff --git a/weboob/tools/browser2/test.py b/weboob/tools/browser2/test.py new file mode 100644 index 00000000..7215cd29 --- /dev/null +++ b/weboob/tools/browser2/test.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2012 Laurent Bachelier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from __future__ import absolute_import + +from .browser import Browser, Weboob + +import requests + +from nose.plugins.skip import SkipTest + + +def test_base(): + b = Browser() + r = b.location('http://httpbin.org/headers') + assert isinstance(r.text, unicode) + assert 'Firefox' in r.text + assert 'python' not in r.text + assert 'identity' not in r.text + assert b.url == 'http://httpbin.org/headers' + + +def test_redirects(): + b = Browser() + b.location('http://httpbin.org/redirect/1') + assert b.url == 'http://httpbin.org/get' + + +def test_brokenpost(): + """ + Tests _fix_redirect() + """ + try: + b = Browser() + # postbin is picky with empty posts. that's good! + r = b.location('http://www.postbin.org/', {}) + # ensures empty data (but not None) does a POST + assert r.request.method == 'POST' + # ensure we were redirected after submitting a post + assert len(r.url) >= len('http://www.postbin.org/') + # send a POST with data + b.location(r.url, {'hello': 'world'}) + r = b.location(r.url + '/feed') + assert 'hello' in r.text + assert 'world' in r.text + except requests.HTTPError, e: + if str(e).startswith('503 '): + raise SkipTest('Quota exceeded') + else: + raise + + +def test_weboob(): + class BooBrowser(Browser): + PROFILE = Weboob('0.0') + + b = BooBrowser() + r = b.location('http://httpbin.org/headers') + assert 'weboob/0.0' in r.text + assert 'identity' in r.text