Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#31550 closed Cleanup/optimization (fixed)

AssertionError raised by test_file_response method of ASGITest class.

Reported by: Yash Saini Owned by: chrisxkeith
Component: HTTP handling Version: 3.0
Severity: Normal Keywords: ASGI tests
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

On running:
python runtests.py asgi
The following assertion is raised in tests\asgi\tests.py in ASGITest.test_file_response method:

    AssertionError: Items in the first set but not the second:
    (b'Content-Type', b'text/x-python')
    Items in the second set but not the first:
    (b'Content-Type', b'text/plain')

Change History (11)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedHTTP handling
Resolution: needsinfo
Status: newclosed
Type: UncategorizedCleanup/optimization

Content-type depends on a platform. Jenkins doesn't report this issue on Windows and Linux. What platform and Python version are you using?

comment:2 by Yash Saini, 4 years ago

I am using Windows 10 and python 3.7.7.

comment:3 by Carlton Gibson, 4 years ago

The failure doesn't reproduce for me on Windows with Python 3.7 or 3.8.

comment:4 by Addison Snelling, 4 years ago

Resolution: needsinfo
Status: closednew

I think the test is incorrect in expecting text/plain instead of text/x-python as the content type for a Python file

test_file_response in tests/asgi/tests.py fails for me as well.

(.venv) PS D:\source\django\tests> py runtests.py asgi.tests
...
======================================================================
FAIL: test_file_response (asgi.tests.ASGITest)
Makes sure that FileResponse works over ASGI.
----------------------------------------------------------------------
...
AssertionError: Items in the first set but not the second:
(b'Content-Type', b'text/x-python')
Items in the second set but not the first:
(b'Content-Type', b'text/plain')

My environment (Windows 10 Pro Build 20211):

>>> sys.platform
'win32'
>>> sys.getwindowsversion()
sys.getwindowsversion(major=10, minor=0, build=20211, platform=2, service_pack='')
>>> sys.version
'3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]'

The set_headers method of django.http.FileResponse queries the content type of the file on line 467:

content_type, encoding = mimetypes.guess_type(filename)

On my machine, the above line sets content_type to text/x-python, but line 79 in the test file expects text/plain on Windows platforms:

self.assertEqual(
    set(response_start['headers']),
    {
        (b'Content-Length', str(len(test_file_contents)).encode('ascii')),
        (b'Content-Type', b'text/plain' if sys.platform == 'win32' else b'text/x-python'),  # line 79
        (b'Content-Disposition', b'inline; filename="urls.py"'),
    },
)

The test method is incorrect, but why doesn't this fail on Jenkins? Is Jenkins running the tests on Windows 10 or an older version? The Python documentation for mimetypes.guess_type doesn't shed much light, specifically on whether the function returns text/plain for python files on Windows platforms (perhaps previous versions).

comment:5 by Carlton Gibson, 4 years ago

Resolution: needsinfo
Status: newclosed

Hi Addison. This works for me. #32041 came up recently, which is related.

If you can dig-in and come up with some more information we can look into it but short of a concrete analysis I'm not sure what we can do. What action are we to take? (The line in the tests is as it is because this was the behaviour we hit developing the feature…)

comment:6 by Carlton Gibson, 4 years ago

Has patch: set
Resolution: needsinfo
Status: closednew
Triage Stage: UnreviewedAccepted

comment:7 by Carlton Gibson, 4 years ago

Owner: changed from nobody to chrisxkeith
Patch needs improvement: set
Status: newassigned

comment:8 by Carlton Gibson, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset

comment:10 by GitHub <noreply@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 7618130:

Fixed #31550 -- Adjusted ASGI test_file_response for various Windows content types.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 5dec57a6:

[3.1.x] Fixed #31550 -- Adjusted ASGI test_file_response for various Windows content types.

Backport of 76181308fb02e67794d0cc1471766a5d7e4c877e from master

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