Skip to content

Fetch CSRF cookie to validate w/ Graphene API #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gql/transport/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class HTTPTransport(object):

def __init__(self, url, headers=None):
def __init__(self, url, headers=None, cookies=None):
self.url = url
self.headers = headers
self.cookies = cookies
1 change: 1 addition & 0 deletions gql/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def execute(self, document, variable_values=None, timeout=None):
post_args = {
'headers': self.headers,
'auth': self.auth,
'cookies': self.cookies,
'timeout': timeout or self.default_timeout,
data_key: payload
}
Expand Down
15 changes: 13 additions & 2 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import pytest
import requests

from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport


@pytest.fixture
def client():
request = requests.get('http://swapi.graphene-python.org/graphql',
headers={
'Host': 'swapi.graphene-python.org',
'Accept': 'text/html',
})
request.raise_for_status()
csrf = request.cookies['csrftoken']

return Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql'),
fetch_schema_from_transport=True
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql',
cookies={"csrftoken": csrf},
headers={'x-csrftoken': csrf}),
fetch_schema_from_transport=True
)


Expand Down