Skip to content

Commit 8257777

Browse files
authored
Merge pull request #9 from anibalsolon/bug/csrf_validation
Fetch CSRF cookie to validate w/ Graphene API
2 parents a2a7f66 + f603852 commit 8257777

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

gql/transport/http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class HTTPTransport(object):
22

3-
def __init__(self, url, headers=None):
3+
def __init__(self, url, headers=None, cookies=None):
44
self.url = url
55
self.headers = headers
6+
self.cookies = cookies

gql/transport/requests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def execute(self, document, variable_values=None, timeout=None):
3131
post_args = {
3232
'headers': self.headers,
3333
'auth': self.auth,
34+
'cookies': self.cookies,
3435
'timeout': timeout or self.default_timeout,
3536
data_key: payload
3637
}

tests/test_transport.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import pytest
2+
import requests
23

34
from gql import Client, gql
45
from gql.transport.requests import RequestsHTTPTransport
56

67

78
@pytest.fixture
89
def client():
10+
request = requests.get('http://swapi.graphene-python.org/graphql',
11+
headers={
12+
'Host': 'swapi.graphene-python.org',
13+
'Accept': 'text/html',
14+
})
15+
request.raise_for_status()
16+
csrf = request.cookies['csrftoken']
17+
918
return Client(
10-
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql'),
11-
fetch_schema_from_transport=True
19+
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql',
20+
cookies={"csrftoken": csrf},
21+
headers={'x-csrftoken': csrf}),
22+
fetch_schema_from_transport=True
1223
)
1324

1425

0 commit comments

Comments
 (0)