def test_login(): """测试登录""" # 测试 GET 请求 request_message = 'GET /login HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' request = Request(request_message) route, method = routes.get(request.path) r = route(request) assert 'Login' in r # 测试 POST 请求 username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) u = User(username=username, password=password) u.save() request_message = f'POST /login HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' \ f'username={username}&password={raw_password}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) u.delete() s = Session.get(r.cookies.get('session_id')) s.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert b'session_id' in bytes(r) r = route(request) assert r.decode('utf-8') == '用户名或密码不正确'
def make_response(request, headers=None): """构造响应报文""" # 默认状态码为 200 status = 200 # 处理静态资源请求 if request.path.startswith('/static'): route, methods = routes.get('/static') else: route, methods = routes.get(request.path) # 如果请求方法不被允许返回 405 状态码 if request.method not in methods: status = 405 data = 'Method Not Allowed' else: # 请求首页时 route 实际上就是我们在 controllers.py 中定义的 index 视图函数 data = route(request) # 如果返回结果为 Response 对象,直接获取响应报文 if isinstance(data, Response): response_bytes = bytes(data) else: # 返回结果为字符串,需要先构造 Response 对象,然后再获取响应报文 response = Response(data, headers=headers, status=status) response_bytes = bytes(response) print(f'response_bytes: {response_bytes}') return response_bytes
def test_delete(): """测试删除 todo""" username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) u = User(username=username, password=password) u.save() s = Session(user_id=u.id, expire_in='2099-12-31 00:00:00') s.save() content = uuid.uuid4().hex t = Todo(user_id=u.id, content=content) t.save() request_message = f'POST /delete HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n' \ f'Cookie: session_id={s.id}\r\n\r\nid={t.id}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(user_id=u.id, content=content, ensure_one=True) u.delete() s.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t is None r = route(request) assert b'302 FOUND' in bytes(r) assert b'/login' in bytes(r)
def test_index(): """测试首页""" request_message = 'GET / HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' request = Request(request_message) route, method = routes.get(request.path) r = route(request) assert b'Todo List' in bytes(r, encoding='utf-8') assert b'/new' in bytes(r, encoding='utf-8')
def make_response(request, headers=None): """构造响应报文 Args: request: 请求对象 headers: 响应头 Returns: 返回响应报文 """ # 默认状态码为 200 status = 200 # 处理静态资源请求 if request.path.startswith('/static'): route, methods = routes.get('/static') else: try: route, methods = routes.get(request.path) except TypeError: # 返回给用户 404 页面 return bytes(errors[404]()) # 如果请求方法不被允许返回 405 状态码 if request.method not in methods: status = 405 data = 'Method Not Allowed' else: # 请求首页时 route 实际上就是我们在 controllers.py 中定义的 index 视图函数 data = route(request) # 如果返回结果为 Response 对象,直接获取响应报文 if isinstance(data, Response): response_bytes = bytes(data) else: # 返回结果为字符串,需要先构造 Response 对象,然后再获取响应报文 response = Response(data, headers=headers, status=status) response_bytes = bytes(response) logger(f'response_bytes: {response_bytes}') return response_bytes
def test_new(): """测试新增 todo""" content = uuid.uuid4().hex request_message = f'POST /new HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\ncontent={content}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=content, ensure_one=True) t.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t.content == content
def test_register(): """测试注册""" # 测试 GET 请求 request_message = 'GET /register HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' request = Request(request_message) route, method = routes.get(request.path) r = route(request) assert 'Register' in r # 测试 POST 请求 username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) request_message = f'POST /register HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' \ f'username={username}&password={password}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) u = User.find_by(username=username, ensure_one=True) u.delete() assert b'302 FOUND' in bytes(r) assert b'/login' in bytes(r)
def test_delete(): """测试删除 todo""" content = uuid.uuid4().hex t = Todo(content=content) t.save() request_message = f'POST /delete HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\nid={t.id}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=content, ensure_one=True) assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t is None
def test_edit(): """测试编辑 todo""" content = uuid.uuid4().hex t = Todo(content=content) t.save() edit_content = 'Edit: ' + content request_message = f'POST /edit HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' \ f'id={t.id}&content={edit_content}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=edit_content, ensure_one=True) t.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t.content == edit_content
def make_response(request, headers=None): """构造响应报文""" # 默认状态码为 200 status = 200 # 获取匹配当前请求路径的处理函数和函数所接收的请求方法 # request.path 等于 '/' 或 '/index' 时,routes.get(request.path) 将返回 (index, ['GET']) route, methods = routes.get(request.path) # 如果请求方法不被允许,返回 405 状态码 if request.method not in methods: status = 405 data = 'Method Not Allowed' else: # 请求首页时 route 实际上就是我们在 controllers.py 中定义的 index 视图函数 data = route() # 获取响应报文 response = Response(data, headers=headers, status=status) response_bytes = bytes(response) print(f'response_bytes: {response_bytes}') return response_bytes
def test_index(): """测试首页""" username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) u = User(username=username, password=password) u.save() s = Session(user_id=u.id, expire_in='2099-12-31 00:00:00') s.save() request_message = f'GET / HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n' \ f'Cookie: session_id={s.id}\r\n\r\n' request = Request(request_message) route, method = routes.get(request.path) r = route(request) u.delete() s.delete() assert b'Todo List' in bytes(r, encoding='utf-8') assert b'/new' in bytes(r, encoding='utf-8') r = route(request) assert b'302 FOUND' in bytes(r) assert b'/login' in bytes(r)