Flask test doesn't populate request.authorization when username and password are URL encoded
<p>I'm trying to use the following test: </p>

<pre><code>def post_webhook(self, payload, **kwargs):
    webhook_username = 'test'
    webhook_password = 'test'

    webhook_url = 'http://{}:{}@localhost:8000/webhook_receive'
    webhook_receive = self.app.post(
        webhook_url.format(webhook_username, webhook_password),
        referrer='http://localhost:8000',
        json=payload)
    return webhook_receive.status_code
</code></pre>

<p>However the main issue is <code>request.authorization</code> is <code>None</code>. Though if I launch the server and use <code>curl -X POST &lt;webhook_url&gt;</code> or <code>requests.post(&lt;webhook_url&gt;)</code>, then <code>request.authorization</code> is properly populated. </p>

<p>Trying to figure out the main issue of how to fix this problem. </p>

<p>Using the <a href="http://flask.pocoo.org/snippets/8/" rel="nofollow">snippet</a> code and the <a href="http://flask.pocoo.org/docs/0.11/testing/#testing" rel="nofollow">Flask Test Client</a>, the next pytest code works for me. The way I send the HTTP Basic Auth is the same way curl and HTTPie send it; in the <strong><code>Authorization</code></strong> header with the user and password encoded in <strong>base64</strong>.</p>

<pre><code>import base64
from app import app

def test_secret_endpoint():
    client = app.test_client()

    # testing a route without authentication required
    rv = client.get('/')
    assert rv.status_code == 200

    # testing a secured route without credentials
    rv = client.post('/secret-page')
    assert rv.status_code == 401

    # testing a secured route with valid credentials
    value = base64.encodestring('admin:secret').replace('\n', '')

    headers = {'Authorization': 'Basic {}'.format(value)}

    rv = client.post('/secret-page', headers=headers)
    assert rv.status_code == 200
    assert 'This is secret' in rv.data
</code></pre>

<p>The route definitions are:</p>

<pre><code>@app.route('/')
def index():
    return 'Hello World :)'


@app.route('/secret-page', methods=['POST'])
@requires_auth
def secret_page():
    return 'This is secret'
</code></pre>

<p>The request header sending the credentials looks something like this:</p>

<pre><code>POST /secret-page HTTP/1.1
Accept: */*
Authorization: Basic YWRtaW46c2VjcmV0
Connection: keep-alive
Content-Length: 0
Host: localhost:5000
...
</code></pre>

python testing flask