<commit_before>from os.path import join, realpath

from aspen.load import Mixin as Config
from aspen.tests import assert_raises
from aspen.tests.fsfix import mk, attach_rm
from aspen.website import Website as _Website


# Fixture
# =======

import random

class Foo:
    pass

def Website():
    config = Config()
    config.paths = Foo()
    config.paths.root = 'fsfix'
    config.paths.__ = 'fsfix/__'
    config.apps = config.load_apps()
    return _Website(config)

def start_response(status, headers, exc=None):
    def write():
        return status, headers
    return write


# Working
# =======

def test_get_app():
    mk('__', '__/etc', ('__/etc/apps.conf', '/ random:choice'))
    expected = random.choice
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual

def test_get_app_no_app():
    expected = None
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual


# environ changes
# ===============
# SCRIPT_NAME, PATH_INFO

def test_get_app_environ_basic():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', realpath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/', 'SERVER_NAME': 'foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/')
               , ('PATH_TRANSLATED', realpath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
               , ('SERVER_NAME', 'foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_without_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '')
               , ('PATH_TRANSLATED', realpath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_with_slash_and_slash_goes_in_PATH_INFO():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo/ random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', realpath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_without_slash_and_slash_goes_in_PATH_INFO():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', realpath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_root_app():
    mk('__', '__/etc', ('__/etc/apps.conf', '/ random:choice'))
    env = {'PATH_INFO':'/', 'SERVER_NAME': 'foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/')
               , ('PATH_TRANSLATED', realpath('fsfix'))
               , ('SCRIPT_NAME', '')
               , ('SERVER_NAME', 'foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual


# Example in docs
# ===============

EXAMPLE = """

/foo        random:choice   # will get both /foo and /foo/
/bar/       random:sample   # /bar will redirect to /bar/
/bar/baz    random:shuffle  # will 'steal' some of /bar's requests

"""

def test_get_app_doc_example():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = None
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_foo_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.choice
    environ = {'PATH_INFO':'/foo', 'SERVER_NAME':'foo'}
    actual = Website().get_app(environ, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_foo_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.choice
    actual = Website().get_app({'PATH_INFO':'/foo/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    environ = { 'wsgi.url_scheme':'http'
              , 'SERVER_NAME':'foo'
              , 'SERVER_PORT':'80'
              , 'PATH_INFO':'/bar'
               }
    expected = ['Resource moved to: http://foo/bar/']
    actual = Website().get_app(environ, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.sample
    actual = Website().get_app({'PATH_INFO':'/bar/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_with_slash_and_more():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz/buz/biz.html'}, start_response)
    assert actual == expected, actual


# Remove the filesystem fixture after each test.
# ==============================================

attach_rm(globals(), 'test_')<commit_msg>tests passing<commit_after>from os.path import join, abspath

from aspen.load import Mixin as Config
from aspen.tests import assert_raises
from aspen.tests.fsfix import mk, attach_rm
from aspen.website import Website as _Website


# Fixture
# =======

import random

class Foo:
    pass

def Website():
    config = Config()
    config.paths = Foo()
    config.paths.root = 'fsfix'
    config.paths.__ = 'fsfix/__'
    config.apps = config.load_apps()
    return _Website(config)

def start_response(status, headers, exc=None):
    def write():
        return status, headers
    return write


# Working
# =======

def test_get_app():
    mk('__', '__/etc', ('__/etc/apps.conf', '/ random:choice'))
    expected = random.choice
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual

def test_get_app_no_app():
    expected = None
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual


# environ changes
# ===============
# SCRIPT_NAME, PATH_INFO

def test_get_app_environ_basic():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', abspath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/', 'SERVER_NAME': 'foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/')
               , ('PATH_TRANSLATED', abspath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
               , ('SERVER_NAME', 'foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_without_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '')
               , ('PATH_TRANSLATED', abspath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_with_slash_and_slash_goes_in_PATH_INFO():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo/ random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', abspath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_without_slash_and_slash_goes_in_PATH_INFO():
    mk('__', '__/etc', ('__/etc/apps.conf', '/foo random:choice'))
    env = {'PATH_INFO':'/foo/bar'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/bar')
               , ('PATH_TRANSLATED', abspath(join('fsfix', 'foo')))
               , ('SCRIPT_NAME', '/foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual

def test_get_app_environ_root_app():
    mk('__', '__/etc', ('__/etc/apps.conf', '/ random:choice'))
    env = {'PATH_INFO':'/', 'SERVER_NAME': 'foo'}
    Website().get_app(env, start_response)
    expected = [ ('PATH_INFO', '/')
               , ('PATH_TRANSLATED', abspath('fsfix'))
               , ('SCRIPT_NAME', '')
               , ('SERVER_NAME', 'foo')
                ]
    actual = list(env.items())
    actual.sort()
    assert actual == expected, actual


# Example in docs
# ===============

EXAMPLE = """

/foo        random:choice   # will get both /foo and /foo/
/bar/       random:sample   # /bar will redirect to /bar/
/bar/baz    random:shuffle  # will 'steal' some of /bar's requests

"""

def test_get_app_doc_example():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = None
    actual = Website().get_app({'PATH_INFO':'/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_foo_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.choice
    environ = {'PATH_INFO':'/foo', 'SERVER_NAME':'foo'}
    actual = Website().get_app(environ, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_foo_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.choice
    actual = Website().get_app({'PATH_INFO':'/foo/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    environ = { 'wsgi.url_scheme':'http'
              , 'SERVER_NAME':'foo'
              , 'SERVER_PORT':'80'
              , 'PATH_INFO':'/bar'
               }
    expected = ['Resource moved to: http://foo/bar/']
    actual = Website().get_app(environ, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.sample
    actual = Website().get_app({'PATH_INFO':'/bar/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_no_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_with_slash():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz/'}, start_response)
    assert actual == expected, actual

def test_get_app_doc_example_bar_baz_with_slash_and_more():
    mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE))
    expected = random.shuffle
    actual = Website().get_app({'PATH_INFO':'/bar/baz/buz/biz.html'}, start_response)
    assert actual == expected, actual


# Remove the filesystem fixture after each test.
# ==============================================

attach_rm(globals(), 'test_')
{'fraction_non_alphanumeric': 0.18323987046836743, 'fraction_numerical': 0.00031593081115235765, 'mean_word_length': 3.2150466045272967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6121513', 'n_tokens_mistral': 4259, 'n_tokens_neox': 4134, 'n_words': 826}