[Django] #35727: Add `response.text_content` to responses

25 views
Skip to first unread message

Django

unread,
Sep 2, 2024, 9:36:37 AM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
------------------------------+-----------------------------------------
Reporter: Adam Johnson | Type: New feature
Status: new | Component: HTTP handling
Version: dev | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+-----------------------------------------
A common pattern in tests is to make assertions on the response content.
[https://docs.djangoproject.com/en/stable/ref/request-
response/#django.http.HttpResponse.content HttpResponse.content] is
`bytes`, requiring a call to `.decode()` for assertions with `str` values:

{{{
class DiggerLogTests(TestCase):
def test_get(self):
response = self.client.get('/digger/logs')
assert 'Good digging' in response.content.decode()
assert '2022-01-01 12:00:00' in response.content.decode()
}}}

This is suboptimal for a few reasons:

* It requires extra code.
* Mixed bytes/str errors may confuse beginners.
* `decode()` imparts a small but measurable overhead, especially if
repeated for each assertion as above.
* `.decode()` defaults to UTF-8. Whilst this is by far the most common
encoding, the response may actually use a different encoding, as per
[https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
the content-type header].

I propose that responses from the test client include a new attribute,
called `text_content`. This would be cached property returning `content`
decoded into a `str`, per any `charset` in `content-type`.

This new attribute would allow the above test to be written as:

{{{
class DiggerLogTests(TestCase):
def test_get(self):
response = self.client.get('/digger/logs')
assert 'Good digging' in response.text
assert '2022-01-01 12:00:00' in response.text
}}}

`text_content` could be added either in `HttpResponse` directly or
attached by the test client, like `response.json()`. I favour the first
option a little more as it may come in useful outside of tests, and the
cache invalidation when `content` is mutated would be easier.

The new attribute could also be used to simplify `assertContains`,
[https://github.com/django/django/blob/387475c5b2f1aa32103dbe21cb281d3b35165a0c/django/test/testcases.py#L586
which decodes on every call]

I have wanted this for a while and Simon Willison suggested this feature
[https://x.com/simonw/status/1728104536898957425 to me on Twitter].
--
Ticket URL: <https://code.djangoproject.com/ticket/35727>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 2, 2024, 12:37:40 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-----------------------------------+------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Natalia Bidart):

* component: HTTP handling => Testing framework
* stage: Unreviewed => Accepted

Comment:

I'm a big +1 for this one, I have suffered this multiple times in many
projects.

One note though: the issue title and content proposes `text_content` for
the new attr name, but the example uses `text`. I personally prefer `text`
because it's shorter and the same that `requests`
[https://requests.readthedocs.io/en/latest/user/quickstart/#response-
content exposes].
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:1>

Django

unread,
Sep 2, 2024, 1:27:47 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-----------------------------------+------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Comment (by Adam Johnson):

Ah yeah, I went back and forth on that when writing the ticket. Okay,
let’s do `text`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:2>

Django

unread,
Sep 2, 2024, 5:08:23 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-----------------------------------+------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Comment (by Adam Johnson):

Natalia, do you have any opinion on adding in `HttpResponse` versus
patching in the test client?
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:3>

Django

unread,
Sep 2, 2024, 5:35:05 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Natalia Bidart):

* component: Testing framework => HTTP handling

Comment:

Thanks for asking Adam! I missed that question/inquiry from the original
description. I see now why you chose "HTTP handling" as Component :-).

I agree with your sentiment to make this a new feature/attribute for
`HttpResponse`, so I'll be switching the component back.
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:4>

Django

unread,
Sep 2, 2024, 10:08:08 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* owner: (none) => Jae Hyuck Sa
* status: new => assigned

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

Django

unread,
Sep 2, 2024, 11:50:08 PM9/2/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* has_patch: 0 => 1

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

Django

unread,
Sep 4, 2024, 9:45:35 PM9/4/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:7>

Django

unread,
Sep 9, 2024, 9:33:47 AM9/9/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:8>

Django

unread,
Sep 10, 2024, 8:10:08 AM9/10/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jacob Walls):

* needs_docs: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:9>

Django

unread,
Sep 10, 2024, 9:07:08 AM9/10/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* needs_docs: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:10>

Django

unread,
Sep 11, 2024, 4:09:25 AM9/11/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:11>

Django

unread,
Sep 11, 2024, 8:26:33 AM9/11/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:12>

Django

unread,
Sep 11, 2024, 9:15:08 AM9/11/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
| Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:13>

Django

unread,
Sep 11, 2024, 9:35:46 AM9/11/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Sarah Boyce):

* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:14>

Django

unread,
Sep 11, 2024, 10:09:37 AM9/11/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:15>

Django

unread,
Sep 27, 2024, 12:06:08 PM9/27/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Comment (by Jae Hyuck Sa ):

Hi! Sarah.

I’ve noticed that content.decode() is being used in many places within our
current test code.
Do you think it would be necessary to make any modifications to this?

Example
-
https://github.com/django/django/blob/5ed72087c450af1a5da138bdfa674a069cf3f09c/tests/sitemaps_tests/test_http.py#L32
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:16>

Django

unread,
Sep 28, 2024, 8:54:29 AM9/28/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jae Hyuck Sa ):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:17>

Django

unread,
Oct 7, 2024, 8:26:27 AM10/7/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:18>

Django

unread,
Oct 15, 2024, 12:12:22 PM10/15/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by JasonCalm):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:19>

Django

unread,
Oct 15, 2024, 12:22:37 PM10/15/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by JasonCalm):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:20>

Django

unread,
Oct 15, 2024, 4:51:08 PM10/15/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------+-----------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by 사재혁):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:21>

Django

unread,
Oct 16, 2024, 4:36:11 AM10/16/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
| Sa
Type: New feature | Status: assigned
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:22>

Django

unread,
Oct 16, 2024, 5:52:31 AM10/16/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
| Sa
Type: New feature | Status: closed
Component: HTTP handling | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"4a685bc0dca5298a7d5a4e162120a90cac7fd1a4" 4a685bc]:
{{{#!CommitTicketReference repository=""
revision="4a685bc0dca5298a7d5a4e162120a90cac7fd1a4"
Fixed #35727 -- Added HttpResponse.text property.

Signed-off-by: SaJH <wogur...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:23>

Django

unread,
Oct 16, 2024, 5:52:32 AM10/16/24
to django-...@googlegroups.com
#35727: Add `response.text_content` to responses
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
| Sa
Type: New feature | Status: closed
Component: HTTP handling | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"0c8177551500e960d2dc04bc4b0fa7060f9172ae" 0c81775]:
{{{#!CommitTicketReference repository=""
revision="0c8177551500e960d2dc04bc4b0fa7060f9172ae"
Refs #35727 -- Updated response.content.decode calls to use the
HttpResponse.text property.

Signed-off-by: SaJH <wogur...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35727#comment:24>
Reply all
Reply to author
Forward
0 new messages