TestClient seems to ignore explicit client address / host header #2987
Unanswered
bjd183
asked this question in
Potential Issue
Replies: 1 comment 2 replies
-
You should be using |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The docs indicate that the test client host header can be set as follows:
When using
TrustedHostMiddleware
withALLOWED_HOSTS=['localhost']
, I receive a400 Invalid host header
when callingclient.get(url)
.Setting the client tuple appears to have no effect because
httpx
sets the host header to matchurl.netloc
here when creating a request. The workaround is to useTestClient(app, base_url='http://localhost')
instead ofTestClient(app, client=('localhost', 8000))
.The TestClient passes the client tuple to the TestClientTransport which, in turn, sets the value into the
scope
variable for use by asgi. It's not clear which of these two approaches is intended or preferred, whether they are compatible or mutually exclusive. Is there something I am missing or not understanding?Beta Was this translation helpful? Give feedback.
All reactions