|
5 | 5 |
|
6 | 6 | import json |
7 | 7 | from typing import Any |
| 8 | +from unittest.mock import Mock |
8 | 9 | from uuid import UUID |
9 | 10 |
|
10 | 11 | import pytest |
11 | 12 | from aiohttp import web |
12 | 13 | from faker import Faker |
13 | 14 | from pydantic import BaseModel |
14 | | -from simcore_service_webserver.utils_aiohttp import envelope_json_response |
| 15 | +from simcore_service_webserver.utils_aiohttp import envelope_json_response, iter_origins |
15 | 16 |
|
16 | 17 |
|
17 | 18 | @pytest.fixture |
@@ -46,3 +47,67 @@ def test_enveloped_failing_response(): |
46 | 47 | assert resp.text is not None |
47 | 48 |
|
48 | 49 | assert {"error"} == set(json.loads(resp.text).keys()) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "headers, expected_output", |
| 54 | + [ |
| 55 | + # No headers - fallback to request |
| 56 | + ( |
| 57 | + {}, |
| 58 | + [("http", "localhost")], |
| 59 | + ), |
| 60 | + # Single entry |
| 61 | + ( |
| 62 | + {"X-Forwarded-Proto": "https", "X-Forwarded-Host": "example.com"}, |
| 63 | + [("https", "example.com"), ("http", "localhost")], |
| 64 | + ), |
| 65 | + # Multiple entries with ports |
| 66 | + ( |
| 67 | + { |
| 68 | + "X-Forwarded-Proto": "https, http", |
| 69 | + "X-Forwarded-Host": "api.example.com:443, 192.168.1.1:8080", |
| 70 | + }, |
| 71 | + [ |
| 72 | + ("https", "api.example.com"), |
| 73 | + ("http", "192.168.1.1"), |
| 74 | + ("http", "localhost"), |
| 75 | + ], |
| 76 | + ), |
| 77 | + # Unequal list lengths (proto longer) |
| 78 | + ( |
| 79 | + { |
| 80 | + "X-Forwarded-Proto": "http, https, ftp", |
| 81 | + "X-Forwarded-Host": "site.com, cdn.site.com", |
| 82 | + }, |
| 83 | + [("http", "site.com"), ("https", "cdn.site.com"), ("http", "localhost")], |
| 84 | + ), |
| 85 | + # With whitespace |
| 86 | + ( |
| 87 | + { |
| 88 | + "X-Forwarded-Proto": " https , http ", |
| 89 | + "X-Forwarded-Host": " example.com , proxy.com ", |
| 90 | + }, |
| 91 | + [("https", "example.com"), ("http", "proxy.com"), ("http", "localhost")], |
| 92 | + ), |
| 93 | + # Duplicate entries |
| 94 | + ( |
| 95 | + { |
| 96 | + "X-Forwarded-Proto": "https, https", |
| 97 | + "X-Forwarded-Host": "example.com, example.com", |
| 98 | + }, |
| 99 | + [("https", "example.com"), ("http", "localhost")], |
| 100 | + ), |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_iter_origins(headers, expected_output): |
| 104 | + request = Mock( |
| 105 | + spec=web.Request, |
| 106 | + headers=headers, |
| 107 | + scheme="http", |
| 108 | + host="localhost:8080", # Port should be stripped |
| 109 | + ) |
| 110 | + |
| 111 | + results = list(iter_origins(request)) |
| 112 | + |
| 113 | + assert results == expected_output |
0 commit comments