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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32567>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim G):
To illustrate the problem better, I added one (failing) test in the test
suite:
https://github.com/timoguic/django/commits/ticket_32567/tests/staticfiles_tests/test_liveserver.py
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:1>
Old description:
New description:
Thanks.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:2>
Comment (by Carlton Gibson):
Hey Tim, nice report thanks.
I didn't have the time to assess yet, but a couple of questions:
1. Did you example `request.path` there — what's it look like? What about
`request.META`?
2. Does it happen with `runserver` hitting the URL by hand?
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:3>
* stage: Unreviewed => Accepted
Comment:
Reproduced on Windows 10 (64bit) with the test case provided.
Tim do you fancy opening a PR with the test at least, so that it's easier
to work with?
Thanks again for the report. Really very clear.
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:4>
Comment (by Tim G):
Replying to [comment:4 Carlton Gibson]:
> Tim do you fancy opening a PR with the test at least, so that it's
easier to work with?
Sure. I just did that.
I think I found a fix, but I am not sure if it's the right approach. Shall
I commit in the PR still?
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:5>
Comment (by Carlton Gibson):
Hey Tim. Yes, do. It’ll highlight what’s going on. We can assess it in
review.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:6>
Comment (by Tim G):
WIP is at https://github.com/django/django/pull/14146
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:7>
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:8>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:9>
* owner: nobody => Tim G
* needs_better_patch: 0 => 1
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32567#comment:10>