如何在中测试重定向瓶子.py?

2024-03-29 08:02:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的瓶子应用程序中测试重定向。不幸的是,我没有找到测试重定向位置的方法。到目前为止,我只能够通过测试BottleException被引发来测试重定向是否被保持。在

def test_authorize_without_token(mocked_database_utils):
  with pytest.raises(BottleException) as resp:
    auth_utils.authorize()

有没有办法获得HTTP响应状态码或/和重定向位置?在

谢谢你的帮助。在


Tags: 方法testtoken应用程序瓶子pytestdefwith
1条回答
网友
1楼 · 发布于 2024-03-29 08:02:07

WebTest是测试WSGI应用程序的功能齐全且简单的方法。下面是一个检查重定向的示例:

from bottle import Bottle, redirect
from webtest import TestApp

# the real webapp
app = Bottle()


@app.route('/mypage')
def mypage():
    '''Redirect'''
    redirect('https://some/other/url')


def test_redirect():
    '''Test that GET /mypage redirects'''

    # wrap the real app in a TestApp object
    test_app = TestApp(app)

    # simulate a call (HTTP GET)
    resp = test_app.get('/mypage', status=[302])

    # validate the response
    assert resp.headers['Location'] == 'https://some/other/url'


# run the test
test_redirect()

相关问题 更多 >