Fix test Client headers for Django 4.2

This commit is contained in:
Kien Dang 2023-09-18 17:53:04 +08:00
parent 83d3d27f14
commit 433a7d8bcb
2 changed files with 20 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import warnings
from django.test import Client, TestCase, TransactionTestCase
from graphene_django.settings import graphene_settings
from graphene_django.utils.utils import _DJANGO_VERSION_AT_LEAST_4_2
DEFAULT_GRAPHQL_URL = "/graphql"
@ -55,8 +56,14 @@ def graphql_query(
else:
body["variables"] = {"input": input_data}
if headers:
header_params = (
{"headers": headers} if _DJANGO_VERSION_AT_LEAST_4_2 else headers
)
resp = client.post(
graphql_url, json.dumps(body), content_type="application/json", **headers
graphql_url,
json.dumps(body),
content_type="application/json",
**header_params
)
else:
resp = client.post(

View File

@ -1,5 +1,6 @@
import inspect
import pkg_resources
from django.db import connection, models, transaction
from django.db.models.manager import Manager
from django.utils.encoding import force_str
@ -145,3 +146,14 @@ def bypass_get_queryset(resolver):
"""
resolver._bypass_get_queryset = True
return resolver
def __django_version():
return pkg_resources.get_distribution("django").parsed_version
def __parse_version(v):
return pkg_resources.parse_version(v)
_DJANGO_VERSION_AT_LEAST_4_2 = __django_version() >= __parse_version("4.2")