如何解决为什么WebTest从注销测试接收HTTP 401状态?
我有一个使用WebTest进行功能测试的小型Flask应用程序。我在测试中遇到了一个问题,其中WebTest在尝试遵循注销过程时收到401未经授权的错误:
def test_sees_alert_on_log_out(self,user,testapp):
'''Show alert on logout.'''
res = testapp.get('/')
# Fills out login form in navbar
form = res.forms['loginForm']
form['username'] = user.username
form['password'] = 'myprecious'
# Submits
res = form.submit()
res = testapp.get(url_for('public.logout'))
FAILED tests/test_functional.py::TestLoggingIn::test_sees_alert_on_log_out - webtest.app.AppError: Bad response: 401 UNAUTHORIZED (not 200 OK or 3xx redirect for http://localhost/logout/)
这是注销和主视图功能:
blueprint = Blueprint('public',__name__,static_folder='../static')
@blueprint.route('/',methods=['GET','POST'])
def home():
'''Home page.'''
form = LoginForm(request.form)
current_app.logger.info('Hello from the home page!')
# Handle logging in
if request.method == 'POST':
if form.validate_on_submit():
login_user(form.user)
flash('You are logged in.','success')
redirect_url = request.args.get('next') or url_for('user.profile',username=form.user.username)
current_app.logger.info(f'logging in - redirecting to {redirect_url}')
return redirect(redirect_url)
else:
flash_errors(form)
return render_template('public/home.html',form=form)
@blueprint.route('/logout/')
@login_required
def logout():
'''Logout.'''
url = url_for('public.home')
current_app.logger.info(f'logging out - redirecting to {url}')
logout_user()
flash('You are logged out.','info')
return redirect(url)
在Docker容器中运行应用程序时,登录/注销过程按预期方式工作,但是我无法理解为什么WebTest在注销时会收到401错误。我对此概念有误解,但无法从其他论坛帖子或文档中找到相关信息。为什么WebTest套件中的行为与实际运行服务器不同?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。