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 process_connection(client): """处理客户端请求 Args: client: 客户端请求对象 Returns: None """ request_bytes = b'' while True: chunk = client.recv(BUFFER_SIZE) request_bytes += chunk if len(chunk) < BUFFER_SIZE: break # 请求报文 request_message = request_bytes.decode('utf-8') logger(f'request_message: {request_message}') # 解析请求报文 request = Request(request_message) try: # 根据请求报文构造响应报文 response_bytes = make_response(request) except Exception as e: logger(e) # 返回给用户 500 页面 response_bytes = bytes(errors[500]()) # 返回响应 client.sendall(response_bytes) # 关闭连接 client.close()
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 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 test_request(): """测试 Request 类""" request_message = 'GET /moin/PythonBooks HTTP/1.1\r\n' \ 'Host: wiki.python.org\r\n' \ 'Connection: keep-alive\r\n' \ 'Cache-Control: max-age=0\r\n' \ 'Upgrade-Insecure-Requests: 1\r\n' \ 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36\r\n' \ 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n' \ 'Accept-Encoding: gzip, deflate, br\r\n' \ 'Accept-Language: zh-CN,zh;q=0.9\r\n\r\n' request = Request(request_message) assert request.method == 'GET' assert request.path == '/moin/PythonBooks' assert request.args == {} assert request.form == {} assert len(request.headers) == 8 assert request.headers['Host'] == 'wiki.python.org'
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)
def process_connection(client): """处理客户端请求""" # 接收请求报文数据 request_bytes = b'' while True: chunk = client.recv(BUFFER_SIZE) request_bytes += chunk if len(chunk) < BUFFER_SIZE: break # 请求报文 request_message = request_bytes.decode('utf-8') logger(f'request_message: {request_message}') # 解析请求报文,构造请求对象 request = Request(request_message) # 根据请求对象构造响应报文 response_bytes = make_response(request) # 返回响应 client.sendall(response_bytes) # 关闭连接 client.close()