﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32023	HttpRequest.headers doesn't get updated when request.META is updated.	lieryan	nobody	"Currently, Django docs recommended using `request.headers` instead of META to access headers, however the behaviour of `request.headers` currently makes it hard to use correctly during tests.

Currently, the following code fails:

{{{
request.META[""HTTP_USER_AGENT""] = ""foobar""
assert request.headers[""User-Agent""] == ""foobar"" # works
request.META[""HTTP_USER_AGENT""] = ""django""
assert request.headers[""User-Agent""] == ""django"" # fails
}}}

This is because `request.headers` is a `@cached_property` that is initialised when request.headers is first accessed, so underlying changes to META don't get reflected to `request.headers`.

In regular Django request, this isn't that big of a deal, because arguable `request.META` should be immutable anyway, and you probably should be cursed if you modify `request.META` in production code.

However, this is rather annoying when writing tests, because often you want to reuse the same request object to pass to different methods, or you want to test the same method with slightly different header value. Due to this caching, you have the option of either recreating the request from scratch, which can be complicated if parts setting up META is spread between `setUp()` and the test method, or you'd have to delete the `request.headers` to force it to reinitialise, which is rather non obvious and error prone, since you might forget to delete it and cause some tests to pass/fail when they shouldn't.

Also, `request.headers` is read only, which means that you still have to use the WSGI environment names when setting headers in tests rather than the standard HTTP name.

I'd propose `HttpHeaders` should be reimplemented so that it isn't a real collection, but just an accessor for `request.META`, also that `HttpHeaders` should implement `__getitem__()` which should also update `request.META`."	New feature	closed	HTTP handling	3.1	Normal	wontfix			Unreviewed	0	0	0	0	0	0
