|
2 | 2 | import os |
3 | 3 | import socket |
4 | 4 | import subprocess |
| 5 | +import threading |
5 | 6 | import time |
6 | 7 |
|
7 | 8 | import psutil |
@@ -98,8 +99,14 @@ def start_mitmdump( |
98 | 99 | if proxy_auth: |
99 | 100 | proxy_command += ["-v", "--proxyauth", proxy_auth] |
100 | 101 |
|
| 102 | + proxy_env = os.environ.copy() |
| 103 | + proxy_env["PYTHONUNBUFFERED"] = "1" |
| 104 | + |
101 | 105 | proxy_process = subprocess.Popen( |
102 | | - proxy_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT |
| 106 | + proxy_command, |
| 107 | + stdout=subprocess.PIPE, |
| 108 | + stderr=subprocess.STDOUT, |
| 109 | + env=proxy_env, |
103 | 110 | ) |
104 | 111 |
|
105 | 112 | try: |
@@ -132,15 +139,31 @@ def proxy_test_finally( |
132 | 139 | expected_proxy_logsize = expected_httpserver_logsize |
133 | 140 |
|
134 | 141 | if proxy_process: |
| 142 | + stdout = [] |
| 143 | + |
| 144 | + def read_proxy_stdout(): |
| 145 | + if proxy_process.stdout is None: |
| 146 | + return |
| 147 | + for line in iter(proxy_process.stdout.readline, b""): |
| 148 | + stdout.append(line.decode("utf-8", errors="replace")) |
| 149 | + |
| 150 | + reader = threading.Thread(target=read_proxy_stdout, daemon=True) |
| 151 | + reader.start() |
| 152 | + |
135 | 153 | # Give mitmdump some time to get a response from the mock server |
136 | | - wait_for(lambda: len(httpserver.log) >= expected_httpserver_logsize, timeout) |
| 154 | + assert wait_for( |
| 155 | + lambda: len(httpserver.log) >= expected_httpserver_logsize, timeout |
| 156 | + ) |
| 157 | + if expected_proxy_logsize != 0: |
| 158 | + # request passed through successfully |
| 159 | + assert wait_for( |
| 160 | + lambda: "POST" in "".join(stdout) and "200 OK" in "".join(stdout), |
| 161 | + timeout, |
| 162 | + ) |
137 | 163 | proxy_process.terminate() |
138 | | - stdout_bytes, _ = proxy_process.communicate() |
139 | | - stdout = stdout_bytes.decode("utf-8", errors="replace") |
| 164 | + stdout_bytes, _ = proxy_process.communicate(timeout=timeout) |
| 165 | + stdout = "".join(stdout) + stdout_bytes.decode("utf-8", errors="replace") |
140 | 166 | if expected_proxy_logsize == 0: |
141 | 167 | # don't expect any incoming requests to make it through the proxy |
142 | 168 | proxy_log_assert(stdout) |
143 | | - else: |
144 | | - # request passed through successfully |
145 | | - assert "POST" in stdout and "200 OK" in stdout |
146 | 169 | assert len(httpserver.log) == expected_httpserver_logsize |
0 commit comments