我希望有人可以帮助我。
我必须在烧瓶 api 中使用 Python 的单元测试编写单元测试。我有一个登录路由,当通过带有 React 前端的应用程序访问它时,它工作得非常好,但是每当我尝试从测试中发布时,request.authorization 是 None ......它让我发疯
我查看了整个互联网并尝试了很多不同的方法,但是无论我做什么,进行测试时 request.authorization 始终为 None
测试:
import unittest
import base64
from backend.peace_api import app
class TestLogin(unittest.TestCase):
# Assert login() with correct authentication
def test_login(self):
with app.app_context():
tester = app.test_client(self)
auth = 'seo@hotmail.com:password'
authheader = base64.b64encode(bytes(auth, 'UTF-8'))
headers = {"HTTP_AUTHORIZATION": "Bearer " + str(authheader), "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
response = tester.post('/api/login/', headers=dict(headers))
print(response.json)
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
路线 :
import jwt
import datetime
from flask import Blueprint, request, jsonify
from …Run Code Online (Sandbox Code Playgroud)