def serve_request(path): """Given an URL path, return response. """ request = StubRequest(path) request.website = test_website response = test_website.handle_safely(request) return response
def serve_request(path): """Given an URL path, return response. """ request = StubRequest(path) request.website = test_website response = test_website.handle_safely(request) return response
def test_bad_fails(): request = StubRequest() # once to get a WWW-Authenticate header hook = inbound_responder(_auth_func("username", "password"), realm="*****@*****.**") response = raises(Response, hook, request).value # do something with the header auth_headers = _auth_headers(response) request.headers['Authorization'] = _digest_auth_for(auth_headers, "username", "badpassword") response = raises(Response, hook, request).value assert response.code == 401, response assert not request.auth.authorized()
def serve_request(path, user=None): """Given an URL path, return response. """ request = StubRequest(path) request.website = test_website if user is not None: user = User.from_id(user) # Note that Cookie needs a bytestring. request.headers.cookie[str('session')] = user.session_token response = test_website.handle_safely(request) return response
def serve_request(path, user=None): """Given an URL path, return response. """ request = StubRequest(path) request.website = test_website if user is not None: user = User.from_id(user) # Note that Cookie needs a bytestring. request.headers.cookie[str('session')] = user.session_token response = test_website.handle_safely(request) return response
def test_methods_changing_changes(): request = StubRequest() request.method = 'POST' assert not request.OPTIONS assert not request.GET assert not request.HEAD assert request.POST assert not request.PUT assert not request.DELETE assert not request.TRACE assert not request.CONNECT
def test_bad_fails(): request = StubRequest() # once to get a WWW-Authenticate header hook = inbound_responder(_auth_func("username", "password"), realm="*****@*****.**") response = assert_raises(Response, hook, request) # do something with the header auth_headers = _auth_headers(response) request.headers['Authorization'] = _digest_auth_for(auth_headers, "username", "badpassword") response = assert_raises(Response, hook, request) assert response.code == 401, response assert not request.auth.authorized()
def test_good_works(): request = StubRequest() # once to get a WWW-Authenticate header hook = inbound_responder(_auth_func("username", "password"), realm="*****@*****.**") response = raises(Response, hook, request).value # do something with the header auth_headers = _auth_headers(response) request.headers['Authorization'] = _digest_auth_for(auth_headers, "username", "password") #print repr(request.headers['Authorization']) response = hook(request) success = request.auth.authorized() assert success assert request.auth.username() == "username", request.auth.username()
def test_good_works(): request = StubRequest() # once to get a WWW-Authenticate header hook = inbound_responder(_auth_func("username", "password"), realm="*****@*****.**") response = assert_raises(Response, hook, request) # do something with the header auth_headers = _auth_headers(response) request.headers['Authorization'] = _digest_auth_for(auth_headers, "username", "password") #print repr(request.headers['Authorization']) response = hook(request) success = request.auth.authorized() assert success assert request.auth.username() == "username", request.auth.username()
def load_simplate(path): """Given an URL path, return resource. """ request = StubRequest(path) request.website = test_website # XXX HACK - aspen.website should be refactored from aspen import gauntlet, sockets test_website.hooks.inbound_early.run(request) gauntlet.run(request) # sets request.fs request.socket = sockets.get(request) test_website.hooks.inbound_late.run(request) return resources.get(request)
def load_simplate(path): """Given an URL path, return resource. """ request = StubRequest(path) request.website = test_website # XXX HACK - aspen.website should be refactored from aspen import dispatcher, sockets test_website.hooks.run('inbound_early', request) dispatcher.dispatch(request) # sets request.fs request.socket = sockets.get(request) test_website.hooks.run('inbound_late', request) return resources.get(request)
def load_request(path): """Given an URL path, return request. """ request = StubRequest(path) request.website = test_website # XXX HACK - aspen.website should be refactored from aspen import dispatcher, sockets test_website.hooks.run('inbound_early', request) dispatcher.dispatch(request) # sets request.fs request.socket = sockets.get(request) test_website.hooks.run('inbound_late', request) return request
def test_get_response_doesnt_reset_content_type_when_not_negotiating(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') response = Response() response.headers['Content-Type'] = 'never/mind' actual = get_response(request, response).headers['Content-Type'] assert actual == "never/mind"
def test_aspen_favicon_doesnt_get_clobbered_by_virtual_path(): mk('%value') request = StubRequest.from_fs('/favicon.ico') gauntlet.run_through(request, gauntlet.not_found) expected = {} actual = request.line.uri.path assert actual == expected, actual
def test_get_response_doesnt_reset_content_type_when_not_negotiating(mk): mk(("index.spt", NEGOTIATED_RESOURCE)) request = StubRequest.from_fs("index.spt") response = Response() response.headers["Content-Type"] = "never/mind" actual = get_response(request, response).headers["Content-Type"] assert actual == "never/mind"
def test_handles_busted_accept(mk): mk(("index.spt", NEGOTIATED_RESOURCE)) request = StubRequest.from_fs("index.spt") # Set an invalid Accept header so it will return default (text/plain) request.headers["Accept"] = "text/html;" actual = get_response(request, Response()).body assert actual == "Greetings, program!\n"
def test_get_response_406_gives_list_of_acceptable_types(mk): mk(("index.spt", NEGOTIATED_RESOURCE)) request = StubRequest.from_fs("index.spt") request.headers["Accept"] = "cheese/head" actual = raises(Response, get_response, request, Response()).value.body expected = "The following media types are available: text/plain, text/html." assert actual == expected
def test_aspen_favicon_doesnt_get_clobbered_by_virtual_path(): mk('%value') request = StubRequest.from_fs('/favicon.ico') gauntlet.run_through(request, gauntlet.not_found) expected = {} actual = request.line.uri.path assert actual == expected, actual
def test_get_response_406_gives_list_of_acceptable_types(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'cheese/head' actual = raises(Response, get_response, request, Response()).value.body expected = "The following media types are available: text/plain, text/html." assert actual == expected
def test_handles_busted_accept(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') # Set an invalid Accept header so it will return default (text/plain) request.headers['Accept'] = 'text/html;' actual = get_response(request, Response()).body assert actual == "Greetings, program!\n"
def test_aspen_favicon_doesnt_get_clobbered_by_virtual_path(mk): mk('%value.spt') request = StubRequest.from_fs('/favicon.ico') dispatcher.dispatch(request) expected = {} actual = request.line.uri.path assert actual == expected
def test_can_override_default_renderer_entirely(mk): mk(('.aspen/configure-aspen.py', OVERRIDE_SIMPLATE), ('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'text/plain' actual = get_response(request, Response()).body assert actual == "glubber"
def test_can_override_default_renderer_entirely(): mk(('.aspen/configure-aspen.py', OVERRIDE_SIMPLATE), ('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'text/plain' actual = get_response(request, Response()).body assert actual == "glubber", actual
def test_get_response_doesnt_reset_content_type_when_not_negotiating(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') response = Response() response.headers['Content-Type'] = 'never/mind' actual = get_response(request, response).headers['Content-Type'] assert actual == "never/mind", actual
def test_get_response_406_gives_list_of_acceptable_types(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'cheese/head' actual = assert_raises(Response, get_response, request, Response()).body expected ="The following media types are available: text/plain, text/html." assert actual == expected, actual
def test_robots_txt_also_shouldnt_be_redirected(): mk('%value') request = StubRequest.from_fs('/robots.txt') err = assert_raises(Response, gauntlet.run_through, request, gauntlet.not_found) actual = err.code assert actual == 404, actual
def test_aspen_favicon_doesnt_get_clobbered_by_virtual_path(): mk('%value.spt') request = StubRequest.from_fs('/favicon.ico') dispatcher.dispatch(request) expected = {} actual = request.line.uri.path 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_configuration_script_can_set_renderer_default(mk): 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
def test_can_override_default_renderer_entirely(): mk(('.aspen/configure-aspen.py', """\ from aspen.renderers import Renderer, Factory class Glubber(Renderer): def render_content(self, context): return "glubber" class GlubberFactory(Factory): Renderer = Glubber website.renderer_factories['glubber'] = GlubberFactory(website) website.default_renderers_by_media_type.default = 'glubber' """), ('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'text/plain' actual = get_response(request, Response()).body assert actual == "glubber", actual
def test_can_override_default_renderer_entirely(): mk(('.aspen/configure-aspen.py', """\ from aspen.renderers import Renderer, Factory class Glubber(Renderer): def render_content(self, context): return "glubber" class GlubberFactory(Factory): Renderer = Glubber website.renderer_factories['glubber'] = GlubberFactory(website) website.default_renderers_by_media_type.default = 'glubber' """), ('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'text/plain' actual = get_response(request, Response()).body assert actual == "glubber", actual
def test_double_failure_still_sets_response_dot_request(mk): 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
def test_get_response_negotiates(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'text/html' actual = get_response(request, Response()).body assert actual == "<h1>Greetings, program!</h1>\n", actual
def check(path, *a): """Given a URI path, return a dispatched request object. """ request = StubRequest.from_fs(path.encode('ascii'), *a) dispatcher.dispatch(request) return request
def test_get_response_is_happy_not_to_negotiate(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') actual = get_response(request, Response()).body assert actual == "Greetings, program!\n"
def test_is_xhr_true(): request = StubRequest() request.headers['X-Requested-With'] = 'XmlHttpRequest' assert request.is_xhr()
def test_is_xhr_true(): request = StubRequest() request.headers['X-Requested-With'] = 'XmlHttpRequest' assert request.is_xhr()
def test_allow_allows_allowed(): request = StubRequest() expected = None actual = request.allow('GET') assert actual is expected, actual
def test_is_xhr_is_case_insensitive(): request = StubRequest() request.headers['X-Requested-With'] = 'xMLhTTPrEQUEST' assert request.is_xhr()
def test_is_xhr_false(): request = StubRequest() assert not request.is_xhr()
def check_indirect_negotiation(path): """Given an urlpath, return a filesystem path per gauntlet.virtual_paths. """ request = StubRequest.from_fs(path) gauntlet.run_through(request, gauntlet.indirect_negotiation) return request
def test_methods_start_with_GET(): request = StubRequest() expected = "GET" actual = request.line.method assert actual == expected
def test_get_response_gets_response(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) response = Response() request = StubRequest.from_fs('index.spt') actual = get_response(request, response) assert actual is response
def test_get_response_raises_406_if_need_be(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'cheese/head' actual = raises(Response, get_response, request, Response()).value.code assert actual == 406
def test_get_response_sets_content_type_when_it_negotiates(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'text/html' actual = get_response(request, Response()).headers['Content-Type'] assert actual == "text/html; charset=UTF-8"
def test_get_response_sets_content_type_when_it_negotiates(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'text/html' actual = get_response(request, Response()).headers['Content-Type'] assert actual == "text/html; charset=UTF-8", actual
def test_get_response_raises_406_if_need_be(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') request.headers['Accept'] = 'cheese/head' actual = assert_raises(Response, get_response, request, Response()).code assert actual == 406, actual
def test_allow_disallows_disallowed(): request = StubRequest() expected = 405 actual = raises(Response, request.allow, 'POST').value.code assert actual == expected
def test_get_response_sets_content_type_when_it_doesnt_negotiate(): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') actual = get_response(request, Response()).headers['Content-Type'] assert actual == "text/plain; charset=UTF-8", actual
def test_robots_txt_also_shouldnt_be_redirected(mk): mk('%value.spt') request = StubRequest.from_fs('/robots.txt') err = raises(Response, dispatcher.dispatch, request).value actual = err.code assert actual == 404
def test_is_xhr_false(): request = StubRequest() assert not request.is_xhr()
def test_get_response_gets_response(): mk(('index', NEGOTIATED_RESOURCE)) response = Response() request = StubRequest.from_fs('index') actual = get_response(request, response) assert actual is response, actual
def test_is_xhr_is_case_insensitive(): request = StubRequest() request.headers['X-Requested-With'] = 'xMLhTTPrEQUEST' assert request.is_xhr()
def test_get_response_is_happy_not_to_negotiate(): mk(('index', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index') actual = get_response(request, Response()).body assert actual == "Greetings, program!\n", actual
def test_allow_can_handle_lowercase(): request = StubRequest() expected = 405 actual = raises(Response, request.allow, 'post').value.code assert actual == expected
def check_virtual_paths(path): """Given an urlpath, return a filesystem path per gauntlet.virtual_paths. """ request = StubRequest.from_fs(path) gauntlet.run_through(request, gauntlet.virtual_paths) return request
def check_index(path, *a): """Given a uripath, return a filesystem path per gauntlet.index. """ request = StubRequest.from_fs(path, *a) gauntlet.run_through(request, gauntlet.index) return request
def test_methods_changing_changes(): request = StubRequest() request.line.method = 'POST' expected = "POST" actual = request.line.method assert actual == expected
def check_trailing_slash(path): """Given an urlpath, return a filesystem path per gauntlet.trailing_slash. """ request = StubRequest.from_fs(path) gauntlet.run_through(request, gauntlet.trailing_slash) return request
def test_get_response_negotiates(mk): mk(('index.spt', NEGOTIATED_RESOURCE)) request = StubRequest.from_fs('index.spt') request.headers['Accept'] = 'text/html' actual = get_response(request, Response()).body assert actual == "<h1>Greetings, program!</h1>\n"