def __init__(self, **kw): super(Service, self).__init__(**kw) self.resources = Resource.create(self.resources) self.middleware = [Middleware.create(x) for x in (self.middleware or [])] if self.root is not None: self.root = os.path.abspath( os.path.join(self.path, self.root) ) self.resources.append(Resource.create(dict(type='static', url='', root=self.root, strict=False)))
def test_resource(self): r = Resource(url='r/', site="*") self.assertEqual( r.match_path('r/'), "" ) self.assertTrue( r.match_site('anything') ) # If we add a keyword argument that isn't allowed, a TypeError is raised. self.assertRaises( TypeError, Resource, not_a_key = True )
def test_app(self): def app(environ, start_response): start_response("200 OK", []) return ("App!",) r = Resource.create({ 'type': 'app', 'app': app }) content, _, _ = self.mock_request(r) self.assertEqual( content, "App!" )
def test_layout(self): r = Resource.create([ Simple( content="A", url = "a/" ), Simple( content="B", url = "b/" ), ]) result = {} content, status, _ = self.mock_request(r, PATH_INFO='a/') self.assertEqual( status, "200 OK") self.assertEqual( content, "A") content, status, _ = self.mock_request(r, PATH_INFO='b/') self.assertEqual( status, "200 OK") self.assertEqual( content, "B") _, status, _ = self.mock_request(r, PATH_INFO='favicon.ico') self.assertEqual( status, "404 Not Found")
def start(self): super(ProxyService, self).start() self._proxy = Resource.create({'type': 'proxy', 'address': self.address})
def test_create(self): r = Resource.create({ "type": "simple" }) self.assertEqual(r.__class__, Simple)
def test_site(self): host_a = Resource(url='', site='host_a') host_b = Resource(url='', site='host_b') host_a_or_b = Resource(url='', site=['host_a', 'host_b']) anything = Resource(url='', site='*') host_none = Resource(url='', site=None) self.assertTrue( host_a.match_site('host_a') ) self.assertFalse( host_a.match_site('host_b') ) self.assertFalse( host_b.match_site('host_a') ) self.assertTrue( host_b.match_site('host_b') ) self.assertTrue( host_a_or_b.match_site('host_a') ) self.assertTrue( host_a_or_b.match_site('host_a') ) self.assertFalse( host_a_or_b.match_site('host_z') ) self.assertTrue( anything.match_site('host_a') ) self.assertTrue( anything.match_site('host_b') ) self.assertTrue( anything.match_site('host_z') ) self.assertFalse( host_none.match_site('host_a') ) self.assertFalse( host_none.match_site('host_b') ) self.assertFalse( host_none.match_site('host_z') )
def simple(self): return Resource.simple(self)
def __init__(self, address): self.resources = Resource.create([]) self.services = Resource.create([]) self.address = address