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_www_root_defaults_to_cwd(): mk() c = Configurable() c.configure([]) expected = os.path.realpath(os.getcwd()) actual = c.www_root 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_redirect_has_only_location(): mk(('index.html.spt', "from aspen import Response\n[---]\nrequest.redirect('http://elsewhere', code=304)\n[---]\n")) actual = handle() assert actual.code == 304 headers = actual.headers assert len(headers) == 1, headers assert headers.get('Location') is not None, headers
def test_can_put_onto_buffer(): mk(('echo.sock.spt', '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_unavailable_knob_works(): mk( '.aspen' , ('.aspen/foo.py', 'bar = "baz"') , ('index.html', "import fooGreetings, {{ foo.bar }}!") ) actual = handle('/', '--unavailable=1').code assert actual == 503, actual
def test_root_defaults_to_cwd(): mk() c = Configurable() c.configure([]) expected = os.getcwd() actual = c.root 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_configurable_sees_root_option(): mk() c = Configurable() c.configure(['--root', FSFIX]) expected = os.getcwd() actual = c.root assert actual == expected, actual
def test_configurable_sees_root_option(): mk() c = Configurable() c.configure(['--www_root', FSFIX]) expected = os.getcwd() actual = c.www_root 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(uri='/echo.sock')) return transport
def test_resources_can_import_from_dot_aspen(): mk( '.aspen' , ('.aspen/foo.py', 'bar = "baz"') , ('index.html', "import fooGreetings, {{ foo.bar }}!") ) expected = "Greetings, baz!" actual = handle('/', '--project_root=.aspen').body assert actual == expected, actual
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_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_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_path_part_params_are_available(): mk(('/foo/index.html.spt', """ if 'b' in path.parts[0].params: a = path.parts[0].params['a'] [---] %(a)s""")) expected = "3" actual = handle('/foo;a=1;b;a=3/').body assert actual == expected, actual + " isn't " + expected
def test_unavailable_knob_sets_retry_after_on_website(): mk( '.aspen' , ('.aspen/foo.py', 'bar = "baz"') , ('index.html', "import fooGreetings, {{ foo.bar }}!") ) actual = handle('/', '--unavailable=10').headers['Retry-After'] expected = datetime.datetime.utcnow().strftime('%a, %d %b %Y') assert actual.startswith(expected), actual assert actual.endswith(' +0000'), 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_resources_can_import_from_dot_aspen(): mk( '.aspen' , ('.aspen/foo.py', 'bar = "baz"') , ('index.html.spt', "from foo import bar\n[---]\nGreetings, %(bar)s!") ) expected = "Greetings, baz!" project_root = os.path.join(FSFIX, '.aspen') actual = handle('/', '--project_root='+project_root).body assert actual == expected, actual
def test_normal_response_is_returned(): mk(('index.html', "Greetings, program!")) expected = '\r\n'.join("""\ HTTP/1.1 Content-Type: text/html Greetings, program! """.splitlines()) actual = handle()._to_http('1.1') assert actual == expected, actual
def test_channel_passes_send_on_to_four_sockets(): mk(('echo.sock', 'channel.send(channel.recv())')) channel = Channel('foo', ThreadedBuffer) sockets = [make_socket(channel=channel) for i in range(4)] channel.send('foo') for socket in sockets: expected = deque([Message.from_bytes('3::/echo.sock:foo')]) actual = socket.outgoing.queue assert actual == expected, actual
def test_channel_passes_send_on_to_one_socket(): mk(('echo.sock', '')) socket = make_socket() channel = Channel('foo', ThreadedBuffer) channel.add(socket) channel.send('foo') expected = deque([Message.from_bytes('3::/echo.sock:foo')]) actual = socket.outgoing.queue 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_ConfigurationError_NOT_raised_if_no_cwd_but_do_have___root(): mk() foo = os.getcwd() os.chdir(FSFIX) os.rmdir(os.getcwd()) c = Configurable() c.configure(['--root', foo]) expected = foo actual = c.root assert actual == expected, actual
def test_ConfigurationError_NOT_raised_if_no_cwd_but_do_have__www_root(): mk() foo = os.getcwd() os.chdir(FSFIX) os.rmdir(os.getcwd()) c = Configurable() c.configure(['--www_root', foo]) expected = foo actual = c.www_root 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() resource = load(request, 0) response = resource.respond(request, response) if body: return response.body else: return response
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() if actual is not None: actual = actual.next() assert actual == expected, actual
def test_double_failure_still_sets_response_dot_request(): mk( '.aspen' , ('.aspen/foo.py', """ def bar(response): response.request """) , ( '.aspen/configure-aspen.py' , 'import foo\nwebsite.hooks.outbound_late.register(foo.bar)' ) , ('index.html', "raise heck") )
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() if actual is not None: actual = actual.next() assert actual == expected, actual
def check(content, filename="index.html", body=True, configure_aspen_py="", response=None, argv=None): if argv is None: argv = [] mk(('.aspen/configure-aspen.py', configure_aspen_py), (filename, content)) request = StubRequest.from_fs(filename, *argv) resource = load(request, 0) response = resource.respond(request, response) if body: return response.body else: return response
def test_website_doesnt_clobber_outbound(): mk( ( '.aspen/configure-aspen.py' , 'import random\nwebsite.hooks.outbound.append(random.choice)' ) ) project_root = os.path.join(FSFIX, '.aspen') website = Website(['--www_root='+FSFIX, '--project_root='+project_root]) expected = 2 actual = len(website.hooks.outbound) assert actual == expected, actual
def test_callback_root_finds_root(): mk() expected = fix() class Values(): pass class Parser: values = Values() parser = Parser() callback_root(None, None, FSFIX, parser) expected = os.path.realpath(FSFIX) actual = parser.values.root assert actual == expected, actual
def test_configuration_script_can_set_renderer_default(): CONFIG = """ website.renderer_default="stdlib_format" """ SIMPLATE = """ name="program" [----] Greetings, {name}! """ mk( ('.aspen/configure-aspen.py', CONFIG), ('index.html.spt', SIMPLATE) ) w = Website(['--www_root', FSFIX, '-p', fix('.aspen'), '--show_tracebacks=yes']) request = StubRequest(b'/') request.website = w response = w.handle_safely(request) actual = response.body.strip() expected = 'Greetings, program!' assert actual == expected, actual
def test_double_failure_still_sets_response_dot_request(): mk( '.aspen' , ('.aspen/foo.py', """ def bar(response): response.request """) , ( '.aspen/configure-aspen.py' , 'import foo\nwebsite.hooks.outbound.append(foo.bar)' ) , ('index.html.spt', "raise heck\n[---]\n") ) # Intentionally break the website object so as to trigger a double failure. project_root = os.path.join(FSFIX, '.aspen') website = Website(['--www_root='+FSFIX, '--project_root='+project_root]) del website.renderer_factories response = website.handle_safely(StubRequest()) expected = 500 actual = response.code assert actual == expected, actual
def test_autoindex_response_is_404_by_default(): mk(('README', "Greetings, program!")) expected = 404 actual = handle().code assert actual == expected, actual
def test_buffer_is_instantiable(): mk(('echo.sock', 'socket.send(socket.recv())')) expected = Buffer actual = Buffer(make_socket(), 'foo').__class__ assert actual is expected, actual
def test_channel_raises_AssertionError_on_double_add(): mk(('echo.sock', '')) socket = make_socket() channel = Channel('foo', ThreadedBuffer) channel.add(socket) assert_raises(AssertionError, channel.add, socket)
def test_ConfigurationError_raised_if_no_cwd(): mk() os.chdir(FSFIX) os.rmdir(FSFIX) c = Configurable() assert_raises(ConfigurationError, c.configure, [])
def test_ConfigurationError_raised_if_no_cwd(): mk() os.chdir(FSFIX) os.rmdir(os.getcwd()) c = Configurable() assert_raises(ConfigurationError, c.configure, [])
def test_socket_is_instantiable(): mk(('echo.sock', '')) expected = Socket actual = make_socket().__class__ assert actual is expected, actual
def test_autoindex_response_is_returned(): mk(('README', "Greetings, program!")) expected = True actual = 'README' in handle('/', '--list_directories=TrUe').body assert actual == expected, actual