def test_parse_network_address_unix_socket_fails_on_windows(): oldval = aspen.WINDOWS try: aspen.WINDOWS = True assert_raises(ValueError, parse.network_address, u"/foo/bar") finally: aspen.WINDOWS = oldval
def test_tornado_base_failure_fails(): mk(("base.html", "{% block foo %}{% end %} Blam.")) make_renderer = tornado_factory_factory() assert_raises( ParseError , make_renderer , "dummy/filepath.txt" , "{% extends base.html %}" "{% block foo %}Some bytes!{% end %}" )
def test_balanced_bank_account(b_b_account, b_account): # b_account = balanced.Account # b_b_account = balanced.BankAccount # b_b_b_account = billing.BalancedBankAccount # got it? b_b_b_account = billing.BalancedBankAccount(balanced_account_uri) assert b_account.find.called_with(balanced_account_uri) assert b_b_account.find.called_with(balanced_bank_account_uri) assert b_b_b_account.is_setup assert_raises(IndexError, b_b_b_account.__getitem__, 'invalid')
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_connecting_unknown_account_fails(): tips = [ ('alice', 'bob', 1) , ('alice', 'carl', 1) ] with tip_graph(*tips) as context: assert_raises( AssertionError , Participant('bob').take_over , StubAccount('github', 'jim') ) actual = context.diff() assert actual == {}, actual
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_balanced_bank_account(b_b_account, b_account): # b_account = balanced.Account # b_b_account = balanced.BankAccount # b_b_b_account = billing.BalancedBankAccount # got it? bank_account = mock.Mock() bank_account.is_valid = True b_account.find.return_value.bank_accounts.all.return_value = [bank_account] b_b_b_account = billing.BalancedBankAccount(balanced_account_uri) assert b_account.find.called_with(balanced_account_uri) assert b_b_account.find.called_with(balanced_bank_account_uri) assert b_b_b_account.is_setup assert_raises(IndexError, b_b_b_account.__getitem__, 'invalid')
def test_raise_response_works(): expected = 404 response = assert_raises( Response , check , "from aspen import Response; " "raise Response(404)" )
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_trailing_slash_redirects_trailing_slash_to_the_right_place(): mk('foo') response = assert_raises(Response, check_trailing_slash, '/foo') expected = '/foo/' actual = response.headers['Location'] 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_packet_with_odd_frames_tells_you_that(): Packet_ = lambda s: list(Packet(s)) # assert_raises chokes on generator packet = FFFD + '4' + FFFD + '0:::' + FFFD exc = assert_raises(SyntaxError, Packet_, packet) expected = "There are an odd number of frames in this packet: %s" % packet actual = exc.args[0] 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 = "/foo/" actual = response.headers["Location"] assert actual == expected, actual
def test_packet_with_odd_frames_tells_you_that(): Packet_ = lambda s: list(Packet(s)) # assert_raises chokes on generator packet = FFFD+'4'+FFFD+'0:::'+FFFD exc = assert_raises(SyntaxError, Packet_, packet) expected = "There are an odd number of frames in this packet: %s" % packet actual = exc.args[0] assert actual == expected, actual
def test_virtual_path_raises_404_on_bad_typecast(): mk(('%year.int/foo.html', "Greetings, program!")) response = assert_raises(Response, check_virtual_paths, '/I am not a year./foo.html') expected = 404 actual = response.code assert actual == expected, actual
def test_parse_specline_obeys_default_by_media_type_default(): resource = get() resource.website.default_renderers_by_media_type.default = 'glubber' err = assert_raises(ValueError, resource._parse_specline, 'media/type') actual = err.args[0] assert actual == ("Unknown renderer for media/type: glubber. " "Possible renderers (starred are missing third-party " "libraries): *pystache, tornado."), actual
def test_event_data_without_args_raises_ValueError(): exc = assert_raises( ValueError , Message.from_bytes , '5:::{"name": "bar", "arrrrgs": []}' ) expected = "An event message must have an 'args' key." actual = exc.args[0] assert actual == expected, actual
def test_event_data_with_reserved_name_raises_ValueError(): exc = assert_raises( ValueError , Message.from_bytes , '5:::{"name": "connect", "args": []}' ) expected = "That event name is reserved: connect." actual = exc.args[0] assert actual == expected, actual
def test_raise_response_works(): expected = 404 response = assert_raises( Response , check , "from aspen import Response; raise Response(404)" ) actual = response.code assert actual == expected, actual
def test_get_renderer_factory_can_raise_syntax_error(): resource = get() resource.website.default_renderers_by_media_type['media/type'] = 'glubber' err = assert_raises( SyntaxError , resource._get_renderer_factory , 'media/type' , 'oo*gle' ) msg = err.args[0] assert msg.startswith("Malformed renderer oo*gle. It must match"), msg
def test_resource_dunder_all_limits_vars(): actual = assert_raises( KeyError , check , "foo = 'bar'\n" "__all__ = []\n" "[---------]\n" "Greetings, %(foo)s!" ) # in production, KeyError is turned into a 500 by an outer wrapper assert type(actual) == KeyError, actual
def test_get_renderer_factory_can_raise_syntax_error(): resource = get() resource.website.default_renderers_by_media_type['media/type'] = 'glubber' err = assert_raises( SyntaxError , resource._get_renderer_factory , 'media/type' , 'oo*gle' ) msg = err.args[0] assert msg.startswith("Malformed renderer oo*gle. It must match" " #![a-z0-9.-]+."), msg
def test_get_renderer_factory_can_raise_syntax_error(): resource = get() resource.website.default_renderers_by_media_type['media/type'] = 'glubber' err = assert_raises( SyntaxError , resource._get_renderer_factory , 'media/type' , 'oo*gle' ) actual = err.args[0] assert actual == ("Malformed renderer oo*gle. It must match #![a-z0-9.-]+." " Possible renderers (might need third-party libs): " "pystache, tornado."), actual
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 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 test_packet_with_odd_frames_raises_SyntaxError(): Packet_ = lambda s: list(Packet(s)) # assert_raises chokes on generator assert_raises(SyntaxError, Packet_, FFFD + '4' + FFFD + '0:::' + FFFD)
def test_accessing_missing_key_raises_Response(): m = Mapping() assert_raises(Response, lambda k: m[k], 'foo')
def test_from_bytes_too_few_colons_raises_SyntaxError(): exc = assert_raises(SyntaxError, Message.from_bytes, '3:') expected = "This message has too few colons: 3:." actual = exc.args[0] assert actual == expected, actual
def test_from_bytes_non_digit_type_raises_ValueError(): exc = assert_raises(ValueError, Message.from_bytes, 'foo:::') expected = "The message type is not in 0..8: foo." actual = exc.args[0] assert actual == expected, actual
def test_version_cant_even_be_lowercase(): assert assert_raises(Response, Version, "http/1.1").code == 400
def test_version_with_garbage_is_safe(): r = assert_raises(Response, Version, "HTTP\xef/1.1") assert r.code == 400, r.code assert r.body == "Bad HTTP version: HTTP\\xef/1.1.", r.body
def test_querystring_chokes_on_bad_unicode(): assert_raises(UnicodeDecodeError, Querystring, "baz=%e2%98")
def test_no_auth(): auth = lambda u, p: u == "username" and p == "password" response = assert_raises(Response, _request_with, auth, None) assert response.code == 401, response
def test_event_data_with_reserved_name_raises_ValueError(): exc = assert_raises(ValueError, Message.from_bytes, '5:::{"name": "connect", "args": []}') expected = "That event name is reserved: connect." actual = exc.args[0] assert actual == expected, actual
def test_version_cant_be_junk(): assert assert_raises(Response, Version, "http flah flah").code == 400
def test_from_bytes_type_too_big_raises_ValueError(): exc = assert_raises(ValueError, Message.from_bytes, '9:::') expected = "The message type is not in 0..8: 9." actual = exc.args[0] assert actual == expected, actual
def test_virtual_path_raises_404_on_direct_access(): mk() response = assert_raises(Response, check_virtual_paths, '/%name/foo.html') expected = 404 actual = response.code assert actual == expected, actual
def test_event_data_without_args_raises_ValueError(): exc = assert_raises(ValueError, Message.from_bytes, '5:::{"name": "bar", "arrrrgs": []}') expected = "An event message must have an 'args' key." actual = exc.args[0] assert actual == expected, actual
def test_mapping_calling_ones_with_missing_key_raises_Response(): m = Mapping() assert_raises(Response, m.ones, 'foo')
def test_wrong_auth(): auth = lambda u, p: u == "username" and p == "password" response = assert_raises(Response, _request_with, auth, "Wacky xxx") assert response.code == 400, response
def test_virtual_path_raises_on_bad_typecast(): mk(('%year.int/foo.html', "Greetings, program!")) assert_raises(Response, check_virtual_paths, '/I am not a year./foo.html')
def test_virtual_path_raises_on_direct_access(): mk() assert_raises(Response, check_virtual_paths, '/%name/foo.html')
def test_method_cant_have_more_attributes(): method = Method("GET") assert_raises(AttributeError, setattr, method, "foo", "bar")
def test_method_cant_be_non_ASCII(): assert assert_raises(Response, Method, "\x80").code == 501
def the501(i): assert assert_raises(Response, Method, chr(i)).code == 501
def test_intercept_socket_protects_direct_access(): request = Request(uri="/foo.sock") assert_raises(Response, gauntlet.intercept_socket, request)
def test_method_we_cap_it_at_64_bytes_just_cause____I_mean___come_on___right(): big = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz--!" assert assert_raises(Response, Method, big).code == 501