Add redirector_test and test_cases

parent 2edcd3c4
if ($test_redirect) {
rewrite ^/test/(.*)$ $test_redirect break;
}
if ($test_rewrite_redirect) {
rewrite ^/test/(.*)$ $test_rewrite_redirect break;
return rewrite;
if ($test_permanent_redirect) {
rewrite ^/test/(.*)$ $test_permanent_redirect permanent;
}
if ($test_status=301_redirect) {
rewrite ^/test/(.*)$ $test_status=301_redirect break;
return status=301;
}
if ($test_permanent_redirect) {
rewrite ^/test/(.*)$ $test_permanent_redirect permanent;
}
map $uri $test_redirect {
/test/some-page /test/another-page;
/test/old-page /test/new-page;
/test/very-old-page /test/very-new-page;
~/test^assets/(.+) /test/new-assets/$1;
/test/some-page /other-project/some-page;
/test^$ /test/cgi-bin/index.cgi;
/test([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$ /test/cgi-bin/index.cgi?del=$1-$2;
/test^([0-9A-Za-z]{12})(\.html?|$)$ /test/cgi-bin/index.cgi?op=download1&id=$1;
/test^([0-9A-Za-z]{12})(\/.+|\.html?|$) /test/cgi-bin/index.cgi?op=download1&id=$1&fname=$2;
/test^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$ /test/cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2;
/test^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$) /test/cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3;
/test^checkfiles\.html$ /test/cgi-bin/index.cgi?op=checkfiles;
/test^contact\.html$ /test/cgi-bin/index.cgi?op=contact;
/test^premium\.html$ /test/cgi-bin/index.cgi?op=payments;
/test^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /test/htaccess/htaccess.html;
/test^login\.html$ /test/cgi-bin/index.cgi?op=login;
/test^catalogue(.*)\.html$ /test/cgi-bin/index.cgi?op=catalogue&date=$1;
/test^news([0-9]*)\.html$ /test/cgi-bin/index.cgi?op=news&page=$1;
/test^n([0-9]+)-.*\.html$ /test/cgi-bin/index.cgi?op=news_details&news_id=$1;
/test^free([0-9]+)\.html$ /test/cgi-bin/index.cgi?op=registration&aff_id=$1;
/test^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$) /test/cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2;
/test^/200([0-9])/([^01])(.*)$ /test/$2$3;
/test^embedmp3-([0-9A-Za-z]{12})\.html$ /test/cgi-bin/index.cgi?op=mp3_embed&file_code=$1;
/test^embedmp4-([0-9A-Za-z]{12})\.html$ /test/cgi-bin/index.cgi?op=mp32_embed&file_code=$1;
/test^box$ /test/cgi-bin/index_box.cgi;
}
\ No newline at end of file
map $uri $test_permanent_redirect {
/test/special-project https://github.com/etersoft/special-project;
/test^/manual/(.*)$ http://www.php.net/manual/$1;
/test/java/jwarehouse/org.eclipse.(.*)/(.*) http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.$1/$2;
}
\ No newline at end of file
map $uri $test_rewrite_redirect {
/test/categories /test/categories;
}
\ No newline at end of file
map $uri $test_status=301_redirect {
/test/article-123.html /test/articles/123;
/test/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /test/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html;
/test/2007/phpbb/sending-post-form-data-with-php-curl.html /test/htaccess/sending-post-form-data-with-php-curl.html;
/test/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /test/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html;
}
\ No newline at end of file
if ($test_redirect) {
rewrite ^/test/(.*)$ $test_redirect break;
}
if ($test_permanent_redirect) {
rewrite ^/test/(.*)$ $test_permanent_redirect permanent;
}
if ($test_status=301_redirect) {
rewrite ^/test/(.*)$ $test_status=301_redirect break;
return status=301;
}
map $uri $test_redirect {
/test^$ /test/cgi-bin/index.cgi;
/test([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$ /test/cgi-bin/index.cgi?del=$1-$2;
/test^([0-9A-Za-z]{12})(\.html?|$)$ /test/cgi-bin/index.cgi?op=download1&id=$1;
/test^([0-9A-Za-z]{12})(\/.+|\.html?|$) /test/cgi-bin/index.cgi?op=download1&id=$1&fname=$2;
/test^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$ /test/cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2;
/test^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$) /test/cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3;
/test^checkfiles\.html$ /test/cgi-bin/index.cgi?op=checkfiles;
/test^contact\.html$ /test/cgi-bin/index.cgi?op=contact;
/test^premium\.html$ /test/cgi-bin/index.cgi?op=payments;
/test^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /test/htaccess/htaccess.html;
/test^login\.html$ /test/cgi-bin/index.cgi?op=login;
/test^catalogue(.*)\.html$ /test/cgi-bin/index.cgi?op=catalogue&date=$1;
/test^news([0-9]*)\.html$ /test/cgi-bin/index.cgi?op=news&page=$1;
/test^n([0-9]+)-.*\.html$ /test/cgi-bin/index.cgi?op=news_details&news_id=$1;
/test^free([0-9]+)\.html$ /test/cgi-bin/index.cgi?op=registration&aff_id=$1;
/test^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$) /test/cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2;
/test^/200([0-9])/([^01])(.*)$ /test/$2$3;
/test^embedmp3-([0-9A-Za-z]{12})\.html$ /test/cgi-bin/index.cgi?op=mp3_embed&file_code=$1;
/test^embedmp4-([0-9A-Za-z]{12})\.html$ /test/cgi-bin/index.cgi?op=mp32_embed&file_code=$1;
/test^box$ /test/cgi-bin/index_box.cgi;
}
\ No newline at end of file
map $uri $test_permanent_redirect {
/test^/manual/(.*)$ http://www.php.net/manual/$1;
/test/java/jwarehouse/org.eclipse.(.*)/(.*) http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.$1/$2;
}
\ No newline at end of file
map $uri $test_status=301_redirect {
/test/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /test/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html;
/test/2007/phpbb/sending-post-form-data-with-php-curl.html /test/htaccess/sending-post-form-data-with-php-curl.html;
/test/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /test/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html;
}
\ No newline at end of file
import unittest
from redirector.tests.utils import get_abs_path
from redirector.tests.utils import get_abs_path, get_rel_path
from redirector.conf.config import Config
from redirector.redirector import *
from redirector.utils import generators
from redirector.utils import parser
class TestGenerator(generators.Generator):
def get_conf_dirs(self):
config = Config()
abs_conf_path = get_abs_path('test.ini')
config.read_config(abs_conf_path)
maps_dir = config.get_default_dir('MAPS_DIR')
config_dir = config.get_default_dir('CONFIG_DIR')
return maps_dir, config_dir
class TestRedirector(Redirector):
def __init__(self, logger=None):
self.parser = parser.ConfigReader(logger=logger)
self.generator = TestGenerator()
class EqualRedirectorTest(unittest.TestCase):
def setUp(self):
self.redirector = Redirector()
self.redirector = TestRedirector()
def test_equal_map(self):
config = Config()
abs_config_path = get_abs_path('test.ini')
config.read_config(abs_config_path)
abs_yaml_path = get_abs_path('test.yaml')
abs_test_path = get_abs_path('test.map')
abs_res_conf_path = get_rel_path('nginx_test_case/location-includes/test.conf')
res_conf_file = open(abs_res_conf_path, 'r')
res_conf_lines = []
for line in res_conf_file:
if len(line) > 2 or not line.startswith('#'):
res_conf_lines.append(line)
res_conf_file.close()
maps_dir = get_rel_path('nginx_test_case/maps')
abs_res_map_paths = ['{}/test.map'.format(maps_dir), '{}/test_permanent_options.map'.format(maps_dir),\
'{}/test_status=301_options.map'.format(maps_dir)]
res_map_lines = []
for path in abs_res_map_paths:
f = open(path, 'r')
for line in f:
if len(line) > 2 or not line.startswith('#'):
res_map_lines.append(line)
f.close()
self.redirector.generate(abs_yaml_path, abs_test_path)
abs_test_conf_path = get_rel_path('nginx/location-includes/test.conf')
test_conf_file = open(abs_test_conf_path, 'r')
test_conf_lines = []
for line in test_conf_file:
if len(line) > 2 or not line.startswith('#'):
test_conf_lines.append(line)
test_conf_file.close()
test_maps_dir = get_rel_path('nginx/maps')
abs_test_map_paths = ['{}/test.map'.format(test_maps_dir), '{}/test_permanent_options.map'.format(test_maps_dir),\
'{}/test_status=301_options.map'.format(test_maps_dir)]
test_map_lines = []
for path in abs_test_map_paths:
f = open(path, 'r')
for line in f:
if len(line) > 2 or not line.startswith('#'):
test_map_lines.append(line)
f.close()
self.assertEqual(res_conf_lines, test_conf_lines)
self.assertEqual(res_map_lines, test_map_lines)
[DEFAULT]
nginx_dir = '/home/kantegory/pvt/nginx-redirector/tests/nginx'
nginx_dir = /home/kantegory/pvt/nginx-redirector/redirector/tests/nginx
maps_dir = %(nginx_dir)s/maps
config_dir = %(nginx_dir)s/location-includes
......@@ -8,3 +8,12 @@ def get_abs_path(fname):
return abs_path
def get_rel_path(fname):
curr_dir = os.path.dirname(os.path.realpath(__file__))
rel_path = os.path.relpath(fname, start=curr_dir)
abs_path = os.path.join(rel_path, curr_dir, fname)
abs_path = os.path.normpath(abs_path)
return abs_path
......@@ -5,24 +5,36 @@ class Generator:
self.map_gen = MapGenerator()
self.conf_gen = ConfigGenerator()
def get_conf_dirs(self):
config = Config()
config.read_config()
maps_dir = config.get_default_dir('MAPS_DIR')
config_dir = config.get_default_dir('CONFIG_DIR')
return maps_dir, config_dir
def generate(self, redirects_data, project_name):
redirects_map = self.map_gen.generate_map(redirects_data[0], project_name)
redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], project_name)
conf_data = self.conf_gen.generate_config(project_name, [code for code, data in redirects_with_options])
config = Config()
config.read_config()
#config = Config()
#config.read_config()
maps_dir, config_dir = self.get_conf_dirs()
try:
with open(config.get_default_dir('MAPS_DIR') + "/%s.map" % project_name, "w") as map_file:
with open(maps_dir + "/%s.map" % project_name, "w") as map_file:
map_file.write(redirects_map)
for code, data in redirects_with_options:
if code == "301":
code = "permanent"
elif code == "302":
code = "redirect"
with open(config.get_default_dir('MAPS_DIR') + "/%s_%s_options.map" % (project_name, code), "w") as map_file:
with open(maps_dir + "/%s_%s_options.map" % (project_name, code), "w") as map_file:
map_file.write(data)
with open(config.get_default_dir('CONFIG_DIR') + "/%s.conf" % project_name, "w") as conf_file:
with open(config_dir + "/%s.conf" % project_name, "w") as conf_file:
conf_file.write(conf_data)
except Exception as e:
print(e)
......
# comment
/some-page /another-page
/old-page /new-page
/very-old-page /very-new-page
# regexp
~^assets/(.+) /new-assets/$1
# additional redirect options
/categories /categories [rewrite]
/article-123.html /articles/123 [status=301]
# root-relative urls
/some-page //other-project/some-page
# absolute redirect
/special-project https://github.com/etersoft/special-project
# RewriteRule ^$ /cgi-bin/index.cgi [L]
^$ /cgi-bin/index.cgi
# RewriteRule ([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$ /cgi-bin/index.cgi?del=$1-$2 [L]
([0-9A-Za-z]{12})-del-([0-9A-Za-z]+)/.+$ /cgi-bin/index.cgi?del=$1-$2
# RedirectMatch 301 ^/manual/(.*)$ http://www.php.net/manual/$1
^/manual/(.*)$ http://www.php.net/manual/$1
# RewriteRule ^([0-9A-Za-z]{12})(\.html?|$)$ /cgi-bin/index.cgi?op=download1&id=$1 [L]
^([0-9A-Za-z]{12})(\.html?|$)$ /cgi-bin/index.cgi?op=download1&id=$1
# RewriteRule ^([0-9A-Za-z]{12})(\/.+|\.html?|$) /cgi-bin/index.cgi?op=download1&id=$1&fname=$2 [L]
^([0-9A-Za-z]{12})(\/.+|\.html?|$) /cgi-bin/index.cgi?op=download1&id=$1&fname=$2
# RedirectMatch 301 /java/jwarehouse/org.eclipse.(.*)/(.*) http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.$1/$2
/java/jwarehouse/org.eclipse.(.*)/(.*) http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.$1/$2
# RewriteRule ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$ /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2 [L]
^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})$ /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2
# RewriteRule ^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$) /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3 [L]
^([0-9A-Za-z\-_]{4,64})/([0-9A-Za-z]{12})(\/.+|\.html?|$) /cgi-bin/index.cgi?op=download1&usr_login=$1&id=$2&fname=$3
# RewriteRule ^checkfiles\.html$ /cgi-bin/index.cgi?op=checkfiles [L]
^checkfiles\.html$ /cgi-bin/index.cgi?op=checkfiles
# Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html
/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html [status=301]
# RewriteRule ^contact\.html$ /cgi-bin/index.cgi?op=contact [L]
^contact\.html$ /cgi-bin/index.cgi?op=contact
# RewriteRule ^premium\.html$ /cgi-bin/index.cgi?op=payments [L]
^premium\.html$ /cgi-bin/index.cgi?op=payments
# RedirectMatch 301 ^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /htaccess/htaccess.html
^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /htaccess/htaccess.html
# RewriteRule ^login\.html$ /cgi-bin/index.cgi?op=login [L]
^login\.html$ /cgi-bin/index.cgi?op=login
# Redirect 301 /2007/phpbb/sending-post-form-data-with-php-curl.html /htaccess/sending-post-form-data-with-php-curl.html
/2007/phpbb/sending-post-form-data-with-php-curl.html /htaccess/sending-post-form-data-with-php-curl.html [status=301]
# RewriteRule ^catalogue(.*)\.html$ /cgi-bin/index.cgi?op=catalogue&date=$1 [L]
^catalogue(.*)\.html$ /cgi-bin/index.cgi?op=catalogue&date=$1
# RewriteRule ^news([0-9]*)\.html$ /cgi-bin/index.cgi?op=news&page=$1 [L]
^news([0-9]*)\.html$ /cgi-bin/index.cgi?op=news&page=$1
# Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html
/2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html [status=301]
# RewriteRule ^n([0-9]+)-.*\.html$ /cgi-bin/index.cgi?op=news_details&news_id=$1 [L]
^n([0-9]+)-.*\.html$ /cgi-bin/index.cgi?op=news_details&news_id=$1
# RewriteRule ^free([0-9]+)\.html$ /cgi-bin/index.cgi?op=registration&aff_id=$1 [L]
^free([0-9]+)\.html$ /cgi-bin/index.cgi?op=registration&aff_id=$1
# RewriteRule ^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$) /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2 [L]
^users/([0-9A-Za-z\-_]{4,64})/?([0-9]+|$) /cgi-bin/index.cgi?op=user_public&usr_login=$1&fld_id=$2
# RedirectMatch 301 ^/200([0-9])/([^01])(.*)$ /$2$3
^/200([0-9])/([^01])(.*)$ /$2$3
# RewriteRule ^embedmp3-([0-9A-Za-z]{12})\.html$ /cgi-bin/index.cgi?op=mp3_embed&file_code=$1 [L]
^embedmp3-([0-9A-Za-z]{12})\.html$ /cgi-bin/index.cgi?op=mp3_embed&file_code=$1
# RewriteRule ^embedmp4-([0-9A-Za-z]{12})\.html$ /cgi-bin/index.cgi?op=mp32_embed&file_code=$1 [L]
^embedmp4-([0-9A-Za-z]{12})\.html$ /cgi-bin/index.cgi?op=mp32_embed&file_code=$1
# RewriteRule ^box$ /cgi-bin/index_box.cgi [L]
^box$ /cgi-bin/index_box.cgi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment