|
| 1 | +""" |
| 2 | +Integration tests for CORS preflight (OPTIONS) handling. |
| 3 | +
|
| 4 | +Regression test for https://github.com/sparckles/robyn/issues/1346 |
| 5 | +These tests spin up a real Robyn server with ALLOW_CORS enabled and make |
| 6 | +actual HTTP requests — no TestClient. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import pathlib |
| 11 | +import signal |
| 12 | +import socket |
| 13 | +import subprocess |
| 14 | +import time |
| 15 | + |
| 16 | +import pytest |
| 17 | +import requests |
| 18 | + |
| 19 | +CORS_PORT = 8083 |
| 20 | +CORS_HOST = "127.0.0.1" |
| 21 | +CORS_BASE_URL = f"http://{CORS_HOST}:{CORS_PORT}" |
| 22 | +REQUEST_TIMEOUT = 5 |
| 23 | + |
| 24 | +ALLOWED_ORIGIN = "http://localhost:3000" |
| 25 | +DISALLOWED_ORIGIN = "http://evil.example.com" |
| 26 | + |
| 27 | + |
| 28 | +def _start_cors_server(): |
| 29 | + app_path = os.path.join(pathlib.Path(__file__).parent.resolve(), "cors_app.py") |
| 30 | + env = os.environ.copy() |
| 31 | + env["ROBYN_HOST"] = CORS_HOST |
| 32 | + env["ROBYN_PORT"] = str(CORS_PORT) |
| 33 | + |
| 34 | + process = subprocess.Popen( |
| 35 | + ["python3", app_path], |
| 36 | + env=env, |
| 37 | + preexec_fn=os.setsid, |
| 38 | + ) |
| 39 | + |
| 40 | + timeout = 15 |
| 41 | + start = time.time() |
| 42 | + while True: |
| 43 | + if process.poll() is not None: |
| 44 | + raise RuntimeError(f"CORS server exited early with code {process.returncode}") |
| 45 | + if time.time() - start > timeout: |
| 46 | + os.killpg(os.getpgid(process.pid), signal.SIGKILL) |
| 47 | + raise ConnectionError(f"CORS server didn't start on {CORS_HOST}:{CORS_PORT}") |
| 48 | + try: |
| 49 | + sock = socket.create_connection((CORS_HOST, CORS_PORT), timeout=2) |
| 50 | + sock.close() |
| 51 | + break |
| 52 | + except Exception: |
| 53 | + time.sleep(0.5) |
| 54 | + |
| 55 | + time.sleep(1) |
| 56 | + return process |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture(scope="module") |
| 60 | +def cors_server(): |
| 61 | + process = _start_cors_server() |
| 62 | + yield |
| 63 | + try: |
| 64 | + os.killpg(os.getpgid(process.pid), signal.SIGKILL) |
| 65 | + except ProcessLookupError: |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +# --------------------------------------------------------------------------- |
| 70 | +# Preflight (OPTIONS) tests |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | + |
| 73 | + |
| 74 | +def test_options_preflight_returns_204_with_cors_headers(cors_server): |
| 75 | + """Browser sends OPTIONS preflight; server must return 204 with all CORS headers.""" |
| 76 | + resp = requests.options( |
| 77 | + f"{CORS_BASE_URL}/data", |
| 78 | + headers={ |
| 79 | + "Origin": ALLOWED_ORIGIN, |
| 80 | + "Access-Control-Request-Method": "POST", |
| 81 | + "Access-Control-Request-Headers": "Content-Type, Authorization", |
| 82 | + }, |
| 83 | + timeout=REQUEST_TIMEOUT, |
| 84 | + ) |
| 85 | + assert resp.status_code == 204 |
| 86 | + assert resp.headers["Access-Control-Allow-Origin"] == ALLOWED_ORIGIN |
| 87 | + assert "POST" in resp.headers["Access-Control-Allow-Methods"] |
| 88 | + assert resp.headers["Access-Control-Allow-Credentials"] == "true" |
| 89 | + |
| 90 | + |
| 91 | +def test_options_preflight_no_duplicate_allow_origin(cors_server): |
| 92 | + """ |
| 93 | + Regression: ensure Access-Control-Allow-Origin appears exactly once. |
| 94 | + Duplicate values cause browsers to reject the preflight (Fetch spec). |
| 95 | + """ |
| 96 | + resp = requests.options( |
| 97 | + f"{CORS_BASE_URL}/", |
| 98 | + headers={ |
| 99 | + "Origin": ALLOWED_ORIGIN, |
| 100 | + "Access-Control-Request-Method": "GET", |
| 101 | + }, |
| 102 | + timeout=REQUEST_TIMEOUT, |
| 103 | + ) |
| 104 | + raw_headers = resp.raw.headers if hasattr(resp.raw, "headers") else resp.headers |
| 105 | + |
| 106 | + origin_values = raw_headers.getall("Access-Control-Allow-Origin", None) if hasattr(raw_headers, "getall") else None |
| 107 | + |
| 108 | + if origin_values is not None: |
| 109 | + assert len(origin_values) == 1, f"Access-Control-Allow-Origin appeared {len(origin_values)} times: {origin_values}" |
| 110 | + |
| 111 | + assert resp.headers["Access-Control-Allow-Origin"] == ALLOWED_ORIGIN |
| 112 | + |
| 113 | + |
| 114 | +def test_options_preflight_disallowed_origin_returns_403(cors_server): |
| 115 | + """Origins not in the allowed list should be rejected.""" |
| 116 | + resp = requests.options( |
| 117 | + f"{CORS_BASE_URL}/data", |
| 118 | + headers={ |
| 119 | + "Origin": DISALLOWED_ORIGIN, |
| 120 | + "Access-Control-Request-Method": "POST", |
| 121 | + }, |
| 122 | + timeout=REQUEST_TIMEOUT, |
| 123 | + ) |
| 124 | + assert resp.status_code == 403 |
| 125 | + |
| 126 | + |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | +# Normal request tests (non-preflight) |
| 129 | +# --------------------------------------------------------------------------- |
| 130 | + |
| 131 | + |
| 132 | +def test_get_with_allowed_origin_has_cors_headers(cors_server): |
| 133 | + """Normal GET from an allowed origin should carry CORS response headers.""" |
| 134 | + resp = requests.get( |
| 135 | + f"{CORS_BASE_URL}/", |
| 136 | + headers={"Origin": ALLOWED_ORIGIN}, |
| 137 | + timeout=REQUEST_TIMEOUT, |
| 138 | + ) |
| 139 | + assert resp.status_code == 200 |
| 140 | + allow_origin = resp.headers.get("Access-Control-Allow-Origin") |
| 141 | + assert allow_origin is not None |
| 142 | + assert "Access-Control-Allow-Methods" in resp.headers |
| 143 | + |
| 144 | + |
| 145 | +def test_get_without_origin_still_has_global_cors_headers(cors_server): |
| 146 | + """Requests without Origin (e.g. curl, Postman) should still get global CORS headers.""" |
| 147 | + resp = requests.get(f"{CORS_BASE_URL}/", timeout=REQUEST_TIMEOUT) |
| 148 | + assert resp.status_code == 200 |
| 149 | + assert "Access-Control-Allow-Methods" in resp.headers |
| 150 | + |
| 151 | + |
| 152 | +def test_post_with_allowed_origin(cors_server): |
| 153 | + """POST from allowed origin should succeed with CORS headers.""" |
| 154 | + resp = requests.post( |
| 155 | + f"{CORS_BASE_URL}/data", |
| 156 | + headers={ |
| 157 | + "Origin": ALLOWED_ORIGIN, |
| 158 | + "Content-Type": "application/json", |
| 159 | + }, |
| 160 | + data="{}", |
| 161 | + timeout=REQUEST_TIMEOUT, |
| 162 | + ) |
| 163 | + assert resp.status_code == 200 |
| 164 | + |
| 165 | + |
| 166 | +def test_custom_response_headers_not_clobbered_by_globals(cors_server): |
| 167 | + """Route-level headers set by the handler should not be overwritten by globals.""" |
| 168 | + resp = requests.get( |
| 169 | + f"{CORS_BASE_URL}/custom-header", |
| 170 | + headers={"Origin": ALLOWED_ORIGIN}, |
| 171 | + timeout=REQUEST_TIMEOUT, |
| 172 | + ) |
| 173 | + assert resp.status_code == 200 |
| 174 | + assert resp.headers.get("x-custom") == "hello" |
| 175 | + assert "Access-Control-Allow-Methods" in resp.headers |
0 commit comments