From e8f36b018db2db9b8fba980b5a2b2a680f851e16 Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Mon, 18 Sep 2023 22:23:53 +0700 Subject: [PATCH] Fix test Client headers for Django 4.2 (#1465) * Fix test Client headers for Django 4.2 * Lazy import pkg_resources since it could be quite heavy * Remove use of pkg_resources altogether --- graphene_django/utils/testing.py | 9 ++++++++- graphene_django/utils/utils.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index ad9ff35..6cd0e3b 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -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( diff --git a/graphene_django/utils/utils.py b/graphene_django/utils/utils.py index d7993e7..364eff9 100644 --- a/graphene_django/utils/utils.py +++ b/graphene_django/utils/utils.py @@ -1,5 +1,6 @@ import inspect +import django from django.db import connection, models, transaction from django.db.models.manager import Manager from django.utils.encoding import force_str @@ -145,3 +146,8 @@ def bypass_get_queryset(resolver): """ resolver._bypass_get_queryset = True return resolver + + +_DJANGO_VERSION_AT_LEAST_4_2 = django.VERSION[0] > 4 or ( + django.VERSION[0] >= 4 and django.VERSION[1] >= 2 +)