def __init__(self, path, app):
self.path = path
self.app = app
- regex = '^' + path + '$'
- regex = re.sub('{([a-zA-Z_][a-zA-Z0-9_]*)}', r'(?P<\1>[^/]*)', regex)
+ regex = "^" + path + "$"
+ regex = re.sub("{([a-zA-Z_][a-zA-Z0-9_]*)}", r"(?P<\1>[^/]*)", regex)
self.path_regex = re.compile(regex)
def matches(self, scope):
- match = self.path_regex.match(scope['path'])
+ match = self.path_regex.match(scope["path"])
if match:
- kwargs = dict(scope.get('kwargs', {}))
+ kwargs = dict(scope.get("kwargs", {}))
kwargs.update(match.groupdict())
child_scope = scope.copy()
- child_scope['kwargs'] = kwargs
+ child_scope["kwargs"] = kwargs
return True, child_scope
return False, {}
def __init__(self, path, app):
self.path = path
self.app = app
- regex = '^' + path
- regex = re.sub('{([a-zA-Z_][a-zA-Z0-9_]*)}', r'(?P<\1>[^/]*)', regex)
+ regex = "^" + path
+ regex = re.sub("{([a-zA-Z_][a-zA-Z0-9_]*)}", r"(?P<\1>[^/]*)", regex)
self.path_regex = re.compile(regex)
def matches(self, scope):
- match = self.path_regex.match(scope['path'])
+ match = self.path_regex.match(scope["path"])
if match:
- kwargs = dict(scope.get('kwargs', {}))
+ kwargs = dict(scope.get("kwargs", {}))
kwargs.update(match.groupdict())
child_scope = scope.copy()
- child_scope['kwargs'] = kwargs
- child_scope['root_path'] = scope.get('root_path', '') + match.string
- child_scope['path'] = scope['path'][match.span()[1]:]
+ child_scope["kwargs"] = kwargs
+ child_scope["root_path"] = scope.get("root_path", "") + match.string
+ child_scope["path"] = scope["path"][match.span()[1] :]
return True, child_scope
return False, {}
return self.not_found(scope)
def not_found(self, scope):
- return Response('Not found', 404, media_type='text/plain')
+ return Response("Not found", 404, media_type="text/plain")
def homepage(scope):
- return Response('Hello, world', media_type='text/plain')
+ return Response("Hello, world", media_type="text/plain")
def users(scope):
- return Response('All users', media_type='text/plain')
+ return Response("All users", media_type="text/plain")
def user(scope):
- content = 'User ' + scope['kwargs']['username']
- return Response(content, media_type='text/plain')
+ content = "User " + scope["kwargs"]["username"]
+ return Response(content, media_type="text/plain")
-app = Router([
- Path('/', app=homepage),
- PathPrefix('/users', app=Router([
- Path('', app=users),
- Path('/{username}', app=user),
- ]))
-])
+app = Router(
+ [
+ Path("/", app=homepage),
+ PathPrefix(
+ "/users", app=Router([Path("", app=users), Path("/{username}", app=user)])
+ ),
+ ]
+)
def test_router():
client = TestClient(app)
- response = client.get('/')
+ response = client.get("/")
assert response.status_code == 200
- assert response.text == 'Hello, world'
+ assert response.text == "Hello, world"
- response = client.get('/foo')
+ response = client.get("/foo")
assert response.status_code == 404
- assert response.text == 'Not found'
+ assert response.text == "Not found"
- response = client.get('/users')
+ response = client.get("/users")
assert response.status_code == 200
- assert response.text == 'All users'
+ assert response.text == "All users"
- response = client.get('/users/tomchristie')
+ response = client.get("/users/tomchristie")
assert response.status_code == 200
- assert response.text == 'User tomchristie'
+ assert response.text == "User tomchristie"