MCPcopy
hub / github.com/mitmproxy/mitmproxy / test_start_stop

Function test_start_stop

test/mitmproxy/addons/test_proxyserver.py:58–107  ·  view source on GitHub ↗
(caplog_async)

Source from the content-addressed store, hash-verified

56
57
58async def test_start_stop(caplog_async):
59 caplog_async.set_level("INFO")
60
61 async def server_handler(
62 reader: asyncio.StreamReader, writer: asyncio.StreamWriter
63 ):
64 assert await reader.readuntil(b"\r\n\r\n") == b"GET /hello HTTP/1.1\r\n\r\n"
65 writer.write(b"HTTP/1.1 204 No Content\r\n\r\n")
66 await writer.drain()
67
68 ps = Proxyserver()
69 nl = NextLayer()
70 state = HelperAddon()
71
72 with taddons.context(ps, nl, state) as tctx:
73 async with tcp_server(server_handler) as addr:
74 tctx.configure(ps, listen_host="127.0.0.1", listen_port=0)
75 assert not ps.servers
76 assert await ps.setup_servers()
77 ps.running()
78 await caplog_async.await_log("HTTP(S) proxy listening at")
79 assert ps.servers
80
81 proxy_addr = ps.listen_addrs()[0]
82 reader, writer = await asyncio.open_connection(*proxy_addr)
83 req = f"GET http://{addr[0]}:{addr[1]}/hello HTTP/1.1\r\n\r\n"
84 writer.write(req.encode())
85 assert (
86 await reader.readuntil(b"\r\n\r\n")
87 == b"HTTP/1.1 204 No Content\r\n\r\n"
88 )
89 assert repr(ps) == "Proxyserver(1 active conns)"
90 assert ps.active_connections() == 1
91
92 await (
93 ps.setup_servers()
94 ) # assert this can always be called without side effects
95 tctx.configure(ps, server=False)
96 await caplog_async.await_log("stopped")
97 if ps.servers.is_updating:
98 async with ps.servers._lock:
99 pass # wait until start/stop is finished.
100 assert not ps.servers
101 assert state.flows
102 assert state.flows[0].request.path == "/hello"
103 assert state.flows[0].response.status_code == 204
104
105 writer.close()
106 await writer.wait_closed()
107 await _wait_for_connection_closes(ps)
108
109
110async def _wait_for_connection_closes(ps: Proxyserver):

Callers

nothing calls this directly

Calls 15

setup_serversMethod · 0.95
runningMethod · 0.95
listen_addrsMethod · 0.95
active_connectionsMethod · 0.95
ProxyserverClass · 0.90
NextLayerClass · 0.90
HelperAddonClass · 0.85
tcp_serverFunction · 0.85
set_levelMethod · 0.80
contextMethod · 0.80
await_logMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…