def test_simple_match(self): """Check simple case of match""" app = web.match("/first", "first") | (lambda e, d: Response()) self.assertEqual(web.ask(app, "/first").status_int, 200) self.assertEqual(web.ask(app, "/second"), None)
def test_prefix_with_zeros_in_int(self): '''Simple prefix''' from iktomi.web.url_converters import Converter, ConvertError def handler(env, data): return Response() class ZeroInt(Converter): def to_python(self, value, env=None): try: value = int(value) except ValueError: raise ConvertError(self.name, value) else: return value def to_url(self, value): return str(value) app = web.cases( web.prefix('/section/<int:section_id>', convs={'int': ZeroInt}) | web.match('/item', 'doc') | handler) #self.assertEqual(web.ask(app, '/section/1').status_int, 200) self.assertEqual(web.ask(app, '/section/1/item').status_int, 200) self.assertEqual(web.ask(app, '/section/001/item').status_int, 200)
def test_simple_match(self): '''Check simple case of match''' app = web.match('/first', 'first') | (lambda e,d: Response()) self.assertEqual(web.ask(app, '/first').status_int, 200) self.assertEqual(web.ask(app, '/second'), None)
def test_aliases(self): def handler(env, data): return Response(env.current_location + ' ' + env.root.domain1.as_url + ' ' + env.root.domain1.as_url.with_host() + ' ' + env.root.domain2.as_url + ' ' + env.root.domain2.as_url.with_host()) www = web.subdomain('', 'www') app = web.subdomain('example.com', 'example.ru', 'example.com.ua') | web.cases( web.subdomain('ru', None, primary=None) | web.cases( web.subdomain('moscow') | www | web.match('/', 'domain3'), www | web.match('/', 'domain1') ), web.subdomain('en', 'eng') | www | web.match('/', 'domain2'), ) app = app | handler # XXX As for now .with_host() return primary domain, not the current one # It is easy to change the behaviour, but which behaviour is # correct? self.assertEqual(web.ask(app, 'http://ru.example.com/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://www.ru.example.ru/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://www.example.ru/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://moscow.example.ru/').body, 'domain3 http://example.com/ http://example.com/ ' 'http://en.example.com/ http://en.example.com/')
def test_external_urls(self): 'External URL reverse' def host1(env, data): self.assertEqual(env.root.host1.as_url.with_host(), 'http://host1.example.com/url') self.assertEqual(env.root.host2.as_url, 'http://host2.example.com/url') self.assertEqual(env.root.host1.as_url, '/url') self.host1_called = True return Response() def host2(env, data): self.assertEqual(env.root.host2.as_url.with_host(), 'https://host2.example.com/url') self.assertEqual(env.root.host1.as_url, 'https://host1.example.com/url') self.assertEqual(env.root.host2.as_url, '/url') self.host2_called = True return Response() app = web.subdomain('example.com') | web.cases ( web.subdomain('host1') | web.match('/url', 'host1') | host1, web.subdomain('host2') | web.match('/url', 'host2') | host2, ) assert web.ask(app, 'http://host1.example.com/url') assert web.ask(app, 'https://host2.example.com/url') assert self.host1_called and self.host2_called
def test_external_urls(self): "External URL reverse" def host1(env, data): self.assertEqual(env.root.host1.as_url.with_host(), "http://host1.example.com/url") self.assertEqual(env.root.host2.as_url, "http://host2.example.com/url") self.assertEqual(env.root.host1.as_url, "/url") self.host1_called = True return Response() def host2(env, data): self.assertEqual(env.root.host2.as_url.with_host(), "https://host2.example.com/url") self.assertEqual(env.root.host1.as_url, "https://host1.example.com/url") self.assertEqual(env.root.host2.as_url, "/url") self.host2_called = True return Response() app = web.subdomain("example.com") | web.cases( web.subdomain("host1") | web.match("/url", "host1") | host1, web.subdomain("host2") | web.match("/url", "host2") | host2, ) assert web.ask(app, "http://host1.example.com/url") assert web.ask(app, "https://host2.example.com/url") assert self.host1_called and self.host2_called
def test_simple_match(self): '''Check simple case of match''' app = web.match('/first', 'first') | (lambda e, d: Response()) self.assertEqual(web.ask(app, '/first').status_int, 200) self.assertEqual(web.ask(app, '/second'), None)
def test_expiring_auth(self): set_call_args = [] def set_mock(key, value, time): set_call_args.append((key, value, time)) return True with mock.patch('iktomi.storage.LocalMemStorage.set', side_effect=set_mock): response = self.login('user name', '123') self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assertTrue('Set-Cookie' in response.headers) # checking set was called with right arguments self.assertEqual(len(set_call_args), 1, msg="set was not called") auth_cookie = response.headers['Set-Cookie'].split(";")[0] key, value, time = set_call_args[0] self.assertEqual(key, auth_cookie.replace("=", ":")) self.assertEqual(time, 7 * 24 * 3600) # calling protected resource to ensure that expire was also called # on get request with mock.patch('iktomi.storage.LocalMemStorage.get', return_value=value): web.ask(self.app, '/b', headers={'Cookie': response.headers['Set-Cookie']}) # checking set was expire time was renewed with same arguments self.assertEqual(len(set_call_args), 2, msg="set was not called") key2, value2, time2 = set_call_args[1] self.assertEqual(key2, key) self.assertEqual(value2, value) self.assertEqual(time2, time)
def test_external_urls_no_subdomain(self): 'External URL reverse with no subdomains provided in location' def config(env, data, nxt): env.root = root.bind_to_env(env) return nxt(env, data) called_urls = [] def get_handler(num, result): def handler(env, data): self.assertEqual(env.root.url1.as_url.with_host(), result) called_urls.append(num) return Response() return handler url1 = get_handler(1, 'http://example.com/url') url2 = get_handler(2, 'http://example.com:8000/url') url3 = get_handler(3, 'https://example.com:80/url') app = web.request_filter(config) | web.cases( web.match('/url', 'url1') | url1, web.match('/url2', 'url2') | url2, web.match('/url3', 'url3') | url3, ) root = web.Reverse.from_handler(app) assert web.ask(app, 'http://example.com/url') assert web.ask(app, 'http://example.com:8000/url2') assert web.ask(app, 'https://example.com:80/url3') assert called_urls == [1,2,3]
def test_by_method_default(self): app = web.match('/') | web.by_method({ 'DELETE': lambda e,d: Response('delete'), }, default_handler=lambda e,d: Response('default')) self.assertEqual(web.ask(app, '/', method="DELETE").body, 'delete') self.assertEqual(web.ask(app, '/').body, 'default')
def test_by_method(self): app = web.match("/") | web.by_method( {"DELETE": lambda e, d: Response("delete"), ("POST", "PUT"): lambda e, d: Response("post")} ) self.assertEqual(web.ask(app, "/", method="PUT").body, "post") self.assertEqual(web.ask(app, "/", method="DELETE").body, "delete") self.assertEqual(web.ask(app, "/").status_int, 405)
def test_by_method(self): app = web.match('/') | web.by_method({ 'DELETE': lambda e,d: Response('delete'), ('POST', 'PUT'): lambda e,d: Response('post'), }) self.assertEqual(web.ask(app, '/', method="PUT").body, 'post') self.assertEqual(web.ask(app, '/', method="DELETE").body, 'delete') self.assertEqual(web.ask(app, '/').status_int, 405)
def test_anonymouse(self): '`Auth` anonymouse access' response = web.ask(self.app, '/a') self.assertEqual(response.status_int, 200) self.assertEqual(response.body, b'ok') response = web.ask(self.app, '/b') self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/login?next=/b')
def test_by_method_default(self): app = web.match('/') | web.by_method( { 'DELETE': lambda e, d: Response('delete'), }, default_handler=lambda e, d: Response('default')) self.assertEqual(web.ask(app, '/', method="DELETE").body, b'delete') self.assertEqual(web.ask(app, '/').body, b'default')
def test_not_found(self): """Check int converter with handler which accepts params""" def handler(env, data): return Response() app = web.cases(web.match("/first", "first") | handler, web.match("/second/<int:id>", "second") | handler) self.assert_(web.ask(app, "/second/42/") is None) self.assert_(web.ask(app, "/second/42s") is None)
def test_int_converter(self): """Check int converter""" def handler(env, data): self.assertEqual(data.id, 42) return Response() app = web.cases(web.match("/first", "first") | handler, web.match("/second/<int:id>", "second") | handler) web.ask(app, "/second/42")
def test_by_method(self): app = web.match('/') | web.by_method( { 'DELETE': lambda e, d: Response('delete'), ('POST', 'PUT'): lambda e, d: Response('post'), }) self.assertEqual(web.ask(app, '/', method="PUT").body, b'post') self.assertEqual(web.ask(app, '/', method="DELETE").body, b'delete') self.assertEqual(web.ask(app, '/').status_int, 405)
def test_subdomain(self): '''Subdomain filter''' def handler(env, data): self.assertEqual(env.request.path, '/') return Response() def handler_with_prefix(env, data): self.assertEqual(env.request.path, '/j') return Response() app = web.subdomain('host') | web.cases( web.subdomain('') | web.match('/', 'index') | handler, web.subdomain('k') | web.cases( web.subdomain('l') | web.cases( web.match('/', 'l') | handler, ), web.subdomain('') | web.match('/', 'k') | handler), web.subdomain('j') | web.match('/', 'j1') | handler, web.subdomain('j') | web.match('/j', 'j2') | handler_with_prefix) self.assertEqual(web.ask(app, 'http://host/').status_int, 200) self.assertEqual(web.ask(app, 'http://k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://x.l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://j.host/').status_int, 200) # Test if subdomains in routing are cleaned properly self.assertEqual(web.ask(app, 'http://j.host/j').status_int, 200) self.assert_(web.ask(app, 'http://x.k.host/') is None) self.assert_(web.ask(app, 'http://lk.host/') is None) self.assert_(web.ask(app, 'http://mhost/') is None)
def test_subdomain(self): '''Subdomain filter''' def handler(env, data): self.assertEqual(env.request.path, '/') return Response() def handler_with_prefix(env, data): self.assertEqual(env.request.path, '/j') return Response() app = web.subdomain('host') | web.cases( web.subdomain('') | web.match('/', 'index') | handler, web.subdomain('k') | web.cases( web.subdomain('l') | web.cases(web.match('/', 'l') | handler, ), web.subdomain('') | web.match('/', 'k') | handler), web.subdomain('j') | web.match('/', 'j1') | handler, web.subdomain('j') | web.match('/j', 'j2') | handler_with_prefix) self.assertEqual(web.ask(app, 'http://host/').status_int, 200) self.assertEqual(web.ask(app, 'http://k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://x.l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://j.host/').status_int, 200) # Test if subdomains in routing are cleaned properly self.assertEqual(web.ask(app, 'http://j.host/j').status_int, 200) self.assert_(web.ask(app, 'http://x.k.host/') is None) self.assert_(web.ask(app, 'http://lk.host/') is None) self.assert_(web.ask(app, 'http://mhost/') is None)
def test_not_found(self): '''Check int converter with handler which accepts params''' def handler(env, data): return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) self.assert_(web.ask(app, '/second/42/') is None) self.assert_(web.ask(app, '/second/42s') is None)
def test_int_converter(self): '''Check int converter''' def handler(env, data): self.assertEqual(data.id, 42) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) web.ask(app, '/second/42')
def test_multiple_int_convs(self): '''Check multiple int converters''' def handler(env, data): self.assertEqual(data.id, 42) self.assertEqual(data.param, 23) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>/<int:param>', 'second') | handler) web.ask(app, '/second/42/23')
def test_handler_after_case(self): """Test if the handler next to cases is called""" r = Response() def handler(env, data): return r app = web.cases(web.match("/first", "first"), web.match("/second/<int:id>", "second")) | handler self.assertEqual(web.ask(app, "/first"), r) self.assertEqual(web.ask(app, "/second/2"), r)
def test_handler_after_case(self): '''Test if the handler next to cases is called''' r = Response() def handler(env, data): return r app = web.cases(web.match('/first', 'first'), web.match('/second/<int:id>', 'second')) | handler self.assertEqual(web.ask(app, '/first'), r) self.assertEqual(web.ask(app, '/second/2'), r)
def test_multiple_int_convs(self): """Check multiple int converters""" def handler(env, data): self.assertEqual(data.id, 42) self.assertEqual(data.param, 23) return Response() app = web.cases( web.match("/first", "first") | handler, web.match("/second/<int:id>/<int:param>", "second") | handler ) web.ask(app, "/second/42/23")
def test_current_location(self): def test_ns(env, data): return env.current_location app = web.cases( web.match('/', 'm1'), web.prefix('/ns', name="ns1.ns2") | web.cases( web.match(''), web.match('/m2', 'm2'), )) | test_ns self.assertEqual(web.ask(app, '/'), 'm1') self.assertEqual(web.ask(app, '/ns'), 'ns1.ns2') self.assertEqual(web.ask(app, '/ns/m2'), 'ns1.ns2.m2')
def test_handler_after_case(self): '''Test if the handler next to cases is called''' r = Response() def handler(env, data): return r app = web.cases( web.match('/first', 'first'), web.match('/second/<int:id>', 'second') ) | handler self.assertEqual(web.ask(app, '/first'), r) self.assertEqual(web.ask(app, '/second/2'), r)
def test_current_location(self): def test_ns(env, data): return env.current_location app = web.cases( web.match('/', 'm1'), web.prefix('/ns', name="ns1.ns2") | web.cases( web.match(''), web.match('/m2', 'm2'), ) ) | test_ns self.assertEqual(web.ask(app, '/'), 'm1') self.assertEqual(web.ask(app, '/ns'), 'ns1.ns2') self.assertEqual(web.ask(app, '/ns/m2'), 'ns1.ns2.m2')
def test_unicode(self): """Routing rules with unicode""" # XXX move to urltemplate and reverse tests? app = web.cases(web.prefix(u"/հայերեն") | web.cases(web.match(u"/%", "percent") | (lambda e, d: Response()))) encoded = "/%D5%B0%D5%A1%D5%B5%D5%A5%D6%80%D5%A5%D5%B6/%25" self.assertEqual(web.Reverse.from_handler(app).percent.as_url, encoded) self.assertEqual(web.Reverse.from_handler(app).percent.as_url.get_readable(), u"/հայերեն/%") self.assertNotEqual(web.ask(app, encoded), None) # ??? # rctx have prefixes, so we need new one self.assertEqual(web.ask(app, encoded).status_int, 200)
def test_unicode(self): """IRI tests""" app = web.subdomain(u"рф") | web.subdomain(u"сайт") | web.match("/", "site") | (lambda e, d: Response()) encoded = "http://xn--80aswg.xn--p1ai/" self.assertEqual(web.Reverse.from_handler(app).site.as_url.get_readable(), u"http://сайт.рф/") self.assertEqual(web.Reverse.from_handler(app).site.as_url, encoded) self.assertNotEqual(web.ask(app, encoded), None)
def test_match_empty_pattern(self): '''Test if match() works proper with empty patterns''' r = Response() app = web.prefix('/') | web.match('', 'index') | (lambda e, d: r) self.assertEqual(web.ask(app, '/'), r)
def test_login(self): '`SqlaModelAuth` login of valid user' response = self.login('user name', '123') self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assert_('Set-Cookie' in response.headers) cookie = response.headers['Set-Cookie'] response = web.ask(self.app, '/b', headers={'Cookie': cookie}) self.assertEqual(response.status_int, 200) self.assertEqual(response.body, 'ok') response = web.ask(self.app, '/logout', data={}, headers={'Cookie': cookie}) self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assert_(response.headers['Set-Cookie'].startswith('auth=; Max-Age=0; Path=/;'))
def test_unicode(self): '''IRI tests''' app = web.subdomain(u'рф') | web.subdomain(u'сайт') | web.match('/', 'site') | (lambda e,d: Response() ) encoded = 'http://xn--80aswg.xn--p1ai/' self.assertEqual(web.Reverse.from_handler(app).site.as_url.get_readable(), u'http://сайт.рф/') self.assertEqual(web.Reverse.from_handler(app).site.as_url, encoded) self.assertNotEqual(web.ask(app, encoded), None)
def test_match_empty_pattern(self): '''Test if match() works proper with empty patterns''' r = Response() app = web.prefix('/') | web.match('', 'index') | (lambda e,d: r) self.assertEqual(web.ask(app, '/'), r)
def test_simple_match(self): '''Method''' from webob.exc import HTTPMethodNotAllowed app = web.cases( web.match('/', 'simple') | web.method('post'), web.match('/second', 'second') | web.method('POST'), web.match('/strict', 'strict') | web.method('post', strict=True) ) | (lambda e,d: Response()) self.assertEqual(web.ask(app, '/'), None) self.assertEqual(web.ask(app, '/', method='post').status_int, 200) self.assertEqual(web.ask(app, '/second', method='post').status_int, 200) self.assertRaises(HTTPMethodNotAllowed, lambda: web.ask(app, '/strict').status_int) self.assertEqual(web.ask(app, '/strict', method='post').status_int, 200)
def test_protected_resource(self): '`Auth` requesting protected resource by logined user' response = self.login('user name', '123') response = web.ask(self.app, '/b', headers={'Cookie': response.headers['Set-Cookie']}) self.assertEqual(response.status_int, 200) self.assertEqual(response.body, b'ok')
def test_simple_match(self): """Method""" from webob.exc import HTTPMethodNotAllowed app = web.cases( web.match("/", "simple") | web.method("post"), web.match("/second", "second") | web.method("POST"), web.match("/strict", "strict") | web.method("post", strict=True), ) | (lambda e, d: Response()) self.assertEqual(web.ask(app, "/"), None) self.assertEqual(web.ask(app, "/", method="post").status_int, 200) self.assertEqual(web.ask(app, "/second", method="post").status_int, 200) self.assertRaises(HTTPMethodNotAllowed, lambda: web.ask(app, "/strict").status_int) self.assertEqual(web.ask(app, "/strict", method="post").status_int, 200)
def test_prefix_state(self): '''Prefix state correctness''' def handler(env, data): return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.namespace('doc') | web.cases( web.match('/item', '') | handler, web.prefix('/list') | web.cases(web.match('/item', 'list') | handler), web.match('/other-thing', 'something') | handler), web.match('/something', 'something') | handler) self.assertEqual(web.ask(app, '/docs/something'), None) self.assertEqual(web.ask(app, '/docs/list/something'), None) self.assertEqual(web.ask(app, '/docs/list/other-thing'), None)
def test_unicode(self): '''Routing rules with unicode''' # XXX move to urltemplate and reverse tests? app = web.cases( web.prefix(u'/հայերեն') | web.cases( web.match(u'/%', 'percent') | (lambda e, d: Response()))) encoded = '/%D5%B0%D5%A1%D5%B5%D5%A5%D6%80%D5%A5%D5%B6/%25' self.assertEqual(web.Reverse.from_handler(app).percent.as_url, encoded) self.assertEqual( web.Reverse.from_handler(app).percent.as_url.get_readable(), u'/հայերեն/%25') self.assertNotEqual(web.ask(app, encoded), None) # ??? # rctx have prefixes, so we need new one self.assertEqual(web.ask(app, encoded).status_int, 200)
def test_login(self): '`SqlaModelAuth` login of valid user' response = self.login('user name', '123') self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assertTrue('Set-Cookie' in response.headers) cookie = response.headers['Set-Cookie'] response = web.ask(self.app, '/b', headers={'Cookie': cookie}) self.assertEqual(response.status_int, 200) self.assertEqual(response.body, b'ok') response = web.ask(self.app, '/logout', data={}, headers={'Cookie': cookie}) self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assertTrue(response.headers['Set-Cookie']\ .startswith('auth=; Max-Age=0; Path=/;'))
def test_nested_namespaces(self): def test_ns(env, data): return env.namespace app = web.prefix('/ns1', name="ns1") | \ web.prefix('/ns2', name="ns2") | \ web.match() | test_ns self.assertEqual(web.ask(app, '/ns1/ns2'), 'ns1.ns2')
def test_logout(self): '`Auth` logout of logined user' response = self.login('user name', '123') response = web.ask(self.app, '/logout', data={}, headers={'Cookie': response.headers['Set-Cookie']}) self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assertTrue(response.headers['Set-Cookie']\ .startswith('auth=; Max-Age=0; Path=/;'))
def test_simple_match(self): '''Method''' from webob.exc import HTTPMethodNotAllowed app = web.cases( web.match('/', 'simple') | web.method('post'), web.match('/second', 'second') | web.method('POST'), web.match('/strict', 'strict') | web.method('post', strict=True)) | (lambda e, d: Response()) self.assertEqual(web.ask(app, '/'), None) self.assertEqual(web.ask(app, '/', method='post').status_int, 200) self.assertEqual( web.ask(app, '/second', method='post').status_int, 200) self.assertRaises(HTTPMethodNotAllowed, lambda: web.ask(app, '/strict').status_int) self.assertEqual( web.ask(app, '/strict', method='post').status_int, 200)
def test_unicode(self): '''IRI tests''' app = web.subdomain(u'рф') | web.subdomain(u'сайт') | web.match( '/', 'site') | (lambda e, d: Response()) encoded = 'http://xn--80aswg.xn--p1ai/' self.assertEqual( web.Reverse.from_handler(app).site.as_url.get_readable(), u'http://сайт.рф/') self.assertEqual(web.Reverse.from_handler(app).site.as_url, encoded) self.assertNotEqual(web.ask(app, encoded), None)
def test_prefix_root(self): '''Prefix root''' def handler(env, data): self.assertEqual(env._route_state.path, '/') return Response() app = web.cases( web.match('/', 'index') | handler, web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs'), None) self.assertEqual(web.ask(app, '/docs/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags'), None) self.assertEqual(web.ask(app, '/docs/tags/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags/asdasd'), None)
def test_aliases(self): def handler(env, data): return Response(env.current_location + ' ' + env.root.domain1.as_url + ' ' + env.root.domain1.as_url.with_host() + ' ' + env.root.domain2.as_url + ' ' + env.root.domain2.as_url.with_host()) www = web.subdomain('', 'www') app = web.subdomain( 'example.com', 'example.ru', 'example.com.ua') | web.cases( web.subdomain('ru', None, primary=None) | web.cases( web.subdomain('moscow') | www | web.match('/', 'domain3'), www | web.match('/', 'domain1')), web.subdomain('en', 'eng') | www | web.match('/', 'domain2'), ) app = app | handler # XXX As for now .with_host() return primary domain, not the current one # It is easy to change the behaviour, but which behaviour is # correct? self.assertEqual( web.ask(app, 'http://ru.example.com/').body, b'domain1 / http://example.com/ ' b'http://en.example.com/ http://en.example.com/') self.assertEqual( web.ask(app, 'http://www.ru.example.ru/').body, b'domain1 / http://example.com/ ' b'http://en.example.com/ http://en.example.com/') self.assertEqual( web.ask(app, 'http://www.example.ru/').body, b'domain1 / http://example.com/ ' b'http://en.example.com/ http://en.example.com/') self.assertEqual( web.ask(app, 'http://moscow.example.ru/').body, b'domain3 http://example.com/ http://example.com/ ' b'http://en.example.com/ http://en.example.com/')
def test_sane_exceptions(self): # XXX what is this? 0_o 'Not yet completed test of sane exceptions' @web.request_filter def get_items(env, data, nxt): return nxt(env, data) def raise_exc(env, data): raise Exception('somewhere deep inside') app = web.prefix('/prefix') | web.match('/', '') | get_items | raise_exc self.assertRaises(Exception, lambda: web.ask(app, '/prefix/'))
def test_prefix_leaf(self): '''Simple prefix''' def handler(env, data): self.assertEqual(env._route_state.path, '/item') return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs/item').status_int, 200)
def test_logout(self): '`Auth` logout of logined user' self.auth.crash_without_storage = False warnings = [] response = self.login('user name', '123') with mock.patch('logging.Logger.warning', side_effect=lambda m, *args:warnings.append(m % args)): response = web.ask(self.app, '/logout', data={}, headers={'Cookie': response.headers['Set-Cookie']}) self.assertEqual(response.status_int, 303) self.assertEqual(response.headers['Location'], '/') self.assertTrue(response.headers['Set-Cookie']\ .startswith('auth=; Max-Age=0; Path=/;')) self.assertEqual(len(warnings), 1) self.assertIn('storage', warnings[0]) self.assertIn('is unreachable', warnings[0])
def test_logout_anonymouse(self): '`Auth` logout of anonymouse' response = web.ask(self.app, '/logout', data={}) self.assertEqual(response.status_int, 303)
def login(self, login, password): return web.ask(self.app, '/login', data={'login':login, 'password':password})
def test_logout_by_get(self): '`Auth` logout by GET metod' response = web.ask(self.app, '/logout') self.assertEqual(response, None)