| ... | @@ -1,12 +1,13 @@ | ... | @@ -1,12 +1,13 @@ |
| 1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
| 2 | import argparse | 2 | import argparse |
| 3 | from http.client import HTTPConnection | 3 | from http.client import HTTPSConnection |
| 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 5 | import json | 5 | import json |
| 6 | import os | 6 | import os |
| 7 | from pathlib import Path | 7 | from pathlib import Path |
| 8 | import re | 8 | import re |
| 9 | import socket | 9 | import socket |
| | 10 | import ssl |
| 10 | import subprocess | 11 | import subprocess |
| 11 | import tempfile | 12 | import tempfile |
| 12 | import threading | 13 | import threading |
| ... | @@ -28,6 +29,8 @@ class Hub(BaseHTTPRequestHandler): | ... | @@ -28,6 +29,8 @@ class Hub(BaseHTTPRequestHandler): |
| 28 | key = "management-fixture" if self.path.startswith("/v0/") else "client-fixture" | 29 | key = "management-fixture" if self.path.startswith("/v0/") else "client-fixture" |
| 29 | if self.headers.get("Authorization") != "Bearer " + key: | 30 | if self.headers.get("Authorization") != "Bearer " + key: |
| 30 | status, body = 401, b'{"error":"unauthorized"}' | 31 | status, body = 401, b'{"error":"unauthorized"}' |
| | 32 | elif self.path.startswith("/v1/transport-canceled"): |
| | 33 | status, body = 499, b"" |
| 31 | elif self.path.startswith("/v1/slow"): | 34 | elif self.path.startswith("/v1/slow"): |
| 32 | type(self).started.set() | 35 | type(self).started.set() |
| 33 | time.sleep(12) | 36 | time.sleep(12) |
| ... | @@ -48,6 +51,8 @@ class Hub(BaseHTTPRequestHandler): | ... | @@ -48,6 +51,8 @@ class Hub(BaseHTTPRequestHandler): |
| 48 | self.send_header("Set-Cookie", "response-cookie-private-fixture") | 51 | self.send_header("Set-Cookie", "response-cookie-private-fixture") |
| 49 | self.send_header("X-Fixture-Request-ID", self.headers.get("X-Request-ID", "")) | 52 | self.send_header("X-Fixture-Request-ID", self.headers.get("X-Request-ID", "")) |
| 50 | self.send_header("X-Request-ID", "upstream-private-fixture") | 53 | self.send_header("X-Request-ID", "upstream-private-fixture") |
| | 54 | if self.path == "/v1/transport-canceled-no-retry": |
| | 55 | self.send_header("X-Should-Retry", "false") |
| 51 | self.end_headers() | 56 | self.end_headers() |
| 52 | self.wfile.write(body) | 57 | self.wfile.write(body) |
| 53 | except (BrokenPipeError, ConnectionResetError): | 58 | except (BrokenPipeError, ConnectionResetError): |
| ... | @@ -135,18 +140,19 @@ def main(): | ... | @@ -135,18 +140,19 @@ def main(): |
| 135 | site = rendered.split(host + " {\n", 1)[1].split("\n}\n", 1)[0] | 140 | site = rendered.split(host + " {\n", 1)[1].split("\n}\n", 1)[0] |
| 136 | site = site.replace(" tls internal\n", "") | 141 | site = site.replace(" tls internal\n", "") |
| 137 | config = root / "Caddyfile" | 142 | config = root / "Caddyfile" |
| 138 | content = f"{{\n admin 127.0.0.1:{admin}\n auto_https off\n}}\nhttp://127.0.0.1:{port} {{\n{site}\n}}\n" | 143 | content = f"{{\n admin 127.0.0.1:{admin}\n auto_https disable_redirects\n skip_install_trust\n servers {{\n timeouts {{\n read_body_idle 1s\n }}\n }}\n}}\nhttps://localhost:{port} {{\n bind 127.0.0.1\n tls internal\n{site}\n}}\n" |
| 139 | config.write_text(content) | 144 | config.write_text(content) |
| 140 | for backend in (hub, login): | 145 | for backend in (hub, login): |
| 141 | threading.Thread(target=backend.serve_forever, daemon=True).start() | 146 | threading.Thread(target=backend.serve_forever, daemon=True).start() |
| 142 | log_path = root / "caddy.log" | 147 | log_path = root / "caddy.log" |
| | 148 | tls = ssl._create_unverified_context() |
| 143 | with log_path.open("w") as log: | 149 | with log_path.open("w") as log: |
| 144 | process = subprocess.Popen([args.caddy, "run", "--config", str(config), "--adapter", "caddyfile"], stdout=log, stderr=log, env=environment) | 150 | process = subprocess.Popen([args.caddy, "run", "--config", str(config), "--adapter", "caddyfile"], stdout=log, stderr=log, env=environment) |
| 145 | try: | 151 | try: |
| 146 | def request(path, key=None, method="GET", body=None, **headers): | 152 | def request(path, key=None, method="GET", body=None, **headers): |
| 147 | if key: | 153 | if key: |
| 148 | headers["Authorization"] = "Bearer " + key | 154 | headers["Authorization"] = "Bearer " + key |
| 149 | connection = HTTPConnection("127.0.0.1", port, timeout=20) | 155 | connection = HTTPSConnection("localhost", port, timeout=20, context=tls) |
| 150 | connection.request(method, path, body=body, headers=headers) | 156 | connection.request(method, path, body=body, headers=headers) |
| 151 | response = connection.getresponse() | 157 | response = connection.getresponse() |
| 152 | result = response.status, dict(response.getheaders()), response.read() | 158 | result = response.status, dict(response.getheaders()), response.read() |
| ... | @@ -169,6 +175,22 @@ def main(): | ... | @@ -169,6 +175,22 @@ def main(): |
| 169 | assert result == status, (path, result) | 175 | assert result == status, (path, result) |
| 170 | assert headers["X-Request-Id"] == headers["X-Fixture-Request-Id"] | 176 | assert headers["X-Request-Id"] == headers["X-Fixture-Request-Id"] |
| 171 | assert headers["X-Request-Id"] != "spoof-private-fixture" | 177 | assert headers["X-Request-Id"] != "spoof-private-fixture" |
| | 178 | assert "X-Should-Retry" not in headers |
| | 179 | status, headers, _ = request("/v1/transport-canceled", "client-fixture") |
| | 180 | assert status == 499 and headers["X-Should-Retry"] == "true" |
| | 181 | status, headers, _ = request("/v1/transport-canceled-no-retry", "client-fixture") |
| | 182 | assert status == 499 and headers["X-Should-Retry"] == "false", (status, headers) |
| | 183 | client = HTTPSConnection("localhost", port, timeout=5, context=tls) |
| | 184 | client.putrequest("POST", "/v1/upload-idle") |
| | 185 | client.putheader("Authorization", "Bearer client-fixture") |
| | 186 | client.putheader("Content-Length", "1000000") |
| | 187 | client.endheaders(b"x" * 40000) |
| | 188 | response = client.getresponse() |
| | 189 | assert response.status in (499, 504), response.status |
| | 190 | if response.status == 499: |
| | 191 | assert response.headers["X-Should-Retry"] == "true" |
| | 192 | response.read() |
| | 193 | client.close() |
| 172 | assert request("/api/summary", "client-fixture")[0] == 302 | 194 | assert request("/api/summary", "client-fixture")[0] == 302 |
| 173 | assert request("/v1", "client-fixture")[0] == 302 | 195 | assert request("/v1", "client-fixture")[0] == 302 |
| 174 | Hub.started.clear() | 196 | Hub.started.clear() |
| ... | @@ -182,13 +204,13 @@ def main(): | ... | @@ -182,13 +204,13 @@ def main(): |
| 182 | assert outcome and outcome[0][0] == 200 and outcome[0][2].endswith(b"data: complete\n\n"), outcome | 204 | assert outcome and outcome[0][0] == 200 and outcome[0][2].endswith(b"data: complete\n\n"), outcome |
| 183 | assert request("/v1/broken", "client-fixture")[0] == 502 | 205 | assert request("/v1/broken", "client-fixture")[0] == 502 |
| 184 | Hub.started.clear() | 206 | Hub.started.clear() |
| 185 | client = socket.create_connection(("127.0.0.1", port)) | 207 | client = tls.wrap_socket(socket.create_connection(("127.0.0.1", port)), server_hostname="localhost") |
| 186 | client.sendall(f"POST /v1/upload-canceled HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nAuthorization: Bearer client-fixture\r\nContent-Length: 100000\r\n\r\n".encode() + b"x" * 40000) | 208 | client.sendall(f"POST /v1/upload-canceled HTTP/1.1\r\nHost: localhost:{port}\r\nAuthorization: Bearer client-fixture\r\nContent-Length: 100000\r\n\r\n".encode() + b"x" * 40000) |
| 187 | assert Hub.started.wait(5) | 209 | assert Hub.started.wait(5) |
| 188 | client.close() | 210 | client.close() |
| 189 | Hub.started.clear() | 211 | Hub.started.clear() |
| 190 | client = socket.create_connection(("127.0.0.1", port)) | 212 | client = tls.wrap_socket(socket.create_connection(("127.0.0.1", port)), server_hostname="localhost") |
| 191 | client.sendall(f"GET /v1/cancel HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nAuthorization: Bearer client-fixture\r\n\r\n".encode()) | 213 | client.sendall(f"GET /v1/cancel HTTP/1.1\r\nHost: localhost:{port}\r\nAuthorization: Bearer client-fixture\r\n\r\n".encode()) |
| 192 | assert Hub.started.wait(5) | 214 | assert Hub.started.wait(5) |
| 193 | client.close() | 215 | client.close() |
| 194 | deadline = time.monotonic() + 5 | 216 | deadline = time.monotonic() + 5 |
| ... | @@ -201,7 +223,7 @@ def main(): | ... | @@ -201,7 +223,7 @@ def main(): |
| 201 | continue | 223 | continue |
| 202 | if entry.get("service") == service: | 224 | if entry.get("service") == service: |
| 203 | entries.append(entry) | 225 | entries.append(entry) |
| 204 | if any(entry.get("status") == 499 for entry in entries): | 226 | if any(entry.get("status") == 499 and entry.get("path") == "/v1/cancel" for entry in entries): |
| 205 | break | 227 | break |
| 206 | assert time.monotonic() < deadline, entries | 228 | assert time.monotonic() < deadline, entries |
| 207 | time.sleep(.05) | 229 | time.sleep(.05) |
| ... | @@ -210,7 +232,7 @@ def main(): | ... | @@ -210,7 +232,7 @@ def main(): |
| 210 | assert all(re.fullmatch(r"127\.0\.0\.1:[0-9]+", entry["client"]) for entry in entries) | 232 | assert all(re.fullmatch(r"127\.0\.0\.1:[0-9]+", entry["client"]) for entry in entries) |
| 211 | assert any(entry.get("upstream_header_ms") for entry in entries if entry.get("status") == 200) | 233 | assert any(entry.get("upstream_header_ms") for entry in entries if entry.get("status") == 200) |
| 212 | assert all(entry.get("upstream_status") == 200 for entry in entries if entry.get("status") == 200) | 234 | assert all(entry.get("upstream_status") == 200 for entry in entries if entry.get("status") == 200) |
| 213 | assert all(not entry.get("upstream_status") for entry in entries if entry.get("status") == 499) | 235 | assert all(not entry.get("upstream_status") for entry in entries if entry.get("status") == 499 and entry.get("path") == "/v1/cancel") |
| 214 | upload = next(entry for entry in entries if entry.get("path") == "/v1/upload-canceled") | 236 | upload = next(entry for entry in entries if entry.get("path") == "/v1/upload-canceled") |
| 215 | assert upload["status"] in (499, 502) and upload["content_length"] == "100000" and upload["bytes_read"] == 40000 | 237 | assert upload["status"] in (499, 502) and upload["content_length"] == "100000" and upload["bytes_read"] == 40000 |
| 216 | complete = next(entry for entry in entries if entry.get("path") == "/v1/slow") | 238 | complete = next(entry for entry in entries if entry.get("path") == "/v1/slow") |