MCPcopy
hub / github.com/encode/starlette / handle_request

Method handle_request

starlette/testclient.py:225–373  ·  starlette/testclient.py::_TestClientTransport.handle_request
(self, request: httpx.Request)

Source from the content-addressed store, hash-verified

223 self.client = client
224
225 def handle_request(self, request: httpx.Request) -> httpx.Response:
226 scheme = request.url.scheme
227 netloc = request.url.netloc.decode(encoding=class="st">"ascii")
228 path = request.url.path
229 raw_path = request.url.raw_path
230 query = request.url.query.decode(encoding=class="st">"ascii")
231
232 default_port = {class="st">"http": 80, class="st">"ws": 80, class="st">"https": 443, class="st">"wss": 443}[scheme]
233
234 if class="st">":" in netloc:
235 host, port_string = netloc.split(class="st">":", 1)
236 port = int(port_string)
237 else:
238 host = netloc
239 port = default_port
240
241 class="cm"># Include the class="st">'host' header.
242 if class="st">"host" in request.headers:
243 headers: list[tuple[bytes, bytes]] = []
244 elif port == default_port: class="cm"># pragma: no cover
245 headers = [(bclass="st">"host", host.encode())]
246 else: class="cm"># pragma: no cover
247 headers = [(bclass="st">"host", (fclass="st">"{host}:{port}").encode())]
248
249 class="cm"># Include other request headers.
250 headers += [(key.lower().encode(), value.encode()) for key, value in request.headers.multi_items()]
251
252 scope: dict[str, Any]
253
254 if scheme in {class="st">"ws", class="st">"wss"}:
255 subprotocol = request.headers.get(class="st">"sec-websocket-protocol", None)
256 if subprotocol is None:
257 subprotocols: Sequence[str] = []
258 else:
259 subprotocols = [value.strip() for value in subprotocol.split(class="st">",")]
260 scope = {
261 class="st">"type": class="st">"websocket",
262 class="st">"path": unquote(path),
263 class="st">"raw_path": raw_path.split(bclass="st">"?", 1)[0],
264 class="st">"root_path": self.root_path,
265 class="st">"scheme": scheme,
266 class="st">"query_string": query.encode(),
267 class="st">"headers": headers,
268 class="st">"client": self.client,
269 class="st">"server": [host, port],
270 class="st">"subprotocols": subprotocols,
271 class="st">"state": self.app_state.copy(),
272 class="st">"extensions": {class="st">"websocket.http.response": {}},
273 }
274 session = WebSocketTestSession(self.app, scope, self.portal_factory)
275 raise _Upgrade(session)
276
277 scope = {
278 class="st">"type": class="st">"http",
279 class="st">"http_version": class="st">"1.1",
280 class="st">"method": request.method,
281 class="st">"path": unquote(path),
282 class="st">"raw_path": raw_path.split(bclass="st">"?", 1)[0],

Callers

nothing calls this directly

Calls 6

_UpgradeClass · 0.85
decodeMethod · 0.80
multi_itemsMethod · 0.80
readMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected