Skip to content

Commit 95b285f

Browse files
committed
argh, it got messy
1 parent 4053f89 commit 95b285f

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

tests/proxy.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import socket
44
import subprocess
5+
import threading
56
import time
67

78
import psutil
@@ -98,8 +99,14 @@ def start_mitmdump(
9899
if proxy_auth:
99100
proxy_command += ["-v", "--proxyauth", proxy_auth]
100101

102+
proxy_env = os.environ.copy()
103+
proxy_env["PYTHONUNBUFFERED"] = "1"
104+
101105
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,
103110
)
104111

105112
try:
@@ -132,15 +139,31 @@ def proxy_test_finally(
132139
expected_proxy_logsize = expected_httpserver_logsize
133140

134141
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+
135153
# 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+
)
137163
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")
140166
if expected_proxy_logsize == 0:
141167
# don't expect any incoming requests to make it through the proxy
142168
proxy_log_assert(stdout)
143-
else:
144-
# request passed through successfully
145-
assert "POST" in stdout and "200 OK" in stdout
146169
assert len(httpserver.log) == expected_httpserver_logsize

0 commit comments

Comments
 (0)