Skip to content

Commit 5ad239d

Browse files
committed
Merge pull request #30 from vitalk/request-ctx-fixture
Request context fixture. Closes #29.
2 parents 3fa3652 + f86465a commit 5ad239d

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

docs/features.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ An instance of ``app.test_client``. Typically refers to
8484

8585
.. hint::
8686

87-
During tests execution the application has pushed context, e.g.
87+
During tests execution the request context has been pushed, e.g.
8888
``url_for``, ``session`` and other context bound objects are available
8989
without context managers.
9090

@@ -139,6 +139,26 @@ other headless browsers).
139139
assert res.code == 200
140140
141141
142+
``request_ctx`` - request context
143+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144+
145+
The request context which contains all request relevant information.
146+
147+
.. hint::
148+
149+
The request context has been pushed implicitly any time the ``app``
150+
fixture is applied and is kept around during test execution, so it's easy
151+
to introspect the data:
152+
153+
.. code:: python
154+
155+
from flask import request, url_for
156+
157+
def test_request_headers(client):
158+
res = client.get(url_for('ping'), headers=[('X-Something', '42')])
159+
assert request.headers['X-Something'] == '42'
160+
161+
142162
Content negotiation
143163
~~~~~~~~~~~~~~~~~~~
144164

pytest_flask/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
__version__ = "0.7.5"
4+
__version__ = "0.8.0"

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: 3 additions & 3 deletions
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

@@ -64,8 +64,8 @@ def test_json(client):
6464

6565

6666
@pytest.fixture(autouse=True)
67-
def _push_application_context(request):
68-
"""During tests execution application has pushed context, e.g. `url_for`,
67+
def _push_request_context(request):
68+
"""During tests execution request context has been pushed, e.g. `url_for`,
6969
`session`, etc. can be used in tests as is::
7070
7171
def test_app(app, client):

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- ``config`` - you application config,
1818
- ``live_server`` - runs an application in the background (useful for tests
1919
with `Selenium <http://www.seleniumhq.org>`_ and other headless browsers),
20+
- ``request_ctx`` - the request context,
2021
- ``accept_json``, ``accept_jsonp``, ``accept_any`` - accept headers
2122
suitable to use as parameters in ``client``.
2223
@@ -28,7 +29,7 @@
2829
def test_app(app):
2930
assert not app.debug, 'Ensure the app not in debug mode'
3031
31-
During tests execution the application has pushed context, e.g. ``url_for``,
32+
During tests execution the request context has been pushed, e.g. ``url_for``,
3233
``session`` and other context bound objects are available without context
3334
managers:
3435

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)