|
3 | 3 | from ..tutils import Placeholder |
4 | 4 | from ..tutils import Playbook |
5 | 5 | from ..tutils import reply |
| 6 | +from mitmproxy import connection |
| 7 | +from mitmproxy import options |
6 | 8 | from mitmproxy.proxy.commands import CloseConnection |
7 | 9 | from mitmproxy.proxy.commands import CloseTcpConnection |
8 | 10 | from mitmproxy.proxy.commands import OpenConnection |
|
11 | 13 | from mitmproxy.proxy.events import DataReceived |
12 | 14 | from mitmproxy.proxy.layers import tcp |
13 | 15 | from mitmproxy.proxy.layers.tcp import TcpMessageInjected |
| 16 | +from mitmproxy.proxy import context as proxy_context |
14 | 17 | from mitmproxy.tcp import TCPFlow |
15 | 18 | from mitmproxy.tcp import TCPMessage |
16 | 19 |
|
@@ -151,26 +154,37 @@ def test_inject(tctx): |
151 | 154 | assert len(f().messages) == 2 |
152 | 155 |
|
153 | 156 |
|
| 157 | +def _build_tctx() -> proxy_context.Context: |
| 158 | + opts = options.Options() |
| 159 | + client = connection.Client( |
| 160 | + peername=("client", 1234), |
| 161 | + sockname=("127.0.0.1", 8080), |
| 162 | + state=connection.ConnectionState.OPEN, |
| 163 | + timestamp_start=1605699329, |
| 164 | + ) |
| 165 | + return proxy_context.Context(client, opts) |
| 166 | + |
154 | 167 |
|
155 | | -def test_kill_closes_connection_when_flow_not_live(tctx): |
| 168 | +def test_kill_closes_connection_when_flow_not_live(): |
156 | 169 | """If a TCP flow is marked not live, the layer should close rather than relay data.""" |
157 | 170 |
|
158 | 171 | def mark_flow_dead(flow: TCPFlow) -> None: |
159 | 172 | flow.live = False |
160 | 173 |
|
161 | 174 | f = Placeholder(TCPFlow) |
| 175 | + tctx = _build_tctx() |
162 | 176 |
|
163 | 177 | assert ( |
164 | | - Playbook(tcp.TCPLayer(tctx)) |
165 | | - << tcp.TcpStartHook(f) |
166 | | - >> reply() |
167 | | - << OpenConnection(tctx.server) |
168 | | - >> reply(None) |
169 | | - >> DataReceived(tctx.client, b"terminate") |
170 | | - << tcp.TcpMessageHook(f) |
171 | | - >> reply(side_effect=mark_flow_dead) |
172 | | - << CloseConnection(tctx.server, half_close=True) |
173 | | - << None |
| 178 | + Playbook(tcp.TCPLayer(tctx)) |
| 179 | + << tcp.TcpStartHook(f) |
| 180 | + >> reply() |
| 181 | + << OpenConnection(tctx.server) |
| 182 | + >> reply(None) |
| 183 | + >> DataReceived(tctx.client, b"terminate") |
| 184 | + << tcp.TcpMessageHook(f) |
| 185 | + >> reply(side_effect=mark_flow_dead) |
| 186 | + << CloseTcpConnection(tctx.server, half_close=True) |
| 187 | + << None |
174 | 188 | ) |
175 | 189 |
|
176 | 190 | flow = f() |
|
0 commit comments