-
Notifications
You must be signed in to change notification settings - Fork 21
added support in requests.Session #58
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
Open
uriyay
wants to merge
3
commits into
prodigyeducation:master
Choose a base branch
from
uriyay:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -145,6 +145,120 @@ def test_execute_query_with_operation_name(self, post_mock): | |
) | ||
|
||
|
||
class TestGraphqlClientExecuteSession(TestCase): | ||
"""Test cases for synchronous graphql request function using a Session object.""" | ||
|
||
def test_execute_basic_query(self): | ||
"""Sends a graphql POST request to an endpoint.""" | ||
session_mock = MagicMock() | ||
client = GraphqlClient(endpoint="http://www.test-api.com/") | ||
query = """ | ||
{ | ||
tests { | ||
status | ||
} | ||
} | ||
""" | ||
client.execute(query, session=session_mock) | ||
|
||
session_mock.post.assert_called_once_with( | ||
"http://www.test-api.com/", json={"query": query}, headers={} | ||
) | ||
|
||
def test_execute_query_with_variables(self): | ||
"""Sends a graphql POST request with variables.""" | ||
session_mock = MagicMock() | ||
client = GraphqlClient(endpoint="http://www.test-api.com/") | ||
query = "" | ||
variables = {"id": 123} | ||
client.execute(query, variables, session=session_mock) | ||
|
||
session_mock.post.assert_called_once_with( | ||
"http://www.test-api.com/", | ||
json={"query": query, "variables": variables}, | ||
headers={}, | ||
) | ||
|
||
def test_raises_http_errors_as_exceptions(self): | ||
"""Raises an exception if an http error code is returned in the response.""" | ||
session_mock = MagicMock() | ||
response_mock = MagicMock() | ||
response_mock.raise_for_status.side_effect = HTTPError() | ||
session_mock.post.return_value = response_mock | ||
|
||
client = GraphqlClient(endpoint="http://www.test-api.com/") | ||
|
||
with self.assertRaises(HTTPError): | ||
client.execute(query="", session=session_mock) | ||
|
||
def test_execute_query_with_headers(self): | ||
"""Sends a graphql POST request with headers.""" | ||
session_mock = MagicMock() | ||
client = GraphqlClient( | ||
endpoint="http://www.test-api.com/", | ||
headers={"Content-Type": "application/json", "Existing": "123"}, | ||
) | ||
query = "" | ||
client.execute(query=query, | ||
headers={"Existing": "456", "New": "foo"}, | ||
session=session_mock) | ||
uriyay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
session_mock.post.assert_called_once_with( | ||
"http://www.test-api.com/", | ||
json={"query": query}, | ||
headers={ | ||
"Content-Type": "application/json", | ||
"Existing": "456", | ||
"New": "foo", | ||
}, | ||
) | ||
|
||
def test_execute_query_with_options(self): | ||
"""Sends a graphql POST request with headers.""" | ||
session_mock = MagicMock() | ||
auth = HTTPBasicAuth("[email protected]", "not_a_real_password") | ||
client = GraphqlClient( | ||
endpoint="http://www.test-api.com/", | ||
auth=auth, | ||
) | ||
query = "" | ||
client.execute(query=query, verify=False, session=session_mock) | ||
|
||
session_mock.post.assert_called_once_with( | ||
"http://www.test-api.com/", | ||
json={"query": query}, | ||
headers={}, | ||
auth=HTTPBasicAuth("[email protected]", "not_a_real_password"), | ||
verify=False, | ||
) | ||
|
||
def test_execute_query_with_operation_name(self): | ||
"""Sends a graphql POST request with the operationName key set.""" | ||
session_mock = MagicMock() | ||
client = GraphqlClient(endpoint="http://www.test-api.com/") | ||
query = """ | ||
query firstQuery { | ||
test { | ||
status | ||
} | ||
} | ||
|
||
query secondQuery { | ||
test { | ||
status | ||
} | ||
} | ||
""" | ||
operation_name = "firstQuery" | ||
client.execute(query, operation_name=operation_name, session=session_mock) | ||
|
||
session_mock.post.assert_called_once_with( | ||
"http://www.test-api.com/", | ||
json={"query": query, "operationName": operation_name}, | ||
headers={}, | ||
) | ||
|
||
|
||
class TestGraphqlClientExecuteAsync(IsolatedAsyncioTestCase): | ||
"""Test cases for the asynchronous graphQL request function.""" | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should validate the type that session is a Session to prevent injection.