def test_configurable_sees_root_option(): mk() c = Configurable() c.configure(['--root', 'fsfix']) expected = os.getcwd() actual = c.root assert actual == expected, actual
def test_root_defaults_to_cwd(): mk() c = Configurable() c.configure([]) expected = os.getcwd() actual = c.root assert actual == expected, actual
def test_trailing_slash_redirects_trailing_slash_to_the_right_place(): mk('foo') response = assert_raises(Response, check_trailing_slash, '/foo') expected = 'http://localhost/foo/' actual = response.headers.one('Location') assert actual == expected, actual
def test_can_put_onto_buffer(): mk(('echo.sock', 'socket.send(socket.recv())')) buffer = Buffer(make_socket(), 'foo') expected = [FFFD+'4'+FFFD+'1:::'] buffer.put(Message.from_bytes('1:::')) actual = list(buffer.flush()) assert actual == expected, actual
def test_all_six(): mk(('foo.conf', """ random:choice random:sample random:randint random:random random:gauss random:choice random:shuffle Ignored! """) ) expected = [ [random.choice, random.sample] , [random.randint] , [random.random, random.gauss] , [random.choice, random.shuffle] , [] , [] ] actual = load('fsfix/foo.conf') assert actual == expected, actual
def test_layering(): mk(('first.conf', """ random:choice random:sample random:randint """), ('second.conf', """ random:random random:gauss random:choice random:shuffle """)) expected = [[], [random.choice, random.sample, random.random, random.gauss], [random.randint, random.choice, random.shuffle], [], [], []] actual = load('fsfix/first.conf', 'fsfix/second.conf') assert actual == expected, actual
def test_file_with_no_extension_matches(): mk( ('%value', '{"Greetings,": "program!"}') , ('value', '{"Greetings,": "program!"}') ) expected = {'value': 'baz'} actual = check_virtual_paths('/baz').path assert actual == expected, actual
def test_virtual_path_docs_6(): mk(('%year.int/index.html', "Tonight we're going to party like it's {{ request.path['year'] }}!")) response = handle('/1999/') expected = "Tonight we're going to party like it's 1999!" actual = response.body assert actual == expected, actual
def test_all_six(): mk(('foo.conf', """ random:choice random:sample random:randint random:random random:gauss random:choice random:shuffle Ignored! """)) expected = [[random.choice, random.sample], [random.randint], [random.random, random.gauss], [random.choice, random.shuffle], [], []] actual = load('fsfix/foo.conf') assert actual == expected, actual
def test_virtual_path_matches_the_first(): mk( ('%first/foo.html', "Greetings, program!") , ('%second/foo.html', "WWAAAAAAAAAAAA!!!!!!!!") ) expected = expect('%first/foo.html') actual = check_virtual_paths('/1999/foo.html').fs assert actual == expected, actual
def test_barely_working(): mk(('index.html', "Greetings, program!")) simplate = Simplate('index.html') expected = 'text/html' actual = simplate[0] assert actual == expected, actual
def test_index_conf_setting_overrides_and_doesnt_extend(): mk( ('.aspen/aspen.conf', '[aspen]\ndefault_filenames = default.html') , ('index.html', "Greetings, program!") ) expected = expect('') actual = check_index('/').fs assert actual == expected, actual
def test_alternate_index_is_found(): mk( ('.aspen/aspen.conf', '[aspen]\ndefault_filenames = default.html') , ('default.html', "Greetings, program!") ) expected = expect('default.html') actual = check_index('/').fs assert actual == expected, actual
def test_socket_can_shake_hands(): mk(('echo.sock', '')) socket = make_socket() response = socket.shake_hands() expected = '15:10:xhr-polling' actual = response.body.split(':', 1)[1] assert actual == expected, actual
def test_layering(): mk(('first.conf', """ random:choice random:sample random:randint """), ('second.conf', """ random:random random:gauss random:choice random:shuffle """) ) expected = [ [] , [random.choice, random.sample, random.random, random.gauss] , [random.randint, random.choice, random.shuffle] , [] , [] , [] ] actual = load('fsfix/first.conf', 'fsfix/second.conf') assert actual == expected, actual
def test_file_matches_other_extension(): mk( ('%value.json', '{"Greetings,": "program!"}') , ('%value.txt', "Greetings, program!") ) expected = "%value.txt" actual = os.path.basename(check_virtual_paths('/baz.txt').fs) assert actual == expected, actual
def test_file_matches_in_face_of_dir(): mk( ('%page/index.html', 'Nothing to see here.') , ('%value.txt', "Greetings, program!") ) expected = {'value': 'baz'} actual = check_virtual_paths('/baz.html').path assert actual == expected, actual
def test_trailing_slash_redirects_trailing_slash(): mk('foo') response = assert_raises(Response, check_trailing_slash, '/foo') expected = 301 actual = response.code assert actual == expected, actual
def test_classes_instantiated(): mk('__/etc', lib_python, ('__/etc/middleware.conf', 'TESTING_middleware:App'), (lib_python + '/TESTING_middleware.py', MODULE)) from TESTING_middleware import App as expected actual = Loader().load_middleware()[1].__class__ assert actual == expected, actual
def test_form_feeds_on_same_line(): mk(('foo.conf', """ random:choice random:sample random:randint random:random random:choice random:gauss random:shuffle Ignored! """)) expected = [[random.choice, random.sample], [random.randint], [random.random], [random.choice], [random.gauss], [random.shuffle]] actual = load('fsfix/foo.conf') assert actual == expected, actual
def test_simplates_can_import_from_dot_aspen(): mk( '.aspen' , ('.aspen/foo.py', 'bar = "baz"') , ('index.html', "import fooGreetings, {{ foo.bar }}!") ) expected = "Greetings, baz!" actual = handle().body assert actual == expected, actual
def make_transport(content='', state=0): mk(('echo.sock', content)) socket = make_socket() transport = XHRPollingTransport(socket) transport.timeout = 0.05 # for testing, could screw up the test if state == 1: transport.respond(Request(url='/echo.sock')) return transport
def test_index_conf_setting_takes_second_if_first_is_missing(): mk( ( '.aspen/aspen.conf' , '[aspen]\ndefault_filenames = index.html default.html') , ('default.html', "Greetings, program!") ) expected = expect('default.html') actual = check_index('/').fs assert actual == expected, actual
def test_handle_barely_working(): mk(('index.html', "Greetings, program!")) request = StubRequest('index.html') response = Response() handle(request, response) actual = response.body expected = "Greetings, program!" assert actual == expected, actual
def test_comments_ignored(): mk('__/etc', ('__/etc/apps.conf', """\ #comment /foo random:choice#comment /bar random:sample # comments""")) expected = [('/foo', random.choice), ('/bar', random.sample)] actual = Loader().load_apps() assert actual == expected, actual
def test_index_conf_setting_ignores_blanks(): mk( ( '.aspen/aspen.conf' , '[aspen]\ndefault_filenames: index.html,, ,, ,,, default.html') , ('default.html', "Greetings, program!") ) expected = expect('default.html') actual = check_index('/').fs assert actual == expected, actual
def test_get_app_doc_example_bar_no_slash(): mk('__', '__/etc', ('__/etc/apps.conf', EXAMPLE)) environ = dict() environ['wsgi.url_scheme'] = 'http' environ['HTTP_HOST'] = 'foo' environ['PATH_INFO'] = '/bar' err = assert_raises(Response, Website().get_app, environ) assert err.code == 301, err.code
def test_virtual_path_docs_3(): mk( ('%name/index.html', "^L\nGreetings, {{ request.path['name'] }}!") , ('%name/%cheese.txt', "^L\r\n{{ request.path['name'].title() }} likes {{ request.path['cheese'] }} cheese.") ) response = handle('/chad/cheddar.txt') expected = "Chad likes cheddar cheese." actual = response.body assert actual == expected, actual
def test_index_conf_setting_works_with_only_comma(): mk( ( '.aspen/aspen.conf' , '[aspen]\ndefault_filenames: index.html,default.html') , ('default.html', "Greetings, program!") ) expected = expect('default.html') actual = check_index('/').fs assert actual == expected, actual
def test_virtual_path_docs_4(): mk( ('%name/index.html', "Greetings, {{ request.path['name'] }}!") , ('%name/%cheese.txt', "{{ request.path['name'].title() }} likes {{ request.path['cheese'] }} cheese.") ) response = handle('/chad/cheddar.txt/') expected = 404 actual = response.code assert actual == expected, actual
def check(content, filename="index.html", body=True, aspenconf="", response=None): mk(('.aspen/aspen.conf', aspenconf), (filename, content)) request = StubRequest.from_fs(filename) response = response or Response() handle(request, response) if body: return response.body else: return response
def test_virtual_path_docs_4(): mk(('%name/index.html', "Greetings, {{ request.path['name'] }}!"), ('%name/%cheese.txt', "{{ request.path['name'].title() }} likes {{ request.path['cheese'] }} cheese." )) response = handle('/chad/cheddar.txt/') expected = 404 actual = response.code assert actual == expected, actual
def test_double_failure_still_sets_response_dot_request(): mk( '.aspen' , ('.aspen/foo.py', """ def bar(response): response.request """) , ('.aspen/hooks.conf', 'foo:bar') , ('index.html', "raise heck") )
def test_contested_url_path_trailing_second(): mk('__/etc', ('__/etc/apps.conf', """ /foo random:choice /foo/ random:sample """)) err = assert_raises(AppsConfError, Loader().load_apps) assert err.msg == "URL path is contested: '/foo/'", err.msg
def test_two_sockets_are_instantiable(): mk(('echo.sock', '')) socket1 = make_socket() socket2 = make_socket() expected = (Socket, Socket) actual = (socket1.__class__, socket2.__class__) assert actual == expected, actual
def test_virtual_path_docs_6(): mk( ( '%year.int/index.html' , "Tonight we're going to party like it's {{ request.path['year'] }}!" ) ) response = handle('/1999/') expected = "Tonight we're going to party like it's 1999!" actual = response.body assert actual == expected, actual
def test_channel_can_have_sockets_added_to_it(): mk(('echo.sock', 'channel.send(channel.recv())')) socket = make_socket() channel = Channel('foo', ThreadedBuffer) channel.add(socket) expected = [socket] actual = list(channel) assert actual == expected, actual
def test_socket_can_barely_function(): mk(('echo.sock', 'socket.send("Greetings, program!")')) socket = make_socket() socket.tick() expected = FFFD+'33'+FFFD+'3::/echo.sock:Greetings, program!' actual = socket._recv().next() assert actual == expected, actual
def test_socket_can_echo(): mk(('echo.sock', 'socket.send(socket.recv())')) with SocketInThread() as socket: socket._send('3::/echo.sock:Greetings, program!') time.sleep(0.05) # give the resource time to tick expected = FFFD+'33'+FFFD+'3::/echo.sock:Greetings, program!' actual = socket._recv().next() assert actual == expected, actual
def test_virtual_path_docs_5(): mk(('%name/index.html', "Greetings, {{ request.path['name'] }}!"), ('%name/%cheese.txt', "{{ request.path['name'].title() }} likes {{ request.path['cheese'] }} cheese." ), ('%year.int/index.html', "Tonight we're going to party like it's {{ request.path['year'] }}!")) response = handle('/1999/') expected = "Greetings, 1999!" actual = response.body assert actual == expected, actual
def test_comments_ignored(): mk('__/etc', ('__/etc/middleware.conf', """ #comment random:choice#comment random:sample # comments """)) expected = [Responder, random.sample, random.choice] actual = Loader().load_middleware() assert actual == expected, actual
def test_comments_ignored(): mk('.aspen', ('.aspen/hooks.conf', """ #comment random:choice#comment random:sample # comments """)) expected = [[random.choice, random.sample], [], [], [], [], []] actual = load() assert actual == expected, actual
def test_normal_response_is_returned(): mk(('index.html', "Greetings, program!")) expected = '\r\n'.join("""\ HTTP/1.1 Content-Length: 19 Content-Type: text/html; charset=UTF-8 Greetings, program! """.splitlines()) actual = check()._to_http('1.1') assert actual == expected, actual
def test_basic(): mk('__/etc', ('__/etc/handlers.conf', """ fnmatch aspen.rules:fnmatch [random:choice] fnmatch * """)) expected = [handler] actual = Loader().load_handlers() assert actual == expected, actual
def test_section_not_callable(): mk('__/etc', ('__/etc/handlers.conf', """ foo random:choice [string:digits] foo """)) err = assert_raises(HandlersConfError, Loader().load_handlers) assert err.msg == "'string:digits' is not callable", err.msg
def test_readme_contents(): mk('__/etc', ('__/etc/apps.conf', '/foo random:choice')) Loader().load_apps() expected = """\ This directory is served by the application configured on line 1 of __/etc/apps.conf. To wit: /foo random:choice """ actual = open('fsfix/foo/README.aspen').read() assert actual == expected, actual
def test_apps_layer(): mk('__/etc', ('__/etc/apps.conf', """ / random:choice /foo random:sample /foo/bar random:shuffle /baz/ random:seed """)) expected = [('/foo/bar', random.shuffle), ('/foo', random.sample), ('/baz/', random.seed), ('/', random.choice)] actual = Loader().load_apps() assert actual == expected, actual
def test_classes_instantiated(): mk('__/etc', lib_python, ('__/etc/handlers.conf', """ foo TESTING_handlers:Rule [TESTING_handlers:App] foo * """), (lib_python + '/TESTING_handlers.py', MODULE)) from TESTING_handlers import App, Rule handlers = Loader().load_handlers() expected = App actual = handlers[0].handle.__class__ assert actual == expected, actual expected = Rule actual = handlers[0]._funcs['foo'].__class__ assert actual == expected, actual
def test_caret_L_converted_to_page_break(): mk('.aspen', ('.aspen/hooks.conf', """ ^L random:choice random:sample ^L random:randint """)) expected = [[], [random.choice, random.sample], [random.randint], [], [], []] actual = load() assert actual == expected, actual
def test_equal_sections_dont_screw_up_parsing(): # https://github.com/whit537/aspen/issues/9 mk(('hooks.conf', """ ^L # inbound_early ^L # inbound_late ^L # outbound_early ^L # outbound_late random:shuffle """)) expected = [[], [], [], [], [random.shuffle], []] actual = load('fsfix/hooks.conf') assert actual == expected, actual
def test_outbound_section(): mk('.aspen', ('.aspen/hooks.conf', """ random:choice random:sample random:randint """)) expected = [[], [random.choice, random.sample], [random.randint], [], [], []] actual = load() assert actual == expected, actual
def test_no_file(): mk('__/etc') expected = [Responder] actual = Loader().load_middleware() assert actual == expected, actual
def test_find_default_dir_no_default(): mk('fsfix') err = assert_raises(Response, u.find_default, ['index.htm'], 'fsfix') assert err.code == 403, err.code
def test_empty_file(): mk('__/etc', ('__/etc/middleware.conf', '')) expected = [] actual = Loader().load_apps() assert actual == expected, actual
def test_find_default_non_dir(): mk(('foo', '')) expected = 'fsfix/foo' actual = u.find_default(['index.html'], 'fsfix/foo') assert actual == expected, actual
def test_find_default(): mk(('index.html', '')) expected = 'fsfix/index.html' actual = u.find_default(['index.html'], 'fsfix') assert actual == expected, actual
def test_something(): mk('__/etc', ('__/etc/middleware.conf', 'random:choice')) loader = Loader() expected = [Responder, random.choice] actual = Loader().load_middleware() assert actual == expected, actual
def test_must_be_callable(): mk('__/etc', ('__/etc/middleware.conf', 'string:digits')) err = assert_raises(MiddlewareConfError, Loader().load_middleware) assert err.msg == "'string:digits' is not callable"
def test_order(): mk('__/etc', ('__/etc/middleware.conf', 'random:choice\nrandom:seed')) expected = [Responder, random.seed, random.choice] actual = Loader().load_middleware() assert actual == expected, actual