def test_session(self): self.session_id = uuid.uuid4() def genId(req): return self.session_id.hex router = Router() sessionManager = BaseSessionManager(genId) router.pipe()(sessionManager) @router.get() def index(req, res): if 'alice' in sessionManager.session: res.set_data(sessionManager.session['alice']) else: sessionManager.session['alice'] = 'bob' res.set_data('Index') return res c = Client(router.build(), Response) res = c.get('/') self.assertEqual(res.data, b'Index') res = c.get('/') self.assertEqual(res.data, b'bob') c = Client(router.build(), Response) self.session_id = uuid.uuid4() res = c.get('/') self.assertEqual(res.data, b'Index')
def test_method(self): self.index_get_called = False self.index_post_called = False def index_get(req, res): self.index_get_called = True res.set_data('Index_get') return res def index_post(req, res): self.index_post_called = True res.set_data('Index_post') return res router = Router() router._create_endpoint(index_get, '/', ['GET']) router._create_endpoint(index_post, '/', ['POST']) c = Client(router.build(), Response) res = c.get('/') self.assertTrue(self.index_get_called) self.assertFalse(self.index_post_called) self.assertEqual(res.data, b'Index_get') self.index_get_called = False self.index_post_called = False res = c.post('/') self.assertFalse(self.index_get_called) self.assertTrue(self.index_post_called) self.assertEqual(res.data, b'Index_post')
def test_subrouter(self): self.middleware_called = False self.endpoint_called = False self.middleware_a_called = False self.endpoint_a_called = False def middleware(req, res, next): self.middleware_called = True return next(req, res) def endpoint(req, res): self.endpoint_called = True res.set_data('Index') return res router = Router() router._create_middleware(middleware, '/', ['GET']) router._create_endpoint(endpoint, '/', ['GET']) def middleware_a(req, res, next): self.middleware_a_called = True return next(req, res) def endpoint_a(req, res): self.endpoint_a_called = True res.set_data('A') return res router_a = Router() router_a._create_middleware(middleware_a, '/b', ['GET']) router_a._create_endpoint(endpoint_a, '/b', ['GET']) router.mount('/a', router_a) with self.subTest('test / path'): c = Client(router.build(), Response) res = c.get('/') self.assertTrue(self.middleware_called) self.assertTrue(self.endpoint_called) self.assertFalse(self.endpoint_a_called) self.assertEqual(res.data, b'Index') self.middleware_called = False self.endpoint_called = False self.middleware_a_called = False self.endpoint_a_called = False with self.subTest('test /a path'): c = Client(router._application, Response) res = c.get('/a/b') self.assertTrue(self.middleware_called) self.assertFalse(self.endpoint_called) self.assertTrue(self.middleware_a_called) self.assertTrue(self.endpoint_a_called) self.assertEqual(res.data, b'A')
def test_static_file(self): router = Router() router.pipe('/data')(StaticMiddleware(dir=os.getcwd())) @router.get() def index(req, res): res.set_data('Index') return res c = Client(router.build(), Response) res = c.get('/data/text.txt') self.assertEqual(res.data, b'Test data')
def test_decorators(self): self.middleware_called = False self.endpoint_called = False self.middleware_a_called = False self.endpoint_a_called = False router = Router() @router.pipe() def middleware(req, res, next): self.middleware_called = True return next(req, res) @router.pipe('/a') def middleware_a(req, res, next): self.middleware_a_called = True return next(req, res) @router.get('/a') def index_a(req, res): self.endpoint_a_called = True res.set_data('Index /a') return res @router.get() def index(req, res): self.endpoint_called = True res.set_data('Index /') return res c = Client(router.build(), Response) res = c.get('/') self.assertEqual(res.data, b'Index /') self.assertTrue(self.middleware_called) self.assertTrue(self.endpoint_called) self.assertFalse(self.endpoint_a_called) self.assertFalse(self.middleware_a_called) self.middleware_called = False self.endpoint_called = False self.middleware_a_called = False self.endpoint_a_called = False res = c.get('/a') self.assertEqual(res.data, b'Index /a') self.assertTrue(self.middleware_called) self.assertTrue(self.endpoint_a_called) self.assertTrue(self.middleware_a_called) self.assertFalse(self.endpoint_called)
def test_compression(self): router = Router() router.pipe()(CompressionMiddleware()) @router.get() def index(req, res): res.set_data('Index') return res c = Client(router.build(), Response) res = c.get('/', headers={'Accept-Encoding': 'gzip'}) self.assertEqual(res.headers.get('Content-Encoding'), 'gzip')
def test_index(self): self.index_called = False def index(req, res): self.index_called = True res.set_data('Index') return res router = Router() router._create_endpoint(index, '/', ['GET']) c = Client(router.build(), Response) res = c.get('/') self.assertTrue(self.index_called) self.assertEqual(res.data, b'Index')
def test_script_name(self): self.middleware_called = False router = Router() @router.pipe('/img') def images(req, res, next): self.middleware_called = True self.assertEqual(req.environ['SCRIPT_NAME'], '/flower/') return next(req, res) @router.get('/img/flower') def index(req, res): res.set_data('Flower') return res c = Client(router.build(), Response) res = c.get('/img/flower') self.assertEqual(res.data, b'Flower') self.assertTrue(self.middleware_called)
def test_wildcard(self): self.middleware_called = False self.endpoint_called = False self.endpoint_a_called = False def middleware(req, res, next): self.middleware_called = True return next(req, res) def endpoint_a(req, res): self.endpoint_a_called = True res.set_data('a') return res def endpoint(req, res): self.endpoint_called = True res.set_data('Index') return res router = Router() router._create_middleware(middleware, '%', ['GET']) router._create_endpoint(endpoint_a, '/a', ['GET']) router._create_endpoint(endpoint, '/', ['GET']) c = Client(router.build(), Response) res = c.get('/') self.assertTrue(self.middleware_called) self.assertTrue(self.endpoint_called) self.assertFalse(self.endpoint_a_called) self.assertEqual(res.data, b'Index') self.middleware_called = False self.endpoint_called = False self.endpoint_a_called = False c = Client(router._application, Response) res = c.get('/a') self.assertTrue(self.middleware_called) self.assertFalse(self.endpoint_called) self.assertTrue(self.endpoint_a_called) self.assertEqual(res.data, b'a')
res.status_code = 404 return res router.pipe()(sessionManager) @router.get('/signin') def signin(req:Request, res: Response): if sessionManager.user is None: user = User() user.id = 1 user.username = '******' user.password = '******' sessionManager.signin_user(user) res.status_code = 302 res.headers.add('Location', '/') return res @router.get('/') def index(req, res): if sessionManager.user is None: return redirect('/signin') print(f'{sessionManager.user.username} is logged in') res.set_data('Index') return res with simple_server.make_server('', 3000, router.build()) as httpd: httpd.serve_forever()
res = c.get('/hakim/30000') print(res.data) # assert res.data == b'900 route' new_mem = process.memory_info().rss print(new_mem - mem) mem = new_mem router = Router() with time_it('create 60000 routes wsgitools router', fd=fd): for i in range(60000): route_desc = copy.copy(str(i) + ' route') @router.get(rule='/<name>/' + str(i)) def route(req, res): res.set_data(route_desc) return res c = Client(router.build(), Response) with time_it('call route from wsgitools.router', fd=fd): res = c.get('/hakim/30000') print(res.data) # assert res.data == b'900 route' new_mem = process.memory_info().rss print(new_mem - mem) mem = new_mem