|
| 1 | +from pytest import raises |
| 2 | +import webtest |
| 3 | +import base64 |
| 4 | + |
| 5 | +from pywb.webapp.pywb_init import create_wb_router |
| 6 | +from pywb.framework.wsgi_wrappers import init_app |
| 7 | +from pywb.cdx.cdxobject import CDXObject |
| 8 | + |
| 9 | +from urlparse import urlsplit |
| 10 | + |
| 11 | +from server_mock import make_setup_module, BaseIntegration |
| 12 | + |
| 13 | +setup_module = make_setup_module('tests/test_config_proxy_ip_redis.yaml') |
| 14 | + |
| 15 | +from fakeredis import FakeStrictRedis |
| 16 | + |
| 17 | +import pywb.framework.cache |
| 18 | +pywb.framework.cache.StrictRedis = FakeStrictRedis |
| 19 | + |
| 20 | +class TestProxyIPRedisResolver(BaseIntegration): |
| 21 | + def _assert_basic_html(self, resp): |
| 22 | + assert resp.status_int == 200 |
| 23 | + assert resp.content_type == 'text/html' |
| 24 | + assert resp.content_length > 0 |
| 25 | + |
| 26 | + def _assert_basic_text(self, resp): |
| 27 | + assert resp.status_int == 200 |
| 28 | + assert resp.content_type == 'text/plain' |
| 29 | + assert resp.content_length > 0 |
| 30 | + |
| 31 | + def get_url(self, uri, addr='127.0.0.1'): |
| 32 | + parts = urlsplit(uri) |
| 33 | + env = dict(REQUEST_URI=uri, QUERY_STRING=parts.query, SCRIPT_NAME='', REMOTE_ADDR=addr) |
| 34 | + # 'Simulating' proxy by settings REQUEST_URI explicitly to full url with empty SCRIPT_NAME |
| 35 | + return self.testapp.get('/x-ignore-this-x', extra_environ=env) |
| 36 | + |
| 37 | + def test_proxy_ip_default_ts(self): |
| 38 | + resp = self.get_url('http://www.iana.org/') |
| 39 | + self._assert_basic_html(resp) |
| 40 | + |
| 41 | + assert '"20140127171238"' in resp.body |
| 42 | + assert 'wb.js' in resp.body |
| 43 | + |
| 44 | + def test_proxy_ip_get_defaults(self): |
| 45 | + resp = self.get_url('http://info.pywb.proxy/') |
| 46 | + assert resp.content_type == 'application/json' |
| 47 | + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} |
| 48 | + |
| 49 | + def test_proxy_ip_set_ts(self): |
| 50 | + resp = self.get_url('http://info.pywb.proxy/set?ts=1996') |
| 51 | + assert resp.content_type == 'application/json' |
| 52 | + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': '1996'} |
| 53 | + |
| 54 | + def test_proxy_ip_set_ts_coll(self): |
| 55 | + resp = self.get_url('http://info.pywb.proxy/set?ts=1996&coll=all') |
| 56 | + assert resp.content_type == 'application/json' |
| 57 | + assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'} |
| 58 | + |
| 59 | + def test_proxy_ip_set_ts_coll_diff_ip(self): |
| 60 | + resp = self.get_url('http://info.pywb.proxy/set?ts=2006&coll=all', '127.0.0.2') |
| 61 | + assert resp.content_type == 'application/json' |
| 62 | + assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2006'} |
| 63 | + |
| 64 | + # from previous response |
| 65 | + resp = self.get_url('http://info.pywb.proxy/') |
| 66 | + assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'} |
| 67 | + |
| 68 | + resp = self.get_url('http://info.pywb.proxy/set?ip=127.0.0.2&ts=2005') |
| 69 | + assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'} |
| 70 | + |
| 71 | + resp = self.get_url('http://info.pywb.proxy/', '127.0.0.2') |
| 72 | + assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'} |
| 73 | + |
| 74 | + def test_proxy_ip_change_ts_for_ip(self): |
| 75 | + resp = self.get_url('http://info.pywb.proxy/set?ip=1.2.3.4&ts=20140126200624') |
| 76 | + assert resp.json == {'ip': '1.2.3.4', 'coll': None, 'ts': '20140126200624'} |
| 77 | + |
| 78 | + # different ts for this ip |
| 79 | + resp = self.get_url('http://www.iana.org/', '1.2.3.4') |
| 80 | + self._assert_basic_html(resp) |
| 81 | + |
| 82 | + assert '"20140126200624"' in resp.body |
| 83 | + |
| 84 | + # defaults for any other ip |
| 85 | + resp = self.get_url('http://www.iana.org/', '127.0.0.3') |
| 86 | + self._assert_basic_html(resp) |
| 87 | + assert '"20140127171238"' in resp.body |
| 88 | + |
| 89 | + def test_proxy_ip_delete_ip(self): |
| 90 | + resp = self.get_url('http://info.pywb.proxy/') |
| 91 | + assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'} |
| 92 | + |
| 93 | + resp = self.get_url('http://info.pywb.proxy/set?delete=true') |
| 94 | + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} |
| 95 | + |
| 96 | + resp = self.get_url('http://info.pywb.proxy/') |
| 97 | + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} |
| 98 | + |
0 commit comments