[Django] #26668: HTTPResponse.content is not unicode in context of test cases.

5 views
Skip to first unread message

Django

unread,
May 26, 2016, 5:43:24 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------
Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Came across the following when adding `from __future__ import
unicode_literals` to my test.py-s

When doing the following for an existing view with an expected output:

{{{
def test_page_not_found(self):
url = '/help/404/'
c = self.get_client()
response = c.get(url)
self.assertIn("Page Not Found", response.content)
}}}

We get:

{{{
======================================================================
ERROR: test_page_not_found (xsd_help.tests.HelpViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/builds/wjdp/xsacdb/src/xsd_help/tests.py", line 23, in
test_page_not_found
self.assertTrue("Page Not Found" in response.content)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
21370: ordinal not in range(128)
}}}

Workaround is to replace `response.content` with
`unicode(response.content, encoding=settings.DEFAULT_ENCODING)`. According
to what I could find in the docs the content should be encoded with uft-8,
I think this issue is because `request.content` is a bytestring without an
encoding attached.

I'm raising this either in case this in not expected or if it is
suggesting a more descriptive error is generated and/or better
documentation for those who come across this - there was a *lot* of
headscratching needed to find the root of these test failures.

Another suggestion: could we have another property of HTTPResponse that
gives us a unicode string rather than a bytestring for test cases?

See http://stackoverflow.com/questions/36004324/function-assertin-causes-
the-unicodedecodeerror/37456287 for another similar use case.

--
Ticket URL: <https://code.djangoproject.com/ticket/26668>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 26, 2016, 5:44:32 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------

Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by wjdp):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

Came across the following when adding `from __future__ import
unicode_literals` to my test.py-s

When doing the following for an existing view with an expected output:

{{{
def test_page_not_found(self):
url = '/help/404/'
c = self.get_client()
response = c.get(url)
self.assertIn("Page Not Found", response.content)
}}}

We get:

{{{
======================================================================
ERROR: test_page_not_found (xsd_help.tests.HelpViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/builds/wjdp/xsacdb/src/xsd_help/tests.py", line 23, in
test_page_not_found
self.assertTrue("Page Not Found" in response.content)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
21370: ordinal not in range(128)
}}}

Workaround is to replace `response.content` with

`unicode(response.content, encoding=settings.DEFAULT_CHARSET)`. According


to what I could find in the docs the content should be encoded with uft-8,
I think this issue is because `request.content` is a bytestring without an
encoding attached.

I'm raising this either in case this in not expected or if it is
suggesting a more descriptive error is generated and/or better
documentation for those who come across this - there was a *lot* of
headscratching needed to find the root of these test failures.

Another suggestion: could we have another property of HTTPResponse that
gives us a unicode string rather than a bytestring for test cases?

See http://stackoverflow.com/questions/36004324/function-assertin-causes-
the-unicodedecodeerror/37456287 for another similar use case.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:1>

Django

unread,
May 26, 2016, 7:04:11 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------

Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by charettes):

The `SimpleTestCase` class already provides the
[https://docs.djangoproject.com/en/1.9/topics/testing/tools/#django.test.SimpleTestCase.assertContains
assertContains method] for this use case.

I'm afraid there's not much we can do to make the error message more
descriptive as that's standard Python stuff.

--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:2>

Django

unread,
May 26, 2016, 7:08:49 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------

Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by charettes):

I also noticed that the
[https://docs.djangoproject.com/en/1.9/topics/python3/#httprequest-and-
httpresponse-objects Porting to Python 3 guide] mentions why
`assertContains` should be used instead.

Is this the reason you added the `from __future__ import unicode_literals`
import in the first place?

--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:3>

Django

unread,
May 26, 2016, 9:42:21 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------

Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by wjdp):

Ah, must have glossed over that, `assertContains` seems to fix all of
this. `unicode_literals` was unrelated I think, just fixing up use of
literals.

--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:4>

Django

unread,
May 26, 2016, 9:43:14 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------

Reporter: wjdp | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by wjdp):

Feel free to mark as invalid.

--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:5>

Django

unread,
May 26, 2016, 9:48:36 AM5/26/16
to django-...@googlegroups.com
#26668: HTTPResponse.content is not unicode in context of test cases.
-------------------------------+--------------------------------------
Reporter: wjdp | Owner: nobody
Type: Bug | Status: closed

Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by charettes):

* status: new => closed
* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/26668#comment:6>

Reply all
Reply to author
Forward
0 new messages