mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-15 00:03:28 +01:00
tests refactor! init pywb once per module, instead of once per test
refactor common init pattern to server_mock for now (can add fixtures also)
This commit is contained in:
parent
31dd946114
commit
e249f300e3
28
tests/server_mock.py
Normal file
28
tests/server_mock.py
Normal file
@ -0,0 +1,28 @@
|
||||
from pywb.webapp.pywb_init import create_wb_router
|
||||
from pywb.framework.wsgi_wrappers import init_app
|
||||
from webtest import TestApp
|
||||
|
||||
app = None
|
||||
testapp = None
|
||||
|
||||
def make_app(config_file, pywb_router=create_wb_router):
|
||||
app = init_app(pywb_router,
|
||||
load_yaml=True,
|
||||
config_file=config_file)
|
||||
|
||||
testapp = TestApp(app)
|
||||
|
||||
return app, testapp
|
||||
|
||||
def make_setup_module(config, pywb_router=create_wb_router):
|
||||
def setup_module():
|
||||
global app
|
||||
global testapp
|
||||
app, testapp = make_app(config, pywb_router)
|
||||
|
||||
return setup_module
|
||||
|
||||
class BaseIntegration(object):
|
||||
def setup(self):
|
||||
self.app = app
|
||||
self.testapp = testapp
|
@ -4,16 +4,12 @@ from pywb.framework.wsgi_wrappers import init_app
|
||||
|
||||
from memento_fixture import *
|
||||
|
||||
class TestMementoFrameInverse(MementoMixin):
|
||||
TEST_CONFIG = 'tests/test_config_frames.yaml'
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
setup_module = make_setup_module('tests/test_config_frames.yaml')
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
class TestMementoFrameInverse(MementoMixin, BaseIntegration):
|
||||
def test_top_frame_replay(self):
|
||||
resp = self.testapp.get('/pywb/20140127171238/http://www.iana.org/')
|
||||
|
||||
|
@ -1,25 +1,15 @@
|
||||
from pytest import raises
|
||||
import webtest
|
||||
import base64
|
||||
from pywb.webapp.pywb_init import create_wb_router
|
||||
from pywb.framework.wsgi_wrappers import init_app
|
||||
from pywb.cdx.cdxobject import CDXObject
|
||||
from pywb.utils.timeutils import timestamp_now
|
||||
|
||||
class TestWb:
|
||||
TEST_CONFIG = 'tests/test_config.yaml'
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
def setup(self):
|
||||
#self.app = pywb.wbapp.create_wb_app(pywb.pywb_init.pywb_config())
|
||||
# save it in self - useful for debugging
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
setup_module = make_setup_module('tests/test_config.yaml')
|
||||
|
||||
#self.router = pywb_config(self.TEST_CONFIG)
|
||||
#self.app = create_wb_app(self.router)
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
class TestWbIntegration(BaseIntegration):
|
||||
#def setup(self):
|
||||
# self.app = app
|
||||
# self.testapp = testapp
|
||||
|
||||
def _assert_basic_html(self, resp):
|
||||
assert resp.status_int == 200
|
||||
@ -386,6 +376,16 @@ class TestWb:
|
||||
resp = self.testapp.get('/live/http://example.com/?test=test')
|
||||
assert resp.status_int == 200
|
||||
|
||||
def test_live_redir_1(self):
|
||||
resp = self.testapp.get('/live/*/http://example.com/?test=test')
|
||||
assert resp.status_int == 302
|
||||
assert resp.headers['Location'].endswith('/live/http://example.com/?test=test')
|
||||
|
||||
def test_live_redir_2(self):
|
||||
resp = self.testapp.get('/live/2010-2011/http://example.com/?test=test')
|
||||
assert resp.status_int == 302
|
||||
assert resp.headers['Location'].endswith('/live/http://example.com/?test=test')
|
||||
|
||||
def test_live_fallback(self):
|
||||
resp = self.testapp.get('/pywb-fallback//http://example.com/?test=test')
|
||||
assert resp.status_int == 200
|
||||
|
@ -55,38 +55,57 @@ class MockYTDWrapper(object):
|
||||
|
||||
pywb.webapp.live_rewrite_handler.YoutubeDLWrapper = MockYTDWrapper
|
||||
|
||||
|
||||
#=================================================================
|
||||
def setup_module():
|
||||
global requestlog
|
||||
requestlog = []
|
||||
|
||||
def make_httpd(app):
|
||||
global proxyserv
|
||||
proxyserv = ProxyServer(('', 0), ProxyRequest)
|
||||
proxyserv.requestlog = requestlog
|
||||
proxyserv.force_err = False
|
||||
return proxyserv
|
||||
|
||||
global server
|
||||
server = ServerThreadRunner(make_httpd)
|
||||
|
||||
config = dict(collections=dict(rewrite='$liveweb'),
|
||||
framed_replay=True,
|
||||
proxyhostport=server.proxy_dict)
|
||||
|
||||
global cache
|
||||
cache = {}
|
||||
|
||||
def create_cache():
|
||||
return cache
|
||||
|
||||
pywb.webapp.live_rewrite_handler.create_cache = create_cache
|
||||
|
||||
global app
|
||||
app = init_app(create_wb_router,
|
||||
load_yaml=False,
|
||||
config=config)
|
||||
|
||||
global testapp
|
||||
testapp = webtest.TestApp(app)
|
||||
|
||||
|
||||
def teardown_module(self):
|
||||
server.stop_thread()
|
||||
|
||||
#=================================================================
|
||||
class TestProxyLiveRewriter:
|
||||
def setup(self):
|
||||
self.requestlog = []
|
||||
self.cache = {}
|
||||
self.requestlog = requestlog
|
||||
del self.requestlog[:]
|
||||
|
||||
def make_httpd(app):
|
||||
proxyserv = ProxyServer(('', 0), ProxyRequest)
|
||||
proxyserv.requestlog = self.requestlog
|
||||
proxyserv.force_err = False
|
||||
self.proxyserv = proxyserv
|
||||
return proxyserv
|
||||
self.cache = cache
|
||||
self.cache.clear()
|
||||
|
||||
self.server = ServerThreadRunner(make_httpd)
|
||||
|
||||
config = dict(collections=dict(rewrite='$liveweb'),
|
||||
framed_replay=True,
|
||||
proxyhostport=self.server.proxy_dict)
|
||||
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=False,
|
||||
config=config)
|
||||
|
||||
def create_cache():
|
||||
return self.cache
|
||||
|
||||
pywb.webapp.live_rewrite_handler.create_cache = create_cache
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
def teardown(self):
|
||||
self.server.stop_thread()
|
||||
self.app = app
|
||||
self.testapp = testapp
|
||||
|
||||
def test_echo_proxy_referrer(self):
|
||||
headers = [('User-Agent', 'python'), ('Referer', 'http://localhost:80/rewrite/other.example.com')]
|
||||
@ -211,7 +230,7 @@ class TestProxyLiveRewriter:
|
||||
def test_echo_proxy_error(self):
|
||||
headers = [('Range', 'bytes=1000-2000'), ('Referer', 'http://localhost:80/rewrite/https://example.com/')]
|
||||
|
||||
self.proxyserv.force_err = True
|
||||
proxyserv.force_err = True
|
||||
resp = self.testapp.get('/rewrite/http://www.youtube.com/watch?v=DjFZyFWSt1M', headers=headers)
|
||||
|
||||
# not from proxy
|
||||
|
@ -14,11 +14,18 @@ class MockYTDWrapper(object):
|
||||
pywb.webapp.live_rewrite_handler.YoutubeDLWrapper = MockYTDWrapper
|
||||
|
||||
|
||||
def setup_module():
|
||||
global app
|
||||
global testapp
|
||||
app = LiveCli(['-f']).application
|
||||
testapp = webtest.TestApp(app)
|
||||
|
||||
|
||||
#=================================================================
|
||||
class TestLiveRewriter:
|
||||
def setup(self):
|
||||
self.app = LiveCli(['-f']).application
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
self.app = app
|
||||
self.testapp = testapp
|
||||
|
||||
def test_live_live_1(self):
|
||||
headers = [('User-Agent', 'python'), ('Referer', 'http://localhost:80/live/other.example.com')]
|
||||
|
@ -7,16 +7,12 @@ from pywb.utils.timeutils import timestamp_now
|
||||
|
||||
from memento_fixture import *
|
||||
|
||||
class TestMemento(MementoMixin):
|
||||
TEST_CONFIG = 'tests/test_config_memento.yaml'
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
setup_module = make_setup_module('tests/test_config_memento.yaml')
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
class TestMemento(MementoMixin, BaseIntegration):
|
||||
# Below functionality is for archival (non-proxy) mode
|
||||
# It is designed to conform to Memento protocol Pattern 2.1
|
||||
# http://www.mementoweb.org/guide/rfc/#Pattern2.1
|
||||
|
@ -4,17 +4,11 @@ from pywb.perms.perms_handler import create_perms_checker_app
|
||||
from pywb.perms.perms_handler import ALLOW, BLOCK
|
||||
from pywb.framework.wsgi_wrappers import init_app
|
||||
|
||||
class TestPermsApp:
|
||||
TEST_CONFIG = 'tests/test_config.yaml'
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_perms_checker_app,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
setup_module = make_setup_module('tests/test_config.yaml', create_perms_checker_app)
|
||||
|
||||
class TestPermsApp(BaseIntegration):
|
||||
def test_allow(self):
|
||||
resp = self.testapp.get('/check-access/http://example.com')
|
||||
|
||||
|
@ -6,17 +6,12 @@ from pywb.webapp.pywb_init import create_wb_router
|
||||
from pywb.framework.wsgi_wrappers import init_app
|
||||
from pywb.cdx.cdxobject import CDXObject
|
||||
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
class TestProxyHttpAuth:
|
||||
TEST_CONFIG = 'tests/test_config.yaml'
|
||||
setup_module = make_setup_module('tests/test_config.yaml')
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
class TestProxyHttpAuth(BaseIntegration):
|
||||
def _assert_basic_html(self, resp):
|
||||
assert resp.status_int == 200
|
||||
assert resp.content_type == 'text/html'
|
||||
|
@ -8,17 +8,12 @@ from pywb.cdx.cdxobject import CDXObject
|
||||
|
||||
from urlparse import urlsplit
|
||||
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
class TestProxyIPResolver(object):
|
||||
TEST_CONFIG = 'tests/test_config_proxy_ip.yaml'
|
||||
setup_module = make_setup_module('tests/test_config_proxy_ip.yaml')
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
class TestProxyIPResolver(BaseIntegration):
|
||||
def _assert_basic_html(self, resp):
|
||||
assert resp.status_int == 200
|
||||
assert resp.content_type == 'text/html'
|
||||
@ -58,12 +53,13 @@ class TestProxyIPResolver(object):
|
||||
assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'}
|
||||
|
||||
def test_proxy_ip_set_ts_coll_diff_ip(self):
|
||||
resp = self.get_url('http://info.pywb.proxy/set?ts=1996&coll=all', '127.0.0.2')
|
||||
resp = self.get_url('http://info.pywb.proxy/set?ts=2006&coll=all', '127.0.0.2')
|
||||
assert resp.content_type == 'application/json'
|
||||
assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '1996'}
|
||||
assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2006'}
|
||||
|
||||
# from previous response
|
||||
resp = self.get_url('http://info.pywb.proxy/')
|
||||
assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None}
|
||||
assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'}
|
||||
|
||||
resp = self.get_url('http://info.pywb.proxy/set?ip=127.0.0.2&ts=2005')
|
||||
assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'}
|
||||
@ -82,6 +78,6 @@ class TestProxyIPResolver(object):
|
||||
assert '"20140126200624"' in resp.body
|
||||
|
||||
# defaults for any other ip
|
||||
resp = self.get_url('http://www.iana.org/')
|
||||
resp = self.get_url('http://www.iana.org/', '127.0.0.3')
|
||||
self._assert_basic_html(resp)
|
||||
assert '"20140127171238"' in resp.body
|
||||
|
@ -1,9 +1,12 @@
|
||||
import webtest
|
||||
from pywb.webapp.pywb_init import create_wb_router
|
||||
from pywb.framework.wsgi_wrappers import init_app
|
||||
from pywb.framework.basehandlers import BaseHandler
|
||||
from pywb.framework.wbrequestresponse import WbResponse
|
||||
|
||||
from server_mock import make_setup_module, BaseIntegration
|
||||
|
||||
setup_module = make_setup_module('tests/test_config_root_coll.yaml')
|
||||
|
||||
|
||||
# A custom handler
|
||||
class RedirHandler(BaseHandler):
|
||||
@ -11,16 +14,7 @@ class RedirHandler(BaseHandler):
|
||||
return WbResponse.redir_response(self.redir_path + wbrequest.wb_url_str)
|
||||
|
||||
|
||||
class TestMementoFrameInverse(object):
|
||||
TEST_CONFIG = 'tests/test_config_root_coll.yaml'
|
||||
|
||||
def setup(self):
|
||||
self.app = init_app(create_wb_router,
|
||||
load_yaml=True,
|
||||
config_file=self.TEST_CONFIG)
|
||||
|
||||
self.testapp = webtest.TestApp(self.app)
|
||||
|
||||
class TestMementoFrameInverse(BaseIntegration):
|
||||
def test_timestamp_replay_redir(self):
|
||||
resp = self.testapp.get('/http://www.iana.org/')
|
||||
assert resp.status_int == 302
|
||||
|
Loading…
x
Reference in New Issue
Block a user