| ... | ... | @@ -1,12 +1,13 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | import argparse |
| 3 | | from http.client import HTTPConnection |
| 3 | from http.client import HTTPSConnection |
| 4 | 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 5 | 5 | import json |
| 6 | 6 | import os |
| 7 | 7 | from pathlib import Path |
| 8 | 8 | import re |
| 9 | 9 | import socket |
| 10 | import ssl |
| 10 | 11 | import subprocess |
| 11 | 12 | import tempfile |
| 12 | 13 | import threading |
| ... | ... | @@ -28,6 +29,8 @@ class Hub(BaseHTTPRequestHandler): |
| 28 | 29 | key = "management-fixture" if self.path.startswith("/v0/") else "client-fixture" |
| 29 | 30 | if self.headers.get("Authorization") != "Bearer " + key: |
| 30 | 31 | status, body = 401, b'{"error":"unauthorized"}' |
| 32 | elif self.path.startswith("/v1/transport-canceled"): |
| 33 | status, body = 499, b"" |
| 31 | 34 | elif self.path.startswith("/v1/slow"): |
| 32 | 35 | type(self).started.set() |
| 33 | 36 | time.sleep(12) |
| ... | ... | @@ -48,6 +51,8 @@ class Hub(BaseHTTPRequestHandler): |
| 48 | 51 | self.send_header("Set-Cookie", "response-cookie-private-fixture") |
| 49 | 52 | self.send_header("X-Fixture-Request-ID", self.headers.get("X-Request-ID", "")) |
| 50 | 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 | 56 | self.end_headers() |
| 52 | 57 | self.wfile.write(body) |
| 53 | 58 | except (BrokenPipeError, ConnectionResetError): |
| ... | ... | @@ -135,18 +140,19 @@ def main(): |
| 135 | 140 | site = rendered.split(host + " {\n", 1)[1].split("\n}\n", 1)[0] |
| 136 | 141 | site = site.replace(" tls internal\n", "") |
| 137 | 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 | 144 | config.write_text(content) |
| 140 | 145 | for backend in (hub, login): |
| 141 | 146 | threading.Thread(target=backend.serve_forever, daemon=True).start() |
| 142 | 147 | log_path = root / "caddy.log" |
| 148 | tls = ssl._create_unverified_context() |
| 143 | 149 | with log_path.open("w") as log: |
| 144 | 150 | process = subprocess.Popen([args.caddy, "run", "--config", str(config), "--adapter", "caddyfile"], stdout=log, stderr=log, env=environment) |
| 145 | 151 | try: |
| 146 | 152 | def request(path, key=None, method="GET", body=None, **headers): |
| 147 | 153 | if key: |
| 148 | 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 | 156 | connection.request(method, path, body=body, headers=headers) |
| 151 | 157 | response = connection.getresponse() |
| 152 | 158 | result = response.status, dict(response.getheaders()), response.read() |
| ... | ... | @@ -169,6 +175,22 @@ def main(): |
| 169 | 175 | assert result == status, (path, result) |
| 170 | 176 | assert headers["X-Request-Id"] == headers["X-Fixture-Request-Id"] |
| 171 | 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 | 194 | assert request("/api/summary", "client-fixture")[0] == 302 |
| 173 | 195 | assert request("/v1", "client-fixture")[0] == 302 |
| 174 | 196 | Hub.started.clear() |
| ... | ... | @@ -182,13 +204,13 @@ def main(): |
| 182 | 204 | assert outcome and outcome[0][0] == 200 and outcome[0][2].endswith(b"data: complete\n\n"), outcome |
| 183 | 205 | assert request("/v1/broken", "client-fixture")[0] == 502 |
| 184 | 206 | Hub.started.clear() |
| 185 | | client = socket.create_connection(("127.0.0.1", port)) |
| 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) |
| 207 | client = tls.wrap_socket(socket.create_connection(("127.0.0.1", port)), server_hostname="localhost") |
| 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 | 209 | assert Hub.started.wait(5) |
| 188 | 210 | client.close() |
| 189 | 211 | Hub.started.clear() |
| 190 | | client = socket.create_connection(("127.0.0.1", port)) |
| 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()) |
| 212 | client = tls.wrap_socket(socket.create_connection(("127.0.0.1", port)), server_hostname="localhost") |
| 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 | 214 | assert Hub.started.wait(5) |
| 193 | 215 | client.close() |
| 194 | 216 | deadline = time.monotonic() + 5 |
| ... | ... | @@ -201,7 +223,7 @@ def main(): |
| 201 | 223 | continue |
| 202 | 224 | if entry.get("service") == service: |
| 203 | 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 | 227 | break |
| 206 | 228 | assert time.monotonic() < deadline, entries |
| 207 | 229 | time.sleep(.05) |
| ... | ... | @@ -210,7 +232,7 @@ def main(): |
| 210 | 232 | assert all(re.fullmatch(r"127\.0\.0\.1:[0-9]+", entry["client"]) for entry in entries) |
| 211 | 233 | assert any(entry.get("upstream_header_ms") for entry in entries if entry.get("status") == 200) |
| 212 | 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 | 236 | upload = next(entry for entry in entries if entry.get("path") == "/v1/upload-canceled") |
| 215 | 237 | assert upload["status"] in (499, 502) and upload["content_length"] == "100000" and upload["bytes_read"] == 40000 |
| 216 | 238 | complete = next(entry for entry in entries if entry.get("path") == "/v1/slow") |