unit testing - python - Flask test_client() doesn't have request.authorization with pytest -
i have problem when testing flask app pytest.
app required basic auth parameters of request.authorization
in flask.
pytest, flask.test_client() doesn't have request.authorization
.
here's code of fixture:
@pytest.yield_fixture(scope='session') def app() app = create_app() # setup code ctx = app.app_context() ctx.push() yield app ctx.pop() # teadown code @pytest.fixture def test_client(app) return app.test_client()
here's code of test:
def test_index(test_client): res = test_client.get("/", headers={"authorization": "basic {user}".format(user=b64encode(b"test_user"))}) assert res.status_code == 200
when run test, got error:
e assert 401 == 200 e + 401 = <response streamed [401 unauthorized]>.status_code
not auth failure, request.authorization doesn't have value(none).
why happen? there solution?
thanks.
the credentials http basic authentication must have username , password separated colon. try this:
def test_index(test_client): res = test_client.get("/", headers={"authorization": "basic {user}".format(user=b64encode(b"test_user:test_password"))}) assert res.status_code == 200
Comments
Post a Comment