2017-12-11 6 views
0

ボトルアプリケーションでリダイレクトをテストしたかったのです。残念ながら、私はリダイレクションの場所をテストする方法を見つけることができませんでした。これまでのところ、BottleExceptionが発生したことをテストすることで、リダイレクトが行われたことをテストできました。Bottle.pyでリダイレクトをテストするには?

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

HTTP応答ステータスコードまたはリダイレクトの場所を取得する方法はありますか。

ありがとうございました。

+0

「Webtest」はあなたが望むことをします。 https://docs.pylonsproject.org/projects/webtest/en/latest/ –

答えて

3

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

ニース、感謝して、私の問題を解決しました。 – Sudet

関連する問題