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

How to test redirections in Bottle.py?

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

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

有没有办法获取HTTP响应状态码or/and重定向位置?

感谢您的帮助。

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()