<commit_before>from unittest import mock
import pytest

from django.http.request import HttpHeaders
from http import HTTPStatus
from ninja.testing import TestClient
from ninja import Router

router = Router()

@router.get("/request/build_absolute_uri")
def request_build_absolute_uri(request):
    return request.build_absolute_uri()


@router.get("/request/build_absolute_uri/location")
def request_build_absolute_uri_location(request):
    return request.build_absolute_uri('location')


@router.get("/test")
def simple_get(request):
    return 'test'


client = TestClient(router)


@pytest.mark.parametrize('path,expected_status,expected_response', [
    ('/request/build_absolute_uri', HTTPStatus.OK, 'http://testlocation/'),
    ('/request/build_absolute_uri/location', HTTPStatus.OK, 'http://testlocation/location'),
])
def test_sync_build_absolute_uri(path, expected_status, expected_response):
    response = client.get(path)

    assert response.status_code == expected_status
    assert response.json() == expected_response


@pytest.mark.parametrize('version, has_headers', [
    ((2, 0), False),
    ((2, 1), False),
    ((2, 2), True),
    ((3, 0), True),
])
def test_django_2_2_plus_headers(version, has_headers):
    with mock.patch('ninja.testing.client.django', VERSION=version):
        with mock.patch.object(client, '_call') as call:
            client.get('/test')
            request = call.call_args[0][1]
            assert isinstance(request.headers, HttpHeaders) == has_headers<commit_msg>Fix tests for Django < 2.2<commit_after>from unittest import mock
import pytest

from http import HTTPStatus
from ninja.testing import TestClient
from ninja import Router

router = Router()

@router.get("/request/build_absolute_uri")
def request_build_absolute_uri(request):
    return request.build_absolute_uri()


@router.get("/request/build_absolute_uri/location")
def request_build_absolute_uri_location(request):
    return request.build_absolute_uri('location')


@router.get("/test")
def simple_get(request):
    return 'test'


client = TestClient(router)


@pytest.mark.parametrize('path,expected_status,expected_response', [
    ('/request/build_absolute_uri', HTTPStatus.OK, 'http://testlocation/'),
    ('/request/build_absolute_uri/location', HTTPStatus.OK, 'http://testlocation/location'),
])
def test_sync_build_absolute_uri(path, expected_status, expected_response):
    response = client.get(path)

    assert response.status_code == expected_status
    assert response.json() == expected_response


@pytest.mark.parametrize('version, has_headers', [
    ((2, 0), False),
    ((2, 1), False),
    ((2, 2), True),
    ((3, 0), True),
])
def test_django_2_2_plus_headers(version, has_headers):
    with mock.patch('ninja.testing.client.django', VERSION=version):
        with mock.patch.object(client, '_call') as call:
            client.get('/test')
            request = call.call_args[0][1]
            # for Django >= 2.2 we apply a HttpHeaders instance to .headers
            assert isinstance(request.headers, mock.Mock) != has_headers
{'fraction_non_alphanumeric': 0.13008130081300814, 'fraction_numerical': 0.009105691056910569, 'mean_word_length': 4.696296296296296, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4007503', 'n_tokens_mistral': 957, 'n_tokens_neox': 934, 'n_words': 193}