From bd2b5181a0741ee01c901050d9d2682f04525b9c Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Fri, 30 Oct 2015 13:18:58 -0700 Subject: [PATCH] tests: add new tests for redis-based cache, #145 --- tests/test_config_proxy_ip_redis.yaml | 21 ++++++ tests/test_proxy_http_ip_redis.py | 98 +++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 tests/test_config_proxy_ip_redis.yaml create mode 100644 tests/test_proxy_http_ip_redis.py diff --git a/tests/test_config_proxy_ip_redis.yaml b/tests/test_config_proxy_ip_redis.yaml new file mode 100644 index 00000000..c7832feb --- /dev/null +++ b/tests/test_config_proxy_ip_redis.yaml @@ -0,0 +1,21 @@ +collections: + all: + - ./sample_archive/cdx/iana.cdx + - ./sample_archive/cdx/dupes.cdx + - ./sample_archive/cdx/post-test.cdx + +archive_paths: ./sample_archive/warcs/ + +enable_http_proxy: true + +proxy_options: + enable_https_proxy: false + + cookie_resolver: ip + redis_cache_key: redis://localhost:6379/0/proxy:hosts + redis_cache_timeout: 120 + + use_default_coll: all + + use_banner: true + use_client_rewrite: false diff --git a/tests/test_proxy_http_ip_redis.py b/tests/test_proxy_http_ip_redis.py new file mode 100644 index 00000000..c3914070 --- /dev/null +++ b/tests/test_proxy_http_ip_redis.py @@ -0,0 +1,98 @@ +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 urlparse import urlsplit + +from server_mock import make_setup_module, BaseIntegration + +setup_module = make_setup_module('tests/test_config_proxy_ip_redis.yaml') + +from fakeredis import FakeStrictRedis + +import pywb.framework.cache +pywb.framework.cache.StrictRedis = FakeStrictRedis + +class TestProxyIPRedisResolver(BaseIntegration): + def _assert_basic_html(self, resp): + assert resp.status_int == 200 + assert resp.content_type == 'text/html' + assert resp.content_length > 0 + + def _assert_basic_text(self, resp): + assert resp.status_int == 200 + assert resp.content_type == 'text/plain' + assert resp.content_length > 0 + + def get_url(self, uri, addr='127.0.0.1'): + parts = urlsplit(uri) + env = dict(REQUEST_URI=uri, QUERY_STRING=parts.query, SCRIPT_NAME='', REMOTE_ADDR=addr) + # 'Simulating' proxy by settings REQUEST_URI explicitly to full url with empty SCRIPT_NAME + return self.testapp.get('/x-ignore-this-x', extra_environ=env) + + def test_proxy_ip_default_ts(self): + resp = self.get_url('http://www.iana.org/') + self._assert_basic_html(resp) + + assert '"20140127171238"' in resp.body + assert 'wb.js' in resp.body + + def test_proxy_ip_get_defaults(self): + resp = self.get_url('http://info.pywb.proxy/') + assert resp.content_type == 'application/json' + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} + + def test_proxy_ip_set_ts(self): + resp = self.get_url('http://info.pywb.proxy/set?ts=1996') + assert resp.content_type == 'application/json' + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': '1996'} + + def test_proxy_ip_set_ts_coll(self): + resp = self.get_url('http://info.pywb.proxy/set?ts=1996&coll=all') + assert resp.content_type == 'application/json' + 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=2006&coll=all', '127.0.0.2') + assert resp.content_type == 'application/json' + 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': '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'} + + resp = self.get_url('http://info.pywb.proxy/', '127.0.0.2') + assert resp.json == {'ip': '127.0.0.2', 'coll': 'all', 'ts': '2005'} + + def test_proxy_ip_change_ts_for_ip(self): + resp = self.get_url('http://info.pywb.proxy/set?ip=1.2.3.4&ts=20140126200624') + assert resp.json == {'ip': '1.2.3.4', 'coll': None, 'ts': '20140126200624'} + + # different ts for this ip + resp = self.get_url('http://www.iana.org/', '1.2.3.4') + self._assert_basic_html(resp) + + assert '"20140126200624"' in resp.body + + # defaults for any other ip + resp = self.get_url('http://www.iana.org/', '127.0.0.3') + self._assert_basic_html(resp) + assert '"20140127171238"' in resp.body + + def test_proxy_ip_delete_ip(self): + resp = self.get_url('http://info.pywb.proxy/') + assert resp.json == {'ip': '127.0.0.1', 'coll': 'all', 'ts': '1996'} + + resp = self.get_url('http://info.pywb.proxy/set?delete=true') + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} + + resp = self.get_url('http://info.pywb.proxy/') + assert resp.json == {'ip': '127.0.0.1', 'coll': None, 'ts': None} +