Opened 4 years ago

Last modified 5 days ago

#32567 assigned Bug

Issues with ":" and "|" characters in URLs when using LiveServerTestCase on Windows — at Initial Version

Reported by: Tim G Owned by: nobody
Component: Testing framework Version: 3.1
Severity: Normal Keywords: windows, liveservertestcase, path
Cc: Antoliny Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I am using Python 3.8 32-bit on Windows: Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32

I installed Django and requests in a virtual environment:
pip install django requests

My virtual environment now has:

asgiref==3.3.1
certifi==2020.12.5
chardet==4.0.0
Django==3.1.7
idna==2.10
pytz==2021.1
requests==2.25.1
sqlparse==0.4.1
urllib3==1.26.4

I created a new Django project:

django-admin.exe startproject example
cd example
django-admin.exe startapp livecase

I used the following code in my example/livecase/views.py:

from django.http import HttpResponse

def example(request):
    return HttpResponse("OK")

And the following in example/example/urls.py:

from django.contrib import admin
from django.urls import path
from livecase.views import example

urlpatterns = [
    path('test:12345/', example),
    path('admin/', admin.site.urls),
]

I then used the following code in example/livecase/tests.py:

from django.test import LiveServerTestCase
import requests

class MyTest(LiveServerTestCase):
    def test_example(self):
        r = requests.get(f"{self.live_server_url}/test:12345/")
        self.assertEqual(r.status_code, 200)

I run the tests: python manage.py test

I get the following error, with the traceback:

Traceback (most recent call last):
  File "C:\[redacted]\AppData\Local\Programs\Python\Python38-32\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "C:\[redacted]\venv-test\lib\site-packages\django\test\testcases.py", line 1324, in __call__
    return self.application(environ, start_response)
  File "C:\[redacted]\venv-test\lib\site-packages\django\test\testcases.py", line 1325, in __call__
    return super().__call__(environ, start_response)
  File "C:\[redacted]\venv-test\lib\site-packages\django\core\handlers\wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "C:\[redacted]\venv-test\lib\site-packages\django\test\testcases.py", line 1305, in get_response
    return self.serve(request)
  File "C:\[redacted]\venv-test\lib\site-packages\django\test\testcases.py", line 1320, in serve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "C:\[redacted]\venv-test\lib\site-packages\django\views\static.py", line 38, in serve
    fullpath = Path(safe_join(document_root, path))
  File "C:\[redacted]\venv-test\lib\site-packages\django\utils\_os.py", line 29, in safe_join
    raise SuspiciousFileOperation(
django.core.exceptions.SuspiciousFileOperation: The joined path (T:\12345) is located outside of the base path component (C:\[redacted]\example)
F
======================================================================
FAIL: test_example (livecase.tests.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\[redacted]\example\livecase\tests.py", line 9, in test_example
    self.assertEqual(r.status_code, 200)
AssertionError: 500 != 200

----------------------------------------------------------------------
Ran 1 test in 0.528s

If I use "|" instead of ":" in the URL in the test case, the same problem happens.
If I use self.client.get, the test passes and no exception is raised.

I tried to replicate the issue in a Linux environment, and I was not able to (although using Python 3.8 64 bit).

I believe the path conversion issue is happening at https://github.com/django/django/blob/45814af6197cfd8f4dc72ee43b90ecde305a1d5a/django/test/testcases.py#L1439

Thanks.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top