Skip to content

Commit 48633f7

Browse files
committed
Add request_ctx fixture to access request context
1 parent 3fa3652 commit 48633f7

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

pytest_flask/fixtures.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
except ImportError:
1111
from urllib.request import urlopen
1212

13+
from flask import _request_ctx_stack
14+
1315

1416
@pytest.yield_fixture
1517
def client(app):
@@ -132,6 +134,14 @@ def config(app):
132134
return app.config
133135

134136

137+
@pytest.fixture
138+
def request_ctx(app):
139+
"""The request context which contains all request relevant information,
140+
e.g. `session`, `g`, `flashes`, etc.
141+
"""
142+
return _request_ctx_stack.top
143+
144+
135145
@pytest.fixture(params=['application/json', 'text/html'])
136146
def mimetype(request):
137147
return request.param

pytest_flask/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from .fixtures import (
1515
client, config, accept_json, accept_jsonp, accept_any, accept_mimetype,
16-
client_class, live_server
16+
client_class, live_server, request_ctx
1717
)
1818

1919

tests/test_fixtures.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
# -*- coding: utf-8 -*-
33
import pytest
44

5-
from flask import url_for
5+
from flask import request, url_for
66

77

88
class TestFixtures:
99

1010
def test_config_access(self, config):
1111
assert config['SECRET_KEY'] == '42'
1212

13-
def test_application_has_pushed_context(self, app):
14-
assert url_for('ping') == '/ping'
15-
1613
def test_client(self, client):
1714
assert client.get(url_for('ping')).status == b'200 OK'
1815

@@ -22,6 +19,13 @@ def test_accept_json(self, accept_json):
2219
def test_accept_jsonp(self, accept_jsonp):
2320
assert accept_jsonp == [('Accept', 'application/json-p')]
2421

22+
def test_request_ctx(self, app, request_ctx):
23+
assert request_ctx.app is app
24+
25+
def test_request_ctx_is_kept_around(self, client):
26+
res = client.get(url_for('index'), headers=[('X-Something', '42')])
27+
assert request.headers['X-Something'] == '42'
28+
2529

2630
class TestJSONResponse:
2731

0 commit comments

Comments
 (0)