为什么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 (将#修改为@)