Skip to content
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

graphene_django.utils.testing.graphql_query and Django 4.2+ #1501

Open
hokkaido opened this issue Feb 29, 2024 · 1 comment
Open

graphene_django.utils.testing.graphql_query and Django 4.2+ #1501

hokkaido opened this issue Feb 29, 2024 · 1 comment
Labels

Comments

@hokkaido
Copy link

hokkaido commented Feb 29, 2024

What is the current behavior?
This is with graphene-django==3.2.0 and Django==4.2.10 and it concerns the graphql_query function from graphene_django.utils.testing. Django 4.2.+ prefixes the header keys automatically with HTTP_, making the documentation of the graphql_query in that situation wrong.

Pseudo Code:

    headers = {
        'HTTP_AUTHORIZATION': f'Bearer {JWT_TOKEN}'
    }
    return graphql_query(.., .., client=client, headers=headers, graphql_url="/graphql/")

If we then later dump request.META we will see HTTP_HTTP_AUTHORIZATION.

The part of graphene where it handles the Django version can be found here:

{"headers": headers} if _DJANGO_VERSION_AT_LEAST_4_2 else headers

Further context
You see Django's behavior described here in an example: https://docs.djangoproject.com/en/4.2/topics/testing/tools/#django.test.Client.get

c = Client()
c.get(
... "/customers/details/",
... {"name": "fred", "age": 7},
... headers={"accept": "application/json"},
... )
…will send the HTTP header HTTP_ACCEPT to the details view, which is a good way to test code paths that use the django.http.HttpRequest.accepts() method.

Fix
So I suggest at a minimum to update the documentation / description of client_query that with Django 4.2+ headers don't have to be prefixed with HTTP_. Ideally the function it self would take care of it however.

headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
will be set to this value. Keys should be prepended with
"HTTP_" (e.g. to specify the "Authorization" HTTP header,
use "HTTP_AUTHORIZATION" as the key).

@kiendang
Copy link
Collaborator

Ah ok this is valid. I didn't read the Django doc carefully when doing #1465. I think the correct solution to this is to revert #1465 and add a check making sure that headers keys all start with HTTP_. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants