Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: Random redirect

  • [random.choice](https://docs.python.org/library/random.html#random.choice" %}
from flask import Flask, redirect
import random

app = Flask(__name__)

urls = [
    'https://code-maven.com/',
    'https://perlmaven.com/',
    'https://hostlocal.com/',
    'https://pydigger.com/',
    'https://szabgab.com/',
]

@app.route('/')
def index():
    return '<a href="/random">Random</a>'

@app.route('/random')
def random_redirect():
    return redirect(random.choice(urls))
import app

def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert rv.data == b'<a href="/random">Random</a>'


    rv = web.get('/random')
    assert rv.status == '302 FOUND'
    assert 'https://' in rv.headers['Location']
    print(rv.headers['Location'])
    assert rv.headers['Location'] in app.urls