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_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_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_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_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_subdomains_and_namespace(self): app = ( web.subdomain("d2") | web.namespace("subdomain") | web.subdomain("d1") | web.namespace("more") | web.match("/", "index") ) r = web.Reverse.from_handler(app) self.assertEqual(r.subdomain.more.index.as_url, "http://d1.d2/")
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_subdomain_after_namespace(self): app = web.subdomain('news', name='news') | web.subdomain('eng') | web.cases( web.match('/'), web.match('/<int:id>', 'item'), ) r = web.Reverse.from_handler(app) self.assertEqual(r.build_url('news.item', id=1), 'http://eng.news/1') self.assertEqual(r.build_url('news'), 'http://eng.news/') self.assertEqual(r.news.item(id=1).as_url, 'http://eng.news/1') self.assertEqual(r.news.as_url, 'http://eng.news/')
def test_subdomains_and_cases(self): "Locations of web.cases with subdomains" chain = web.subdomain("news") | web.cases(web.match("/", "index"), web.match("/docs", "docs")) for k in ("index", "docs"): self.assert_(web.locations(chain).keys(), [k]) self.assert_(web.locations(chain)[k][0].subdomains) self.assertEqual(web.locations(chain)[k][0].subdomains, ["news"])
def test_subdomain_ns_shortcut(self): view = lambda e, d: None chain = web.subdomain('www', name='namespace') | view assert isinstance(chain.next_handler, web.namespace) self.assertEqual(chain.next_handler.namespace, 'namespace') self.assertEqual(chain.next_handler.next_handler, view)
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_subdomain(self): 'Locations and subdomains' chain = web.subdomain('news') | web.match('/', 'index') self.assert_(location_keys(chain), ['index']) self.assert_(locations(chain)['index'][0].subdomains) subdomains = [x.primary for x in locations(chain)['index'][0].subdomains] self.assertEqual(subdomains, ['news'])
def test_external_urls_no_port(self): "External URL reverse with no port in Request.host (sometimes happens using Flup)" def host1(env, data): if ":" in env.request.host: env.request.host = env.request.host.split(":")[0] 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() app = web.subdomain("example.com") | web.cases( web.subdomain("host1") | web.match("/url", "host1") | host1, web.subdomain("host2") | web.match("/url", "host2"), ) assert web.ask(app, "http://host1.example.com/url") assert self.host1_called
def test_external_urls_no_port(self): 'External URL reverse with no port in Request.host (sometimes happens using Flup)' def host1(env, data): if ':' in env.request.host: env.request.host = env.request.host.split(':')[0] 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() app = web.subdomain('example.com') | web.cases ( web.subdomain('host1') | web.match('/url', 'host1') | host1, web.subdomain('host2') | web.match('/url', 'host2'), ) assert web.ask(app, 'http://host1.example.com/url') assert self.host1_called
def test_subdomains_and_cases(self): 'Locations of web.cases with subdomains' chain = web.subdomain('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(location_keys(chain), [k]) self.assert_(locations(chain)[k][0].subdomains) subdomains = [x.primary for x in locations(chain)[k][0].subdomains] self.assertEqual(subdomains, ['news'])
def test_repr(self): # for coverage '%r' % web.WebHandler() '%r' % web.cases() '%r' % web.request_filter(lambda e, d, n: None) '%r' % web.match('/', 'index') '%r' % web.method('GET') '%r' % web.static_files('/prefix') '%r' % web.prefix('/prefix') '%r' % web.subdomain('name') '%r' % web.namespace('name')
def test_repr(self): # for coverage "%r" % web.WebHandler() "%r" % web.cases() "%r" % web.request_filter(lambda e, d, n: None) "%r" % web.match("/", "index") "%r" % web.method("GET") "%r" % web.static_files("/prefix") "%r" % web.prefix("/prefix") "%r" % web.subdomain("name") "%r" % web.namespace("name")
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_subdomain(self): """Subdomain filter""" def handler(env, data): self.assertEqual(env.request.path, "/") 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, ), ) 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.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_unicode(self): 'Reverse with unicode' # various combinations of url parts containing unicode chain = web.subdomain(u'п') | web.cases( web.prefix(u'/з') | web.match('/', 'unicode1'), web.prefix(u'/з') | web.match('/<string:slug>', 'unicode2'), web.match(u'/д/<string:slug>', 'unicode3'), #regression web.match(u'/<string:slug1>/<string:slug2>', 'unicode4'), #regression ) r = web.Reverse.from_handler(chain) self.assertEqual(r.unicode1.as_url, 'http://xn--o1a/%D0%B7/') self.assertEqual(str(r.unicode1), 'http://xn--o1a/%D0%B7/') self.assertEqual(r.unicode2(slug=u'ю').as_url, 'http://xn--o1a/%D0%B7/%D1%8E') self.assertEqual(r.unicode3(slug=u'ю').as_url, 'http://xn--o1a/%D0%B4/%D1%8E') self.assertEqual(r.unicode4(slug1=u'д', slug2=u'ю').as_url, 'http://xn--o1a/%D0%B4/%D1%8E')
def test_unicode(self): "Reverse with unicode" # various combinations of url parts containing unicode chain = web.subdomain(u"п") | web.cases( web.prefix(u"/з") | web.match("/", "unicode1"), web.prefix(u"/з") | web.match("/<string:slug>", "unicode2"), web.match(u"/д/<string:slug>", "unicode3"), # regression web.match(u"/<string:slug1>/<string:slug2>", "unicode4"), # regression ) r = web.Reverse.from_handler(chain) self.assertEqual(r.unicode1.as_url, "http://xn--o1a/%D0%B7/") self.assertEqual(str(r.unicode1), "http://xn--o1a/%D0%B7/") self.assertEqual(r.unicode2(slug=u"ю").as_url, "http://xn--o1a/%D0%B7/%D1%8E") self.assertEqual(r.unicode3(slug=u"ю").as_url, "http://xn--o1a/%D0%B4/%D1%8E") self.assertEqual(r.unicode4(slug1=u"д", slug2=u"ю").as_url, "http://xn--o1a/%D0%B4/%D1%8E")
def test_subdomain(self): "Locations and subdomains" chain = web.subdomain("news") | web.match("/", "index") self.assert_(web.locations(chain).keys(), ["index"]) self.assert_(web.locations(chain)["index"][0].subdomains) self.assertEqual(web.locations(chain)["index"][0].subdomains, ["news"])
def test_subdomains_and_namespace(self): app = web.subdomain('d2') | web.namespace('subdomain') | \ web.subdomain('d1') | web.namespace('more') | \ web.match('/', 'index') r = web.Reverse.from_handler(app) self.assertEqual(r.subdomain.more.index.as_url, 'http://d1.d2/')
def test_port(self): chain = web.subdomain(u'example.com:8000') | web.match('/', name="index") r = web.Reverse.from_handler(chain) self.assertEqual(r.index.as_url.port, '8000') self.assertEqual(r.index.as_url.host, 'example.com')