-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from tartiflette/bubu/add_tests_to_header_feature
feat(response_headers): Add the ability to modify response headers using contextvars
- Loading branch information
Showing
7 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# [1.2.0] - 2020-01-29 | ||
|
||
## Added | ||
|
||
- Possibility to set header to the response directly from the resolver using `set_response_headers` method. | ||
|
||
```python | ||
from tartiflette_aiohttp import set_response_headers | ||
|
||
@Resolver("Query.X") | ||
async def resolver_x(parent_result, args, ctx, info): | ||
# do things | ||
set_response_headers({"Header": "Value", "AnotherHeader": "AnotherValue"}) | ||
return result | ||
``` | ||
|
||
> Note that this feature uses ContextVar and will only works for python 3.7.1 and later. | ||
## Changed | ||
|
||
- pytest updated to 5.3.4 | ||
- pylint updated to 2.4.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
_RESPONSE_HEADERS_VAR = None | ||
try: | ||
import contextvars | ||
|
||
_RESPONSE_HEADERS_VAR = contextvars.ContextVar( | ||
"response_headers", default={} | ||
) | ||
except ImportError: | ||
pass | ||
|
||
|
||
def set_response_headers(headers: dict): | ||
if _RESPONSE_HEADERS_VAR is not None: | ||
_RESPONSE_HEADERS_VAR.get().update(headers) | ||
else: | ||
print( | ||
"This feature < set_response_headers > only works with python 3.7" | ||
) | ||
|
||
|
||
def get_response_headers(): | ||
if _RESPONSE_HEADERS_VAR is not None: | ||
return _RESPONSE_HEADERS_VAR.get() | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import contextvars | ||
import json | ||
|
||
import pytest | ||
|
||
from aiohttp import web | ||
|
||
from tartiflette import Engine, Resolver, create_engine | ||
from tartiflette_aiohttp import register_graphql_handlers, set_response_headers | ||
|
||
|
||
async def test_header_context_var(aiohttp_client, loop): | ||
@Resolver("Query.bob", schema_name="test_header_context_var") | ||
async def resolver_lol(*args, **kwargs): | ||
set_response_headers({"X-Test": "Lol", "Z-Test": "Ninja"}) | ||
return {"name": "a", "surname": "b"} | ||
|
||
class myEngine(Engine): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
async def cook(self, *args, **kwargs): | ||
await super().cook(*args, **kwargs) | ||
|
||
app = register_graphql_handlers( | ||
app=web.Application(), | ||
engine=myEngine(), | ||
engine_sdl="""type Query { bob: Ninja } type Ninja { name: String surname: String }""", | ||
engine_schema_name="test_header_context_var", | ||
) | ||
|
||
client = await aiohttp_client(app) | ||
resp = await client.post( | ||
"/graphql", | ||
data=json.dumps({"query": "query lol { bob { name surname } }"}), | ||
headers={"content-type": "application/json"}, | ||
) | ||
assert resp.status == 200 | ||
result = await resp.json() | ||
assert result == {"data": {"bob": {"name": "a", "surname": "b"}}} | ||
assert resp.headers["X-Test"] == "Lol" | ||
assert resp.headers["Z-Test"] == "Ninja" |